[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 [email protected]”
// 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: "[email protected]"})

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("[email protected]"), runIO(ex6(‘user‘)))
console.log("exercise 6...ok!")
时间: 2024-10-05 21:08:17

[Javascript] IO Functor的相关文章

[Javascript] Maybe Functor

In normal Javascript, we do undefine check or null check: var person = {age: 14, name: "Suvi"}; var name = person.name ? person.name: null; Sometime backend data return may contain or not contain 'name' prop. So let's see how to define a Maybe()

[Javascript] Either Functor

Either Functor: // API Right(val) // resolve the value Left(val) // return error message Examples: map(function(x) { return x + 1; }, Right(2)) //=> Right(3) map(function(x) { return x + 1; }, Left(‘some message)) //=> Left(‘some message’) var deter

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

?? 引子 Patrick Catanzariti 是一名Web开发工程师,最近他在 sitepoint 发表了<JavaScript Beyond the Web in 2014>,介绍了JavaScript在物联网中的应用,非常有意思.做为JavaScript的爱好者和从业者,我在这里把它翻译了,以飨读者. 顺便说一下,就在上周,我们团队的最新力作"真正的JavaScript控件集"----<新一代JavaScript控件Wijmo 5正式发布>啦. 前言

编程语言的发展脉络图

做了两件事情:第一件是从Wikipedia网站上搜集各种编程语言的资料,第二件是把编程语言之间相互影响的关系用图的形式画出来,从而得到了一张编程语言的发展脉络图. 这两件事情主要是编写Ruby程序来做的,在Wikipedia页面的右上角有一个表格,从中提取Appeared in,Influenced by,Influenced等字段,根据这种关联关系遍历,采集到292种语言,用JSON格式保存到文件中,然后把几个出现年份描述不精确的语言用人工查证的方式进行补充. [["Swift",&

【开源夏令营优秀开题报告】前端与移动合集

CSDN开源夏令活动已经正式进入第一实习阶段,我们遴选出部分优秀提案开题报告进行展示.本文是前端与移动类开题报告展示. 编者按:CSDN开源夏令活动,已经正式进入第一实习阶段,我们遴选出了部分提案的优秀开题报告进行展示.优秀开题报告作者将得到CSDN高校俱乐部发出的"2014开源夏令营荣誉证书"及纪念品一份. 提案 1:基于AngularJS的Docker Dashboard UI 提案简介:使用AngularJS.Bootstrap和D3.js实现Docker Dockboard U

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

在前几期讨论中我们终于推导出了Free Monad.这是一个Monad工厂,它可以把任何F[A]变成Monad.可惜的是它对F[A]是有所要求的:F必须是个Functor.Free Monad由此被称为由Functor F 产生的Monad.F必须是Functor,这个门槛使我们在使用Free Monad时很不方便.举个前面讨论过的例子: 1 trait Console[A] 2 case object GetLine extends Console[String] 3 case class P

Javascript的io操作

一.功能实现核心:FileSystemObject 对象 要在javascript中实现文件操作功能,主要就是依靠FileSystemobject对象. 二.FileSystemObject编程 使用FileSystemObject 对象进行编程很简单,一般要经过如下的步骤: 创建FileSystemObject对象.应用相关方法.访问对象相关属性 . (一)创建FileSystemObject对象 创建FileSystemObject对象的代码只要1行: var fso = new Activ

[Javascript] Functor law

Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g)) compose( map(toUpper), map(reverse), toArray )("bingo"); compose( map( compose(toUpper reverse), toArray ) )("bingo") Natural Transfo

[Javascript] Functor Basic Intro

Well, this stuff will be a little bit strange if you deal with it first time. Container Object: Just a wrapper / contianer for values No Method No Nouns var _Container = function(val){ this.val = val; } var Container = function(x){ return new _Contai