[Javascript] Maybe Functor
In normal Javascript, we do undefine check or null check:
var person = {age: , name: "Suvi"};
var name = person.name ? person.name: null;
Sometime backend data return may contain or not contain 'name' prop.
So let's see how to define a Maybe() functor:
var _Maybe.prototype.map = function(f) {
return this.val ? Maybe(f(this.val)) : Maybe(null);
}
map(capitalize, Maybe("flamethrower"))
//=> Maybe(“Flamethrower”)
The idea of Maybe is check whetehr the Container has value or not, if not return Maybe(null), if has then return Maybe(val).
// Exercise 3
// ==========
// Use safeGet and _.head to find the first initial of the user
var _ = R;
var P = PointFree;
var map = P.fmap;
var compose = P.compose;
var Maybe = P.Maybe;
var Identity = P.Id;
var safeGet = _.curry(function(x,o){ return Maybe(o[x]) })
var user = {id: , name: "Albert"}
console.log("--------Start exercise 3--------")
var ex3 = compose(map(_.head), safeGet('name'));
assertDeepEqual(Maybe('A'), ex3(user))
console.log("exercise 3...ok!")
So after the "safeGet('name')" run, it return "Maybe('Albert')";
Then we use map(_.head): map--> get into the Maybe to check val;
_.head --> Get teh 'A' or 'Albert'.
The good thing about this is if use 'safeGet('address')' which inside 'user' object doesn't provide, then we will get 'Maybe(null)':
"Uncaught expected Maybe(A) to equal Maybe(null) (line 68)"
// Exercise 4
// ==========
// Use Maybe to rewrite ex4 without an if statement
console.log("--------Start exercise 4--------")
var _ = R;
var P = PointFree;
var map = P.fmap;
var compose = P.compose;
var Maybe = P.Maybe;
var Identity = P.Id;
var ex4 = function(n) {
if(n){
return parseInt(n);
}
}
var ex4 = compose(map(parseInt), Maybe)
assertDeepEqual(Maybe(), ex4(""))
console.log("exercise 4...ok!")
So when the value comes into the ex4 which we wrote, first we will get "Maybe(4)";
Because Maybe(4) is inside a Functor, then we need to call map() on it to get value;
[Javascript] Maybe Functor的更多相关文章
- [Javascript] Other functor
EventStream: You can use RxJS, BaconJS or any reactive programming lib you want: var id_s = map(func ...
- [Javascript] IO Functor
IO functor doesn't like Maybe(), Either() functors. Instead of get a value, it takes a function. API ...
- [Javascript] Either Functor
Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...
- [Javascript] Functor Basic Intro
Well, this stuff will be a little bit strange if you deal with it first time. Container Object: Just ...
- [Javascript] Functor law
Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g) ...
- 转:JavaScript函数式编程(三)
转:JavaScript函数式编程(三) 作者: Stark伟 这是完结篇了. 在第二篇文章里,我们介绍了 Maybe.Either.IO 等几种常见的 Functor,或许很多看完第二篇文章的人都会 ...
- 转: JavaScript函数式编程(二)
转: JavaScript函数式编程(二) 作者: Stark伟 上一篇文章里我们提到了纯函数的概念,所谓的纯函数就是,对于相同的输入,永远会得到相同的输出,而且没有任何可观察的副作用,也不依赖外部环 ...
- 转:JavaScript函数式编程(一)
转:JavaScript函数式编程(一) 一.引言 说到函数式编程,大家可能第一印象都是学院派的那些晦涩难懂的代码,充满了一大堆抽象的不知所云的符号,似乎只有大学里的计算机教授才会使用这些东西.在曾经 ...
- javascript函数式编程(一)
一.引言 javascript函数式编程在最近两年来频繁的出现在大众的视野,越来越多的框架(react,angular,vue等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
随机推荐
- 转载:fstream和ifstream详细用法
文件 I/O 在C++中比烤蛋糕简单多了.在这篇文章里,我会详细解释ASCII和二进制文件的输入输出的每个细节,值得注意的是,所有这些都是用C++完成的. 一.ASCII 输出 为了使用下面的方法, ...
- hdu1792 水题
最近转到vim上来了,用vim编写代码,用gcc编译,用gdb调试.这是用vim做的第一道题,纪念下.题目很水,就不说了. /* * Author : ben */ #include <cstd ...
- 《Python 学习手册4th》 第六章 动态类型简介
''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书)“重点笔 ...
- PHP 系统命令函数
function execute($cmd) { $res = ''; if ($cmd) { if(function_exists('system')) { @ob_start(); @system ...
- bzoj 2751 [HAOI2012]容易题(easy)(数学)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2751 [题意] m个位置,已知每个位置的可能取值,问所有可能情况的每个位置的乘积的和. ...
- sgu 176 Flow construction(有源汇的上下界最小流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...
- sys.check_constraints
每个用作 CHECK 约束(sys.objects.type = C)的对象都在表中占一行. SELECT name FROM sys.check_constraints -- equal to SE ...
- JavaFx版本植物大战僵尸
http://www.cnblogs.com/lslvxy/archive/2013/04/17/3026711.html —————————————————————————————————————— ...
- New full duplex HTTP tunnel implementation (client and server)
https://issues.jboss.org/browse/NETTY-246?page=com.atlassian.jirafisheyeplugin:fisheye-issuepanel —— ...
- ocp 1Z0-051 141-175题解析
141. View the Exhibitand examine the structure of CUSTOMERS and GRADES tables. You need to displayna ...