[Javascript] IO Functor
IO functor doesn't like Maybe(), Either() functors. Instead of get a value, it takes a function.
API:
.toIO() // conver a function to IO
IO() // The same a to toIO; but this is a just static method
runIO //IO is lazy, just like Observable, if you don't run it, it has no side effect
Examples:
/*
Cover to IO
*/ var email_io = IO(function(){ return $("#email").val() } var getValue = function(sel){ return $(sel).val() }.toIO() /*
Example runIO
*/
var email_io = IO(function(){ return $("#email").val() })
var msg_io = map(concat("welcome "), email_io) runIO(msg_io)
//=> ”welcome steve@foodie.net”
// Exercise 4
// ==========
// Get the text from the input and strip the spaces
console.log("--------Start exercise 4--------") var getValue = function(x){ return document.querySelector(x).value }.toIO()
var stripSpaces = function(s){ return s.replace(/\s+/g, ''); } var ex4 = compose(map(stripSpaces), getValue) assertEqual("honkeytonk", runIO(ex4('#text')))
console.log("exercise 4...ok!") // Exercise 5
// ==========
// Use getHref() / getProtocal() and runIO() to get the protocal of the page.
var getHref = function(){ return location.href; }.toIO();
var getProtocal = compose(_.head, _.split('/'))
var ex5 = compose(map(getProtocal), getHref) console.log("--------Start exercise 5--------")
assertEqual('http:', runIO(ex5("http://www.google.fi")))
console.log("exercise 5...ok!") // Exercise 6*
// ==========
// Write a function that returns the Maybe(email) of the User from getCache().
// Don't forget to JSON.parse once it's pulled from the cache
//so you can _.get() the email // setup...
localStorage.user = JSON.stringify({email: "george@foreman.net"}) var log = function(x){
console.log(x.toString());
return x;
} var getCache = function(x){ return Maybe(localStorage[x]); }.toIO();
var getEmail = compose(_.get('email'), JSON.parse);
var ex6 = compose(map(map(getEmail)), getCache); // one map for Maybe, one map for IO assertDeepEqual(Maybe("george@foreman.net"), runIO(ex6('user')))
console.log("exercise 6...ok!")
[Javascript] IO Functor的更多相关文章
- [Functional Programming] Async IO Functor
We will see a peculiar example of a pure function. This function contained a side-effect, but we dub ...
- [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] Either Functor
Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...
- 超越Web,Javascript在物联网的应用
引子 Patrick Catanzariti 是一名Web开发project师,近期他在 sitepoint 发表了<JavaScript Beyond the Web in 2014>, ...
- python的简介与简单使用
简介: python的诞生 创始人:Guido van Rossum(荷兰人:吉多) 时间:1989年 特点: 1.简单明了,容易上手 2.多平台,`全面发展,平衡发展, 3.胶水语言,有大量的库 其 ...
- day112:MoFang:种植园使用websocket代替http&服务端基于flask-socketio提供服务&服务端响应信息&种植园页面显示初始化
目录 1.种植园使用websocket代替http 2.服务端基于socket提供服务 3.服务端响应信息 4.种植园页面展示 1.种植园使用websocket代替http 我们需要完成的种植园,是一 ...
- VS2015编译GEOS
下载链接:http://trac.osgeo.org/geos/ 1. 打开cmake,加载geos源码和定位geos的工程存放位置: 2.点击configure,会报错,首先设置CMAKE_INST ...
- 泛函编程(33)-泛函IO:Free Functor - Coyoneda
在前几期讨论中我们终于推导出了Free Monad.这是一个Monad工厂,它可以把任何F[A]变成Monad.可惜的是它对F[A]是有所要求的:F必须是个Functor.Free Monad由此被称 ...
随机推荐
- CF 577C Vasya and Petya's Game
题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的 ...
- [转] C# 中的static静态变量
logitechyan原文关于C#中static静态变量 C#静态变量使用static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被 ...
- Linux Kernel 4.8分支第4个候选版本发布
导读 今天,大神Linus Torvalds宣布了Linux 4.8分支的第四个候选版本,该候选版本在提供常规驱动更新.架构改善和部分KVM调整之外最大的新功能就是修复了英特尔Skylake电源管理B ...
- “自私”的Linux
导读 “如果当时我真的知道从头建立一个操作系统的难度,肯定是不会有勇气去做的.”1991年8月25日,随着林纳斯·托瓦兹(Linus Torvalds)这句“天真”的描述,Linux系统正式与世人见面 ...
- DevExpress z
1.TextEditor(barEditItem)取文本 string editValue = barEditItem1.EditValue.ToString(); //错误,返回null st ...
- LightOJ 1427 -Repository(ac自动机)
题意: 求每个模式串在母串中出现的次数 #include <map> #include <set> #include <list> #include <cma ...
- OpenGL超级宝典第5版&&基础渲染
1.OpenGL查询拓展机制是否被支持 gltools函数库: int gltIsExtSupported(const char *extension) { #ifndef OPENGL_ES GLi ...
- 使用Qmake在树莓派上开发Opencv程序
Qt 安装 PC 端 下载安装即可 https://mirrors.ustc.edu.cn/qtproject/official_releases/qt 树莓派:Qt开发套件和opencv安装sud ...
- leetcode@ [343] Integer Break (Math & Dynamic Programming)
https://leetcode.com/problems/integer-break/ Given a positive integer n, break it into the sum of at ...
- Apache Spark Streaming的简介
Spark Streaming通过将流数据按指定时间片累积为RDD,然后将每个RDD进行批处理,进而实现大规模的流数据处理.其吞吐量能够超越现有主流流处理框架Storm,并提供丰富的API用于流数据计 ...