[Javascript] Maybe Functor

In normal Javascript, we do undefine check or null check:

var person = {age: 14, 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: 2, 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(4), ex4("4"))
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;

时间: 2024-10-22 07:36:30

[Javascript] Maybe Functor的相关文章

[Javascript] Either Functor

Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: map(function(x) { return x + 1; }, Right(2)) //=> Right(3) map(function(x) { return x + 1; }, Left(‘some message)) //=> Left(‘some message’) var deter

[Javascript] IO Functor

IO functor doesn't like Maybe(), Either() functors. Instead of get a value, it takes a function. API: .toIO() // conver a function to IO IO() // The same a to toIO; but this is a just static method runIO //IO is lazy, just like Observable, if you don

[Javascript] Functor law

Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g)) compose( map(toUpper), map(reverse), toArray )("bingo"); compose( map( compose(toUpper reverse), toArray ) )("bingo") Natural Transfo

[Javascript] Functor Basic Intro

Well, this stuff will be a little bit strange if you deal with it first time. Container Object: Just a wrapper / contianer for values No Method No Nouns var _Container = function(val){ this.val = val; } var Container = function(x){ return new _Contai

JS函数式编程 3.1 Javascript的函数式库

?? Functional Programming in Javascript 主目录第三章 建立函数式编程环境 Javascript的函数式库 据说所有的函数式程序员都会写自己的函数库,函数式Javascript程序员也不例外. 随着如今开源代码分享平台如GitHab.Bower和NPM的涌现,对这些函数库进行分享.变得及补充变得越来越容易. 现在已经有很多Javascript的函数式变成苦,从小巧的工具集到庞大的模块库都有. 每一个库都宣扬着自己的函数式编程风格.从一本正经的数学风格到灵活松

JavaScript函数式编程(三)

JavaScript函数式编程(一) JavaScript函数式编程(二) 在第二篇文章里,我们介绍了 Maybe.Either.IO 等几种常见的 Functor,或许很多看完第二篇文章的人都会有疑惑: 『这些东西有什么卵用?』 事实上,如果只是为了学习编写函数式.副作用小的代码的话,看完第一篇文章就足够了.第二篇文章和这里的第三篇着重于的是一些函数式理论的实践,是的,这些很难(但并非不可能)应用到实际的生产中,因为很多轮子都已经造好了并且很好用了.比如现在在前端大规模使用的 Promise

Javascript中call的使用

call 方法应用于:Function 对象调用一个对象的一个方法,以另一个对象替换当前对象.call([thisObj[,arg1[, arg2[,   [,.argN]]]]])参数:thisObj 可选项.将被用作当前对象的对象. arg1, arg2, , argN 可选项.将被传递方法参数序列. 说明:call 方法可以用来代替另一个对象调用一个方法.call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象.如果没有提供 thisObj 参数,那么 G

你不知道的javascript 之 >>

?远大于符号 在数学公式中,">>"代表远大于符号,表示一个数远大于另一个数,如76>>3,-2>>-99等.庞加莱与波莱尔1901年首先使用了它,很快被数学界所接受,沿用至今. 折叠编辑本段右移运算符 在许多计算机编程语言(例如:C语言.C++语言.Java语言.JavaScript语言.Pascal语言等)中,">>"代表右移运算符,就相当于"shr".该运算符为双目运算符,结合方向为从左到右,

初识JavaScript

JavaScript简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在HTML(标准通用标记语言下的一个应用)网页上使用,用来给HTML网页增加动态功能.因为JavaScript兼容于ECMA标准,因此也称为ECMAScript. ECMAScript,描述了该语javascript组成言的语法和基本对象. 文档对象模型(DOM),描述处理网页内容的方法