[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等)标榜自己使用了函数式编程的特性,好像一旦跟函数式编程沾边,就很高大上一 ...
随机推荐
- bjfu1250 模拟
这题貌似是蓝桥杯的一题改了个题面. 就是模拟啦,应该有比我的更简洁的方法. 我的方法是把所有的人(蚂蚁)按位置排完序以后从左往右看,每次有一个向左走的,就会把最左边的t出,这个变成向右中,同时,从左端 ...
- IOS 本地通知UILocalNotification
//发送通知 UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notificati ...
- Linux--使用expect进行自动交互
在linux下进行一些操作时,有时需要与机器进行一些交互操作,比如切换账号时输入账号密码,传输文件时输入账号密码登陆远程机器等,但有时候这些动作需要在shell脚本中进行,这个时候就可以使用expec ...
- 【原】Mongodb相关资料
Mongodb与关系型数据库对比 Mongodb与关系型数据库对比 由于之前主要接触的是关系型数据库,所以主要将Mongodb与关系型数据库进行对比:主要从术语.Server与Client.数据定义语 ...
- MVC和WebForm的优缺点对比
1 WebForm优点 1)支持事件模型开发,得益于丰富的服务端组件,WebForm开发可以迅速的搭建Web应用 2)使用方便,入门容易 3)控件丰富的WebForm 2 WebForm缺点 1)封 ...
- 产品设计敏感度之kindle touch 4
这种训练方法来自于<嵌入式系统开发之道:菜鸟成长日志与项目经理的私房菜>,名字真够长的,但是里面都是干货,我虽然之前有短评这本书,但是后面看完之后会继续再做一个详细的感悟记录. 选出身边的 ...
- Google Appengine参考路径
1.Hello, World! in 5 minutes 2.Creating a Guestbook -Introduction 3.Sample Applications 1.Programmin ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- mac 添加安卓设备的支持
1. 把android设备插到mac电脑上 2. 首先可以看一下之前在该mac电脑上有没有添加过这个 , 命令 : adb devices 如果显示出,下面字样,说明之前添加过了,下面就可以 ...
- html学习笔记之position
今天主要一直看试验position的各种属性,现在记录下来以此备忘. position有四种常有属性,分别是static,fixed.absolute,relative fixed就是相对于窗口的位置 ...