[Functional Programming] Write a simple version of Maybe
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的更多相关文章
- [Functional Programming] Write simple Semigroups type
An introduction to concatting items via the formal Semi-group interface. Semi-groups are simply a ty ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- 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 ...
- 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 ...
- Functional programming
In computer science, functional programming is a programming paradigm, a style of building the struc ...
- 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 ...
- [Functional Programming] Function signature
It is really important to understand function signature in functional programming. The the code exam ...
- Monad (functional programming)
In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
随机推荐
- 去除win7桌面图标小箭头.bat
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Icons" ...
- java反射,代码优化
java的反射机制属实强大,能解决好些问题 在接手别人写的代码的时候,有一个bean类的get方法特别low,我都看不下去 重复代码写五遍,我都觉得太不合理.之后将其中代码抽取出来修改了下. publ ...
- data:image/png;base64这什么玩意
周末下载了个开源的cms系统,基于java. jeecms 这是官网链接 后台页面采用VUE技术全面进行了改版 我勒个去,啥玩意,无非就是js的框架罢了.vue文件 之后再后台管理页面调试的时候发现了 ...
- python计数器Count
python计数器Count # -*- coding:utf-8 -*- """ python计数器Counter 需导入模块collections "&qu ...
- django配置Ueditor
1.安装DjangoUeditor pip install DjangoUeditor 2.在Django中安装DjangoUedito app,在INSTALL_APPS里面增加DjangoUedi ...
- Python中yield和yield from的用法
yield python中yield的用法很像return,都是提供一个返回值,但是yield和return的最大区别在于,return一旦返回,则代码段执行结束,但是yield在返回值以后,会交出C ...
- 批量 修改 android eclipse 项目名
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 最外层的 文件夹名字.
- 【二分答案】【DFS】【分类讨论】Gym - 100851F - Froggy Ford
题意:河里有n块石头,一只青蛙要从左岸跳到右岸,你可以再在任意一个位置放一块石头,使得在最优方案下,青蛙单步跳的距离的最大值最小化,输出该位置. 将原图视作完全图,二分答案mid,然后在图中只保留小于 ...
- [转] Eclipse的Tomcat插件安装
Eclipse的Tomcat服务器插件tomcatPlugin是由Sysdeo公司开发的,其下载地址是:http://www.eclipsetotale.com/tomcatPlugin.html ...
- paip.手机时间设置不能修改灰色禁用 解决大法
paip.手机时间设置不能修改灰色禁用 解决大法 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net ...