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的更多相关文章

  1. [Javascript] Other functor

    EventStream: You can use RxJS, BaconJS or any reactive programming lib you want: var id_s = map(func ...

  2. [Javascript] IO Functor

    IO functor doesn't like Maybe(), Either() functors. Instead of get a value, it takes a function. API ...

  3. [Javascript] Either Functor

    Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...

  4. [Javascript] Functor Basic Intro

    Well, this stuff will be a little bit strange if you deal with it first time. Container Object: Just ...

  5. [Javascript] Functor law

    Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g) ...

  6. 转:JavaScript函数式编程(三)

    转:JavaScript函数式编程(三) 作者: Stark伟 这是完结篇了. 在第二篇文章里,我们介绍了 Maybe.Either.IO 等几种常见的 Functor,或许很多看完第二篇文章的人都会 ...

  7. 转: JavaScript函数式编程(二)

    转: JavaScript函数式编程(二) 作者: Stark伟 上一篇文章里我们提到了纯函数的概念,所谓的纯函数就是,对于相同的输入,永远会得到相同的输出,而且没有任何可观察的副作用,也不依赖外部环 ...

  8. 转:JavaScript函数式编程(一)

    转:JavaScript函数式编程(一) 一.引言 说到函数式编程,大家可能第一印象都是学院派的那些晦涩难懂的代码,充满了一大堆抽象的不知所云的符号,似乎只有大学里的计算机教授才会使用这些东西.在曾经 ...

  9. javascript函数式编程(一)

    一.引言 javascript函数式编程在最近两年来频繁的出现在大众的视野,越来越多的框架(react,angular,vue等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...

随机推荐

  1. tools/adb: No such file or directory

    运行adb出现这种错误: bash: ./adb: No such file or directory   但adb确实存在.那说明你用的是64位的Linux,没装32位运行时库,安装 $ sudo  ...

  2. The Bookcase

    题意: 有n本宽w高h的书,向三层书架上放,每层不能为空,求占用的整体的最小面积(总高度*三层中最宽的) 分析: 不太好想,dp[i][j]表示第一层宽度为i第二层为j放的最小高度 dp[i][j]= ...

  3. HDU-1438 钥匙计数之一

    http://acm.hdu.edu.cn/showproblem.php?pid=1438                                钥匙计数之一 Time Limit: 200 ...

  4. 《Python 学习手册4th》 第十七章 作用域

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  5. linux 常用命令基础

    linux常用的命令 shell 是命令语句,命令解释程序以及程序设计语言的统称,它不仅仅拥有自己内建的shell命令集,同时也能被系统中其他应用程序所调用 shell 的一个重要特性是它本身就是一个 ...

  6. 纯CSS制作冒泡提示框

    来源:http://www.ido321.com/1214.html 前两天翻译了一篇文章,关于利用css的border属性制作基本图形:http://www.ido321.com/1200.html ...

  7. 自学hadoop(三)

    1) 关于hadoop在eclipse插件.经过自己的摸爬滚打.总结一下三条.     a) 2.0或者0.23.0吧 google比较方便.其他的可以自己编译.(这个我不敢保证.我本地环境事2.1. ...

  8. java集合框架复习

    数组类Array是java中最基本的一个存储结构,它用于存储 一组连续的对象或一组类型相同的基本类型的数据. Array特点:效率高,但容量固定且无法动态改变, 缺点:无法判断其中存有多少元素,len ...

  9. MLE MAP EM

    1.最大似然估计 (MLE):  什么是最大似然估计?     问题:给定一组观察数据还有一个参数待定的模型,如何来估计这个未知参数呢? 观察数据(x1,y1)......(xn,yn)   待定模型 ...

  10. Java基础题

    问题: 1.请对比一下重载和重写的区别. 2.请对比一下接口和抽象类的异同. 3.写出一个单例模式,并说明其优点. 4.用过String.StringBuffer吗,说出他们的异同. 5.什么是值传递 ...