IO functor doesn't like Maybe(), Either() functors. Instead of get a value, it takes a function.

API:

  1. .toIO() // conver a function to IO
  2. IO() // The same a to toIO; but this is a just static method
  3. runIO //IO is lazy, just like Observable, if you don't run it, it has no side effect

Examples:

  1. /*
  2. Cover to IO
  3. */
  4.  
  5. var email_io = IO(function(){ return $("#email").val() }
  6.  
  7. var getValue = function(sel){ return $(sel).val() }.toIO()
  8.  
  9. /*
  10. Example runIO
  11. */
  12. var email_io = IO(function(){ return $("#email").val() })
  13. var msg_io = map(concat("welcome "), email_io)
  14.  
  15. runIO(msg_io)
  16. //=> ”welcome steve@foodie.net”
  1. // Exercise 4
  2. // ==========
  3. // Get the text from the input and strip the spaces
  4. console.log("--------Start exercise 4--------")
  5.  
  6. var getValue = function(x){ return document.querySelector(x).value }.toIO()
  7. var stripSpaces = function(s){ return s.replace(/\s+/g, ''); }
  8.  
  9. var ex4 = compose(map(stripSpaces), getValue)
  10.  
  11. assertEqual("honkeytonk", runIO(ex4('#text')))
  12. console.log("exercise 4...ok!")
  13.  
  14. // Exercise 5
  15. // ==========
  16. // Use getHref() / getProtocal() and runIO() to get the protocal of the page.
  17. var getHref = function(){ return location.href; }.toIO();
  18. var getProtocal = compose(_.head, _.split('/'))
  19. var ex5 = compose(map(getProtocal), getHref)
  20.  
  21. console.log("--------Start exercise 5--------")
  22. assertEqual('http:', runIO(ex5("http://www.google.fi")))
  23. console.log("exercise 5...ok!")
  24.  
  25. // Exercise 6*
  26. // ==========
  27. // Write a function that returns the Maybe(email) of the User from getCache().
  28. // Don't forget to JSON.parse once it's pulled from the cache
  29. //so you can _.get() the email
  30.  
  31. // setup...
  32. localStorage.user = JSON.stringify({email: "george@foreman.net"})
  33.  
  34. var log = function(x){
  35. console.log(x.toString());
  36. return x;
  37. }
  38.  
  39. var getCache = function(x){ return Maybe(localStorage[x]); }.toIO();
  40. var getEmail = compose(_.get('email'), JSON.parse);
  41. var ex6 = compose(map(map(getEmail)), getCache); // one map for Maybe, one map for IO
  42.  
  43. assertDeepEqual(Maybe("george@foreman.net"), runIO(ex6('user')))
  44. 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. java 使用相对路径读取文件

    java 使用相对路径读取文件 1.java project环境,使用java.io用相对路径读取文件的例子: *目录结构:  DecisionTree            |___src      ...

  2. Android智能聊天机器人

    http://www.tuling123.com/       注册一个账号,申请一个KEY值.此网站也有文档,可以查看. package com.tulingdemo; import java.te ...

  3. hdu1722 bjfu1258 辗转相除法

    这题就是个公式,代码极简单.但我想,真正明白这题原理的人并不多.很多人只是随便网上一搜,找到公式a了就行,其实这样对自己几乎没有提高. 鉴于网上关于这题的解题报告中几乎没有讲解原理的,我就多说几句,也 ...

  4. 基本输入输出系统BIOS---键盘输入

    基本输入输出系统BIOS概述 硬盘操作系统DOS建立在BIOS的基础上,通过BIOS操纵硬件,例如DOS调用BIOS显示I/O程序完成输入显示,调用打印I/O完成打印输出 通常应用程序应该调用DOS提 ...

  5. 【原】Mongodb相关资料

    Mongodb与关系型数据库对比 Mongodb与关系型数据库对比 由于之前主要接触的是关系型数据库,所以主要将Mongodb与关系型数据库进行对比:主要从术语.Server与Client.数据定义语 ...

  6. 博客测试:博客系统i94web beta1.0 请求测试

    最近博客没怎么更新了,因为一直在撸代码,自己写了一个小小的博客系统:i94web,匆忙发布beta1.0,请求各位测试各种漏洞. 先看几张截图. 首页: 边栏: 文章页: 后台发布: 测试地址:htt ...

  7. InputFormat,OutputFormat,InputSplit,RecordRead(一些常见面试题),使用yum安装64位Mysql

    列举出hadoop常用的一些InputFormat InputFormat是用来对我们的输入数据进行格式化的.TextInputFormat是默认的. InputFormat有哪些类型? DBInpu ...

  8. ehcache 的配置

    配置:一.在src目录下加入ehcache.xml: <cache name="SimplePageCachingFilter" maxElementsInMemory=&q ...

  9. javascript !!作用

    javaScript中使用!!表示取得boolean值,具体作用如下 var value= !!test[1]; 取变量的Boolean值, 相当于 var value = test[1]?true: ...

  10. easily add files to META-INF in NetBeans

    http://georgeinfo.blog.163.com/blog/static/16368334120101019104044650/ ————————————————————————————— ...