[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等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
随机推荐
- POJ 1064 Cable master
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37865 Accepted: 8051 Des ...
- C++ static内容小结
C++中static总结比较好的博客:http://blog.csdn.net/laixingjun/article/details/9139839 http://blog.csdn.net/xiaj ...
- ARM处理机模式--内部寄存器
处理器模式 用户模式(user)简称usr 快速中断模式(FIQ)简称fiq 外部中断模式(IRQ)简称irq 特权模式(supervisor)简称sve 数据访问终止模式(abort)简称abt 未 ...
- bzoj 2134 单选错位(期望)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2134 [题意] ai与ai+1相等得1分,求期望. [思路] 每个题的期望都是独立的. ...
- JAVA使用jdbc连接MYSQL简单示例
以下展示的为JAVA使用jdbc连接MYSQL简单示例: import java.sql.DriverManager; import java.sql.ResultSet; import java.s ...
- COS回应7大质疑
Apple过于封闭,没啥朋友,这家伙应该比较高傲,曾和Intel,IBM and so on..一起玩过!Google过于开放,狐朋狗友,友人泛滥,殃及ecosystem,弊端已显,祸水将至.COS奉 ...
- Java处理InterruptedException异常小结
对于InterruptedException,一种常见的处理方式是捕捉它,然后什么也不做(或者记录下它,不过这也好不到哪去).不幸的是,这种方法忽略了这样一个事实:这期间可能发生中断,而中断可能导致应 ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- 使用PUT方法上传文件无法工作原因分析
现象 在Spring Framework中,使用HTTP的PUT方法上传文件时,在服务器端发现Multipart参数为空. 原因 Spring中的StandardServletMultipartRes ...
- Unity3D之移植学习笔记:移植到Android平台
首先,我们需要一台已经配置好可以开发Android应用的计算机,这里我使用的是Windows系统+Eclipse+ADT的开发环境,当然也可以使用Android Studio或者使用Mac系统都可以. ...