Maybe has two types: Just / Nothing. Just() will just return the value that passed in. Nothing returns nothing...

Just/ Nothing are both functors, they should have .map() method:

const Just = x => ({
map: f => Just(f(x)),
inspect: () => `Just ${x}`,
}) const Nothing = x => ({
map: f => Nothing('Nothing'),
inspect: () => `${x}`,
}) const Maybe = {
Just,
Nothing
}

We added 'inspect' method so that in REPL we can log out understandable message. for example, 'Just 4'  instead of '{ map: [Function: map] }'.... or whatever...

Currently, the code below return 'Just 5'

const inc = n => n + ;

const input = Just()
const result = input.map(inc)

But we don't need 'Just' as a result, we want just 5; in order to achieve that, we add 'option()' method:

const Just = x => ({
map: f => Just(f(x)),
inspect: () => `Just ${x}`,
option: (_) => x,
}) const Nothing = x => ({
map: f => Nothing('Nothing'),
inspect: () => `${x}`,
option: defVal => defVal
})

For Just, it return whatever the current value 'x', ignore the default value we passed in; Nothing it will return the defautl vlaue back.

Now, we got:

const input = Just(4)
const result = input.map(inc).option() // const input = Nothing()
const result = input.map(in) // Nothing
const result = input.map(inc).option() //

Since we don't know it should be Just or Nothing, we can use some predict function as helper:

const safeNum = num => typeof num === 'number' ? Maybe.Just(num): Maybe.Nothing()
const input = safeNum()
const result = input.map(inc).option() // const input = safeNum('')
const result = input.map(inc).option() //

---------------

const Just = x => ({
map: f => Just(f(x)),
inspect: () => `Just ${x}`,
option: (_) => x,
}) const Nothing = x => ({
map: f => Nothing('Nothing'),
inspect: () => `${x}`,
option: defVal => defVal
}) const safeNum = num => typeof num === 'number' ? Maybe.Just(num): Maybe.Nothing() const Maybe = {
Just,
Nothing
} const inc = n => n + ; const input = safeNum()
const result = input.map(inc).option() console.log(result)

[Functional Programming] Write a simple version of Maybe的更多相关文章

  1. [Functional Programming] Write simple Semigroups type

    An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...

  2. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  3. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  4. a primary example for Functional programming in javascript

    background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...

  5. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  6. BETTER SUPPORT FOR FUNCTIONAL PROGRAMMING IN ANGULAR 2

    In this blog post I will talk about the changes coming in Angular 2 that will improve its support fo ...

  7. [Functional Programming] Function signature

    It is really important to understand function signature in functional programming. The the code exam ...

  8. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  9. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

随机推荐

  1. JS延迟执行

    <!DOCTYPE html> <html> <head> <title></title> <script type="te ...

  2. FastReport.Net使用:[34]小册子报表(奇偶页)

    打印一份小册子类型的报表,能实现如下要求: ●单独的封面,目录,报表内容,背面 ●奇偶页不同的页边距 ●奇偶页不同的页面/页脚 下面的例子将用到以上3点. 1.奇偶页的实现主要通过报表控件对象的Pri ...

  3. [BZOJ4373]算术天才⑨与等差数列(线段树)

    [l,r]中所有数排序后能构成公差为k的等差数列,当且仅当: 1.区间中最大数-最小数=k*(r-l) 2.k能整除区间中任意两个相邻数之差,即k | gcd(a[l+1]-a[l],a[l+2]-a ...

  4. Android UI设计规范之常用单位

    px :全称--pixel .像素.例如,480*800的屏幕在横向有320个象素,在纵向有480个象素. 屏幕的分辨率 : 屏幕的长宽方向上像素点的数量. dp(dip) : 全称--Density ...

  5. LinkCutTree 总结

    最近学习了LinkCutTree,总结一下. LinkCutTree是一种数据结构(是Tree Decomposition中的一种),她维护的一般是无向图(一个森林),支持连边.删边.链修改.链查询( ...

  6. bzoj1393 旅游航道

    Description SGOI旅游局在SG-III星团开设了旅游业务,每天有数以万计的地球人来这里观光,包括联合国秘书长,各国总统和SGOI总局局长等.旅游线路四通八达,每天都有总躲得载客太空飞船在 ...

  7. WebSQL的基本使用过程

    1.创建或打开数据库(openDatabase) var db = openDatabase('dbname', '1.0', 'discription', 2 * 1024); // 目前测试只有C ...

  8. JDK源码(1.7) -- java.util.Deque<E>

    java.util.Deque<E> 源码分析(JDK1.7) -------------------------------------------------------------- ...

  9. MySQL之char、varchar和text的设计

    最近有表结构设计中出现了varchar(10000)的设计引起了大家的讨论,我们下面就来分析分析. 首先我们先普及一下常识: 1.char(n)和varchar(n)中括号中n代表字符的个数,并不代表 ...

  10. SMT Surface Mount Technology footprint references

    http://en.wikipedia.org/wiki/Surface-mount_technology Surface-mount technology (SMT) is a method for ...