[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 ...
随机推荐
- 洛谷P1503 鬼子进村 [平衡树,STL]
题目传送门 鬼子进村 题目背景 小卡正在新家的客厅中看电视.电视里正在播放放了千八百次依旧重播的<亮剑>,剧中李云龙带领的独立团在一个县城遇到了一个鬼子小队,于是独立团与鬼子展开游击战. ...
- Python类总结-封装(私有属性,方法)
封装基础 广义上面向对象的封装:代码的保护,面向对象的思想本身就是一种封装 只让自己的对象能调用自己类中的方法 狭义上的封装-面向对象三大特性之一(私有变量,用公有的方法封装私有属性,方法叫封装) 把 ...
- Elasticsearch 删除索引下的所有数据
下面是head中操作的截图 #清空索引 POST quality_control/my_type/_delete_by_query?refresh&slices=5&pretty { ...
- CUDA学习笔记4:CUDA(英伟达显卡统一计算架构)代码运行时间测试
CUDA内核运行时间的测量函数 cudaEvent_t start1; cudaEventCreate(&start1); cudaEvent_t stop1; cudaEventCreate ...
- django基础入门
1. http协议 1.1 请求协议 请求协议格式: 请求首行: // 请求方式 请求路径 协议和版本,例如:GET /index.html HTTP/1.1 请求头信息: // 请求头名称:请求头内 ...
- lightoj 1296 - Again Stone Game 博弈论
思路:由于数据很大,先通过打表找规律可以知道, 当n为偶数的时候其SG值为n/2; 当n为奇数的时候一直除2,直到为偶数m,则SG值为m/2; 代码如下: #include<stdio.h> ...
- 实用小工具 -- 国家地区IP段范围查询工具
如果想限制某个国家地区IP段访问,这几个查询工具就很有用了. 可以查询各个国家IP段范围,并且是持续更新的,使用方便. 当然,除此之外,你还可以通过APNIC.ARIN.RIPE这些官方IP分配机构查 ...
- 更新teaching中fdSubjectID为null的老数据
UPDATE wkwke.tbTeachingV3 teaching SET teaching.fdSubjectID = ( SELECT fdValue FR ...
- 查看linux并发连接数的方法
查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态:netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, ...
- Unity3D架构设计NavMesh寻路(未完待续)
国庆闲来没事把NavMesh巩固一下.以Unity3D引擎为例写一个底层c# NavMesh寻路.由于Unity3D中本身自带的NavMesh寻路不能非常好的融入到游戏项目其中,所以重写一个NavMe ...