[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 ...
随机推荐
- 洛谷P2801 教主的魔法 [分块,二分答案]
题目传送门 教主的魔法 题目描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. ...
- Python数据类型-列表(list)增删改查
1.添加元素 添加单个元素:使用append(object)函数可以为列表添加单个元素,参数object为对象:也就是说所有Python的对象都可以添加到列表中. 添加多个元素(合并列表):使用ext ...
- COMP COMP-3
Comp (Computational) Comp (with no suffix) leaves the choice of the data type to the compiler writer ...
- TRUNCATE delete
The operation cannot be rolled back. DROP and TRUNCATE are DDL commands, whereas DELETE is a DML com ...
- RxJava 机制
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha rxjava 是 以 响应式 编程思想 编程的 java类库
- Linux下C语言多文件的编译以及makefile的应用
1.关于编译和链接 一般来说,无论是C.C++,首先要把源文件编译成中间代码文件,在Windows下也就是.obj文件,UNIX下是.o文件,即Object File,这个动作叫做编译(compile ...
- Android UI设计规范之常用单位
px :全称--pixel .像素.例如,480*800的屏幕在横向有320个象素,在纵向有480个象素. 屏幕的分辨率 : 屏幕的长宽方向上像素点的数量. dp(dip) : 全称--Density ...
- 常用数据库validationQuery检查语句
数据库 validationQuery Oracle select 1 from dual DB2 select 1 from sysibm.sysdummy1 mysql select 1 mi ...
- BZOJ1002輪狀病毒 暴搜 + 找規律 + 高精度
@[暴搜, 找規律, 高精度] Description 轮状病毒有很多变种,所有轮状病毒的变种都是从一个轮状基产生的.一个\(n\)轮状基由圆环上\(n\)个不同的基原子和圆心处一个核原子构成的,2个 ...
- 读书笔记_Effective_C++_条款二十七:尽量少做转型动作
有关转型的几种做法,已经在早些的博客中写过了.这里先简单回顾一下,再讲一讲effective中对之更深入的阐述. 转型可以按风格可以分成C风格转型和C++风格转型两大类,C风格转型很容易看到,因为我们 ...