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的更多相关文章

  1. [Functional Programming] Async IO Functor

    We will see a peculiar example of a pure function. This function contained a side-effect, but we dub ...

  2. [Javascript] Maybe Functor

    In normal Javascript, we do undefine check or null check: , name: "Suvi"}; var name = pers ...

  3. [Javascript] Other functor

    EventStream: You can use RxJS, BaconJS or any reactive programming lib you want: var id_s = map(func ...

  4. [Javascript] Either Functor

    Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: m ...

  5. 超越Web,Javascript在物联网的应用

    引子 Patrick Catanzariti 是一名Web开发project师,近期他在 sitepoint 发表了<JavaScript Beyond the Web in 2014>, ...

  6. python的简介与简单使用

    简介: python的诞生 创始人:Guido van Rossum(荷兰人:吉多) 时间:1989年 特点: 1.简单明了,容易上手 2.多平台,`全面发展,平衡发展, 3.胶水语言,有大量的库 其 ...

  7. day112:MoFang:种植园使用websocket代替http&服务端基于flask-socketio提供服务&服务端响应信息&种植园页面显示初始化

    目录 1.种植园使用websocket代替http 2.服务端基于socket提供服务 3.服务端响应信息 4.种植园页面展示 1.种植园使用websocket代替http 我们需要完成的种植园,是一 ...

  8. VS2015编译GEOS

    下载链接:http://trac.osgeo.org/geos/ 1. 打开cmake,加载geos源码和定位geos的工程存放位置: 2.点击configure,会报错,首先设置CMAKE_INST ...

  9. 泛函编程(33)-泛函IO:Free Functor - Coyoneda

    在前几期讨论中我们终于推导出了Free Monad.这是一个Monad工厂,它可以把任何F[A]变成Monad.可惜的是它对F[A]是有所要求的:F必须是个Functor.Free Monad由此被称 ...

随机推荐

  1. HDU 5835 Danganronpa

    Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  2. HDU 5744 Keep On Movin

    Keep On Movin Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  3. spoj 694(后缀数组)

    题意:求一个字符串的不重复子串的个数. 分析:对于下标为i的位置,能够产生的前缀子串个数为len-i(下标从0开始),对于与它字典序相邻的后缀产生的子串是重复的(就是他们的最长公共前缀),所以我们要减 ...

  4. Flash Player”又来了“!

    2012年,Adobe宣布缩小对Linux平台的支持,自Flash Player 11.2版本之后,只为Linux平台上的Flash Player提供安全更新和bug修复,而不再提供版本更新. 四年之 ...

  5. java web 学习八(HttpServletResponse对象2)

    一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,

  6. MATLAB常用数据类型的转换

    一直以来,在使用MATLAB进行运算的过程中,经常会用到对图像的各种处理,因此需要对图像进行数据转换,因此对经常用到的转换进行整理,方便查看. 1.uint8转化为double用来方便图像的运算: d ...

  7. 【转】Yahoo!团队:网站性能优化的35条黄金守则

    Yahoo!的 Exceptional Performance团队为改善 Web性能带来最佳实践.他们为此进行了一系列的实验.开发了各种工具.写了大量的文章和博客并在各种会议上参与探讨.最佳实践的核心 ...

  8. [Hive - Tutorial] Querying and Inserting Data 查询和插入数据

    Querying and Inserting Data Simple Query Partition Based Query Joins Aggregations Multi Table/File I ...

  9. nservicebus教程-目录

    表的内容 开始 坚持NServiceBus 扩展 每天 举办 管理和监控 发布订阅 长时间运行的流程 定制 版本控制 常见问题解答 样品 开始 概述 NServiceBus一步一步向导 架构原则 事务 ...

  10. Uva 208 - Firetruck

    [题目链接]http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&p ...