[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等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
随机推荐
- HDU 5875 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- 1047图的深度优先遍历c语言
描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...
- codeforces 696C PLEASE 概率dp+公式递推+费马小定理
题意:有3个杯子,排放一行,刚开始钥匙在中间的杯子,每次操作,将左右两边任意一个杯子进行交换,问n次操作后钥匙在中间杯子的概率 分析:考虑动态规划做法,dp[i]代表i次操作后的,钥匙在中间的概率,由 ...
- codeforces 679A Bear and Prime 100 交互
第一次交互题,记录一下吧 #include <cstdio> #include <iostream> #include <ctime> #include <v ...
- DbContext运行时动态附加上一个dbset
参考 Creating DbSet Properties Dynamically C# code? 1 DbSet<MyEntity> set = context.Set<MyEnt ...
- div模拟的下拉框特效jquery
从网上找来的,感觉不错就拿来分享下 <style type="text/css"> body, ul, li { margin: 0; padding: 0; font ...
- 通过gdb跟踪进程调度分析进程切换的过程
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本实验目的:通过gdb在lin ...
- android Loger日志类(获取内置sd卡)
Android手机自带内部存储路径的获取 原文地址:http://my.oschina.net/liucundong/blog/288183 直接贴代码: public static String g ...
- 写i2c_client驱动的两种方式
http://www.cnblogs.com/simonshi/archive/2011/02/24/1963426.html
- 《GettingThingsDone》--GTD学习笔记(二)-GTD实践指导
一.准备阶段的建议: 高级的工作管理方法就是学会一套系统并付诸实施,直到将这套系统和方法融入你的工作和生活中. 通过行动使自己感觉良好,要比通过使自己感觉良好而进入一种较佳的行动状态容易的多. 当你在 ...