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. CXF之七 传输文件

    CXF的文件传输通过MTOM实现.MTOM(SOAP Message Transmission Optimization Mechanism)SOAP消息传输优化机制,可以在SOAP消息中发送二进制数 ...

  2. C# Adding Hyperlink to Windows Form z

    When creating a Windows form in C#, we would like to create a hyperlink so that when the user click ...

  3. WinForm使用webbrowser爬取数据,中文乱码问题

    使用winform中的webbrowser抓取某个网站的数据时,遇到中文乱码的问题: 当时使用的获取网页内容的代码为: webBrowser1.DocumentText.ToString(); 不管我 ...

  4. rfid 门卡系统和人体红外感应开发

    今天忙了一天了,因为毕昇杯我发现如果不加把劲,可能寒假之前代码搞不出了,今天突击了两个模块,一个人体感应模块,和rfid刷卡模块,这两个模块谈不上自己编写代码,今天的任务也仅仅是看懂了代码,现在我总结 ...

  5. CSS计算样式的获取

    一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...

  6. bzoj 3365 [Usaco2004 Feb]Distance Statistics 路程统计(点分治,单调)

    [题意] 求树上长度不超过k的点对数目. [思路] 和 Tree 一样一样的. 就是最后统计的时候别忘把根加上. [代码] #include<set> #include<cmath& ...

  7. 直线相交 POJ 1269

    // 直线相交 POJ 1269 // #include <bits/stdc++.h> #include <iostream> #include <cstdio> ...

  8. 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 ...

  9. PHP网站简单架构 – 单独跑php-fpm

    这个架构比较简单,不做过多的说明 前端1台Nginx:负载均衡+nfs 中间2台php:php-fpm 后端1台数据库:MySQL 安装略,参考<lnmp最新源码一键安装包> 192.16 ...

  10. Struts2运行流程分析

    一.Struts2运行流程图: 二.运行流程分析: 1. 请求发送给StrutsPrepareAndExecuteFilter 2.StrutsPrepareAndExecuteFilter询问Act ...