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. poj 4052(ac自动机)

    题意:自己百度吧!! 分析:就是通过它的fail指针来找出它的子串就行了,这题其实不难的.这好像还是金华邀请赛的题哦! 代码实现: #include<cstdio> #include< ...

  2. 在Ubuntu下卸载Apache

    卸载Apache 转自:http://blog.csdn.net/chmo2011/article/details/7026384 1. 删除apache 代码: $ sudo apt-get --p ...

  3. Java连接Oracle10g

    1.导入驱动包: a.找到oracle安装目录下的jdbc/lib中的文件classes12.jar: b.右击你创建的JAVA工程,找到Build path,选择Add External Archi ...

  4. 个人经验 - Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑

    Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑: 此坑出现的条件: 1.RelativeLayout布局的layout_heigh ...

  5. 常用SQL语句汇总整理

    1.SQL 插入语句得到自动生成的递增ID 值 insert into Table1(Name,des,num) values (''ltp'',''thisisbest'',10); select ...

  6. 瞬间从IT屌丝变大神——注释规则

    注释的主要规则如下: 公共组件和各栏目的维护者都需要在文件头部加上注释说明: /** *文件用途说明 *作者姓名 *联系方式*制作日期 **/ 大的模块注释方法: //======= //代码用途 / ...

  7. Lucene学习笔记: 五,Lucene搜索过程解析

    一.Lucene搜索过程总论 搜索的过程总的来说就是将词典及倒排表信息从索引中读出来,根据用户输入的查询语句合并倒排表,得到结果文档集并对文档进行打分的过程. 其可用如下图示: 总共包括以下几个过程: ...

  8. Windows10输入法的切换

    Alt+Shift            中⇒あ,あ⇒中 Shift                  中⇒英,英⇒中 Alt+Caps Lock    あ⇒カ,A⇒あ⇒カ Ctrl+Caps Loc ...

  9. CentOS6 root 用户 vi/vim 无法开启高亮

    编辑 /etc/profile.d/vim.sh if [ -n "$BASH_VERSION" -o -n "$KSH_VERSION" -o -n &quo ...

  10. MAC机常用快捷键整理表格

    MAC机常用快捷键整理表格 范围 快捷键 说明 图形   (Command 键)在某些 Apple 键盘上,此键也可能为标志()     Control (Control 键)     Alt Opt ...