[Javascript] Either Functor
Either Functor:
// API Right(val) // resolve the value Left(val) // return error message
Examples:
map(function(x) { return x + ; }, Right())
//=> Right(3)
map(function(x) { return x + ; }, Left(‘some message))
//=> Left(‘some message’)
var determineAge = function(user){
return user.age ? Right(user.age) : Left("couldn’t get age");
}
var yearOlder = compose(map(add()), determineAge)
yearOlder({age: })
//=> Right(23)
yearOlder({age: null})
//=> Left("couldn’t get age")
var _ = R;
var P = PointFree;
var map = P.fmap;
var compose = P.compose;
var Maybe = P.Maybe;
var Identity = P.Id; var Either = folktale.data.Either;
var Left = Either.Left;
var Right = Either.Right; // Exercise 1
// ==========
// Write a function that uses checkActive() and showWelcome() to grant access or return the error
console.log("--------Start exercise 1--------") var showWelcome = compose(_.add( "Welcome "), _.get('name')) var checkActive = function(user) {
return user.active ? Right(user) : Left('Your account is not active')
} var ex1 = compose(map(showWelcome), checkActive); assertDeepEqual(Left('Your account is not active'), ex1({active: false, name: 'Gary'}))
assertDeepEqual(Right('Welcome Theresa'), ex1({active: true, name: 'Theresa'}))
console.log("exercise 1...ok!") // Exercise 2
// ==========
// Write a validation function that checks for a length > 3. It should return Right(x) if it is greater than 3 and Left("You need > 3") otherwise
console.log("--------Start exercise 2--------") var ex2 = function(x) {
if(x){
return x.length > ?
Right(x) :
Left("You need > 3");
}else{
return Left("You need > 3");
}
} assertDeepEqual(Right("fpguy99"), ex2("fpguy99"))
assertDeepEqual(Left("You need > 3"), ex2("..."))
console.log("exercise 2...ok!") // Exercise 3
// ==========
// Use ex2 above and Either as a functor to save the user if they are valid var save = function(x){ console.log("SAVED USER!"); return x; } var ex3 = compose( map(save), ex2) console.log("--------Start exercise 3--------")
assertDeepEqual(Right("fpguy99"), ex3("fpguy99"))
assertDeepEqual(Left("You need > 3"), ex3("duh"))
console.log("exercise 3...ok!")
[Javascript] Either Functor的更多相关文章
- [Javascript] Maybe Functor
In normal Javascript, we do undefine check or null check: , name: "Suvi"}; var name = pers ...
- [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] 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等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
随机推荐
- 构建属于自己的ORM框架之二--IQueryable的奥秘
上篇文章标题乱起,被吐槽了,这次学乖了. 上篇文章中介绍了如何解析Expression生成对应的SQL语句,以及IQueryable的一些概念,以及我们所搭建的框架的思想等.但还没把它们结合并应用起来 ...
- UVA 10054 The Necklace
完全就是哭瞎的节奏···QAQ 又是图论··· 题意:有一种项链,每个珠子上有两种颜色,相同颜色的两颗珠子的两头相连,如果能连成环输出珠子的顺序,不能连成环输出"some beads may ...
- kettle连接hadoop&hdfs图文详解
1 引言: 项目最近要引入大数据技术,使用其处理加工日上网话单数据,需要kettle把源系统的文本数据load到hadoop环境中 2 准备工作: 1 首先 要了解支持hadoop的Kettle版本情 ...
- Windows 窗体最小化和隐藏的区别及恢复
应用程序有托盘图标,窗体最小化或者隐藏窗体时,点击托盘图标需要恢复窗体显示,并且恢复之前的现实状体,正常大小或者最大化显示.例如:最大化显示窗体时,点击最小化按钮,窗体最小化,点击托盘图标将最大化显示 ...
- ubuntu下安装selenium2.0 环境
参考:http://www.cnblogs.com/fnng/archive/2013/05/29/3106515.html ubuntu 安装过程: 1.安装:setuptools $ apt-ge ...
- HDU 5778 abs (BestCoder Round #85 C)素数筛+暴力
分析:y是一个无平方因子数的平方,所以可以从sqrt(x)向上向下枚举找到第一个无平方因子比较大小 大家可能觉得这样找过去暴力,但实际上无平方因子的分布式非常密集的,相关题目,可以参考 CDOJ:无平 ...
- WiFi Test Entity
项目要结了,总结下每次新版本WiFi需要测试的内容 1 IOT1.1 connect target[Connected]; modify wifi pwd to wrong pwd[Au ...
- 在这个年纪,打DOTA冲分有那么可笑么?
大学四年,毕业两年,6年dota,总是在最无助的时候让心灵得到一丝安宁的东西,烟和dota. 我不知道dota对别人的意义是什么,一盘dota,在最多不超过1个半小时的时间里,仿佛经历了一个小人生,每 ...
- CSS计算样式的获取
一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...
- Python的列表排序
Python的列表排序 本文为转载,源地址为:http://blog.csdn.net/horin153/article/details/7076321 在 Python 中, 当需要对一个 list ...