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. Python之路【第五篇】: 函数、闭包、装饰器、迭代器、生成器

    目录 函数补充进阶 函数对象 函数的嵌套 名称空间与作用域 闭包函数 函数之装饰器 函数之可迭代对象 函数之迭代器 函数之生成器 面向过程的程序设计思想 一.函数进阶之函数对象 1. 函数对象 秉承着 ...

  2. TCP/IP——基础概念简记

    TCP/IP协议族的分层: 应用层 运输层 网络层 链路层 互联网地址(IP地址):互联网上的每个接口必须有一个唯一的Internet地址,它一定的结构,分为ABCDE五类.A类保留给政府机构,B类分 ...

  3. [转]iOS开发new与alloc/init的区别

    1.在实际开发中很少会用到new,一般创建对象咱们看到的全是[[className alloc] init] 但是并不意味着你不会接触到new,在一些代码中还是会看到[className new], ...

  4. [BZOJ4340][BJOI2015]隐身术(后缀数组)

    考虑到K很小,于是可以暴搜每次用的是哪种操作,跳过AB相等的字符可以用SA求LCP加速. 主要流程就是,枚举B的每个后缀,对每个后缀统计合法前缀个数.DFS搜索每次决策,用SA跳过相同字符,当A或B匹 ...

  5. [BZOJ1040][ZJOI2008]骑士(环套树dp)

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5816  Solved: 2263[Submit][Status ...

  6. [转]Intent和IntentFilter详解

        Intent   Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描 ...

  7. [转]Android 中fill_parent与wrap_content的区别

        在Android中,对于组件的属性“layout_width”和“layout_height”, 其值总是设置为“wrap_content”或“fill_parent”. 那么,这两个值有什么 ...

  8. BZOJ 4027: [HEOI2015]兔子与樱花 树上dp

    4027: [HEOI2015]兔子与樱花 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline ...

  9. myBatis 切换数据源(spring事务)理解

    1. mybatis (SqlSessionTemplate)的动态代理 a) sqlSession的结构 b)SqlSession 结构 public class SqlSessionTemplat ...

  10. Ubuntu中升极下载4.2内核

    http://tech.hexun.com/2015-09-11/179027013.html 从这段话中所表达出的意思可以了解,Linux Kernel 4.3版本已经开始进行,Linus Torv ...