[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由此被称 ...
随机推荐
- java 使用相对路径读取文件
java 使用相对路径读取文件 1.java project环境,使用java.io用相对路径读取文件的例子: *目录结构: DecisionTree |___src ...
- Android智能聊天机器人
http://www.tuling123.com/ 注册一个账号,申请一个KEY值.此网站也有文档,可以查看. package com.tulingdemo; import java.te ...
- hdu1722 bjfu1258 辗转相除法
这题就是个公式,代码极简单.但我想,真正明白这题原理的人并不多.很多人只是随便网上一搜,找到公式a了就行,其实这样对自己几乎没有提高. 鉴于网上关于这题的解题报告中几乎没有讲解原理的,我就多说几句,也 ...
- 基本输入输出系统BIOS---键盘输入
基本输入输出系统BIOS概述 硬盘操作系统DOS建立在BIOS的基础上,通过BIOS操纵硬件,例如DOS调用BIOS显示I/O程序完成输入显示,调用打印I/O完成打印输出 通常应用程序应该调用DOS提 ...
- 【原】Mongodb相关资料
Mongodb与关系型数据库对比 Mongodb与关系型数据库对比 由于之前主要接触的是关系型数据库,所以主要将Mongodb与关系型数据库进行对比:主要从术语.Server与Client.数据定义语 ...
- 博客测试:博客系统i94web beta1.0 请求测试
最近博客没怎么更新了,因为一直在撸代码,自己写了一个小小的博客系统:i94web,匆忙发布beta1.0,请求各位测试各种漏洞. 先看几张截图. 首页: 边栏: 文章页: 后台发布: 测试地址:htt ...
- InputFormat,OutputFormat,InputSplit,RecordRead(一些常见面试题),使用yum安装64位Mysql
列举出hadoop常用的一些InputFormat InputFormat是用来对我们的输入数据进行格式化的.TextInputFormat是默认的. InputFormat有哪些类型? DBInpu ...
- ehcache 的配置
配置:一.在src目录下加入ehcache.xml: <cache name="SimplePageCachingFilter" maxElementsInMemory=&q ...
- javascript !!作用
javaScript中使用!!表示取得boolean值,具体作用如下 var value= !!test[1]; 取变量的Boolean值, 相当于 var value = test[1]?true: ...
- easily add files to META-INF in NetBeans
http://georgeinfo.blog.163.com/blog/static/16368334120101019104044650/ ————————————————————————————— ...