[Javascript] Other functor
EventStream:
You can use RxJS, BaconJS or any reactive programming lib you want:
var id_s = map(function(e) { return '#'+e.id }, Bacon.fromEventTarget(document, "click"))
var element_s = map(document.querySelector, id_s)
//=> EventStream(Element) element_s.onValue(function(el) { alert('The inner html is ' +el.innerHTML) })
Here using BaconJS; we need to call onValue to subscribe stream.
Future:
It is "lazy", you must fork it.
var makeHtml = function(post){ return "<div>"+post.title+"</div>"};
var page_f = map(makeHtml, http.get('/posts/2')) page_f.fork(function(err) { throw(err) },
function(page){ $('#container').html(page) })
console.clear();
var _ = R;
var P = PointFree;
var map = P.fmap;
var compose = P.compose;
var Maybe = P.Maybe;
var Identity = P.Id; // Exercise 1
// ==========
// Use _.add(x,y) and map(f,x) to make a function that increments a value inside a functor
console.log("--------Start exercise 1--------") var ex1 = map(_.add()) assertDeepEqual(Identity(), ex1(Identity()))
console.log("exercise 1...ok!") // Exercise 2
// ==========
// Use _.head to get the first element of the list
var xs = Identity(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
console.log("--------Start exercise 2--------") var ex2 = map(_.head); assertDeepEqual(Identity('do'), ex2(xs))
console.log("exercise 2...ok!") // Exercise 3
// ==========
// Use safeGet and _.head to find the first initial of the user
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!") // Exercise 4
// ==========
// Use Maybe to rewrite ex4 without an if statement
console.log("--------Start exercise 4--------") var ex4 = function(n) {
if(n){
return parseInt(n);
}
} var ex4 = compose(map(parseInt),Maybe) assertDeepEqual(Maybe(), ex4(""))
console.log("exercise 4...ok!") // TEST HELPERS
// =====================
function inspectIt(x){
return (x.inspect && x.inspect()) || (x.toString && x.toString()) || x.valueOf(); //hacky for teachy.
} function assertEqual(x,y){
if(x !== y){ throw("expected "+x+" to equal "+y); }
}
function assertDeepEqual(x,y){
if(x.val !== y.val) throw("expected "+inspectIt(x)+" to equal "+inspectIt(y));
}
[Javascript] Other functor的更多相关文章
- [Javascript] Maybe Functor
In normal Javascript, we do undefine check or null check: , name: "Suvi"}; var name = pers ...
- [Javascript] IO Functor
IO functor doesn't like Maybe(), Either() functors. Instead of get a value, it takes a function. API ...
- [Javascript] Either Functor
Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...
- [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等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
随机推荐
- UNDO表空间设置
flashback query和flashback table都是以用UNDO表空间的内容来进行恢复数据 查看undo内容保存的时间: SQL> show parameter undo_re N ...
- mybatis系列-05-SqlMapConfig.xml详解
mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases(类型别名) typeHandler ...
- 三、python高级特性(切片、迭代、列表生成器、生成器)
1.python高级特性 1.1切片 list列表 L=['Mli','add','sal','saoo','Lkkl'] L[0:3] #即为['Mli','add','sal'] 从索引0开始 ...
- Navicat 远程连接SQL Server 2014 Express 报08001错误
场景:Navicat 远程连接SQL Server 2014 Express 报08001错误,经查验防火墙端口1434,1433已经打开 过程:1. 一开始觉得是连接名称问题,使用IP地址或者主机名 ...
- [AngularJS学习笔记] 基础学习01
2016-06-06开始学习AngularJS AngularJS是会extend HTML的 ng-directives 先学习了四个 ng-app:定义AngularJS Application的 ...
- 详解 jupyter notebook 集成 spark 环境安装
来自: 代码大湿 代码大湿 1 相关介绍 jupyter notebook是一个Web应用程序,允许你创建和分享,包含活的代码,方程的文件,可视化和解释性文字.用途包括:数据的清洗和转换.数值模拟.统 ...
- Linux服务器集群系统(一)(转)
add by zhj:虽然是2002年的文章,但读来还是收益良多.在 章文嵩:谈LVS及阿里开源背后的精彩故事 中LVS发起人及主要贡献者谈了LVS的开发过程及阿里开源的一些故事 原文:http:// ...
- flask中的request.form对象方法
'add','clear','copy','deepcopy','fromkeys','get','gtlist','has_key','items','iteritems','iterkeys',' ...
- AngularJS 学习笔记二
AngularJS指令 指令 描述 讲解 ng_app 定义应用程序的根元素. 指令 ng_bind 绑定 HTML 元素到应用程序数据. 简介 ng_click 定义元素被单击时的行为. HTML ...
- 亲和串(HDU2203)
http://acm.hdu.edu.cn/showproblem.php?pid=2203 题目意思很简单,求s1串所构成的环中是否有s2这个串 用CMP参考http://s.acmore.net/ ...