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. NOIP2013 D1 T3 货车运输

    好吧,遇上这种题,作为蒟蒻的我第一个想到的就是怎么打暴力,然而暴力都打不好QAQ!!!于是只能等教练讲解以后,然后在大犇的指导下终于做出来了. 对了,,好像还,没上题....: 题目描述 A 国有 n ...

  2. 微信小程序开发之路之组件化

    类似于页面,自定义组件拥有自己的 wxml 模版和 wxss 样式. 官方链接 组件化,反过来理解,写重复的页面,方法,写第二遍就烦了,抽取出来就是组件化,可以理解为公用的方法 对于通用的数据,最先想 ...

  3. JSP中动态INCLUDE与静态INCLUDE的区别?

    动态INCLUDE用jsp:include动作实现 它总是会检查所含文件中的变化,适合用于包含动态页面,并且可以带参数 静态INCLUDE用include伪码实现,定不会检查所含文件的变化,适用于包含 ...

  4. 选择器(UIPickerView)

    Apple提供了两种形式的选择器:日期选择器(UIDatePicker)与自定义选择器(UIPickerView). 当用户使用日期选择器选定一个时间后,调用datePickerID.date可以直接 ...

  5. 欧拉回路 uoj117

    写了一道欧拉回路的模板题.先判断是否是欧拉回路,有向图和无向图有一点点不同,然后就是特判独立点的存在. 之后是输出路径,和dls学的dfs,利用last数组的更新可以做到线性的复杂度,否则一不小心就会 ...

  6. 某DP题目4

    题意 有两个栈分别有n和m个数,每次从任意栈中取出一个数,令k为不同输出序列的总数,其中第i种输出序列的产生方式有ai个,求Σai2. n <= 500 分析 此题是关于ai2转换.咋一看此题好 ...

  7. ThinkPHP -- 去除URL中的index.php

    原路径是 http://localhost/test/index.php/index/add 想获得的地址是 http://localhost/test/index/add 那么如何去掉index.p ...

  8. Codeforces Round #302 (Div. 2) D - Destroying Roads 图论,最短路

    D - Destroying Roads Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544 ...

  9. IOS上传图片方法类

    IOS上传图片方法类   iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...

  10. 关于hessian接口类方法顺序及对象序列化的实战研究

    前段时间,提供出去的hessian接口被调用时,调用方出现序列化异常以及其他莫名的异常.同事说hessian接口使用有2个注意事项: 1.对于已经存在的hessian接口,后续增加的方法只能加在文件末 ...