[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 _Container(x);
}
console.log(Container()) // _Container( {val: 3})
Every time we use Container, it will just add the value to the Container object and assign value to prop val.
map function on Container:
This map is not the map what you think it is....
The map you think is the method on the Array.
Here the map is the one get into the Container, grep the value, then apply function on this value. The return value is still Container.
_Container.prototype.map = function(f){
return Container(f(this.val))
}
var res = Container("flamethrower").map(function(s){ return _.capitalize(s) })
console.log(res) //_Container( {val: "Flamethrower"} )
So in the example, map function goes into the Container, get the value "flamethrower", and apply the function '_.capitialize' to the value and return the new value to the Container.
Or you can write like this:
var capitalize = _.capitalize;
var res = Container("flamethrower").map(capitalize);
More examples:
Container([,,]).map(reverse).map(first)
//=> Container(3) Container("flamethrower").map(length).map(add())
//=> Container(13)
So "Container"... what you feel about it? It is nothing... you don't need to care about it. Just every time you call map on it, the return value will still inside the Container, so that you can chain map on it.
Curry map:
Define a global map function, which use Ramda curry method:
var map = R.curry(function(f, obj) {
return obj.map(f)
})
Later we will pass Container as obj, and on our Container, we already defined map function, so we can use here as 'obj.map'.
So, again, here 'obj.map' --> Means, goes into the obj, grep the value and apply function f.
So now what we can do:
Container().map(add()) // Container(4) map(add(), Container()) // Container(4), or map(add(1))(Container(3)), since map is curry method
More exmaples:
map(R.compose(R.head, R.reverse), Container("dog"))
//=> Container(“g”)
So all what we have seen so far, we give a name call "Functor".
Functor
“An object or data structure you can map over”
function: map
// Exercise 1
// ==========
// Use _.add(x,y) and map(f,x) to make a function that increments a value inside a functor
console.log("--------Start exercise 1--------")
//map(): Go inside the object and run the function on the value of the object
//map(): Here map is curry function, so take the function as first arguement and object as second arguement.
//map(_.add(1), Identity(2)) --> Identity(3)
var ex1 = map(_.add()); assertDeepEqual(Identity(), ex1(Identity()))
console.log("exercise 1...ok!") // Exercise 2
// ==========
// Use _.head to get the first element of the list
var xs = Identity(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
console.log("--------Start exercise 2--------") var ex2 = map(_.head) assertDeepEqual(Identity('do'), ex2(xs))
console.log("exercise 2...ok!")
[Javascript] Functor Basic Intro的更多相关文章
- [Javascript] Functor law
Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g) ...
- 浅析Javascript
Javascript是一种脚本语言,从出生就被唾弃,一开始人们使用它只是为了解决诸如页面数据校验之类的问题.它基于prototype的面向对象实现一度被认为很丑很难用,甚至很多身处一线Web开发者都不 ...
- JavaScript Web Application summary
Widget/ HTML DOM (CORE) (local dom) DOM, BOM, Event(Framework, UI, Widget) function(closure) DATA (c ...
- 跨域资源共享(CORS)问题解决方案
CORS:Cross-Origin Resource Sharing(跨域资源共享) CORS被浏览器支持的版本情况如下:Chrome 3+.IE 8+.Firefox 3.5+.Opera 12+. ...
- svg-高斯模糊+swiper伦播
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 使用 libevent 和 libev 提高网络应用性能
使用 libevent 和 libev 提高网络应用性能 Martin C. Brown, 作家, Freelance 简介: 构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件, ...
- 模态框zeroModal快速引入
最基本快速接入 <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...
- Js-函数式编程
前言 JavaScript是一门多范式语言,即可使用OOP(面向对象),也可以使用FP(函数式),由于笔者最近在学习React相关的技术栈,想进一步深入了解其思想,所以学习了一些FP相关的知识点,本文 ...
- JS代码风格自动规整工具Prettier
问题背景 通常使用 ESLint做代码风格检查检查, 和部分代码质量检查. 但是使用ESLint在入库时候, 会产生很多的代码修正工作, 需要开发者一个一个的修改. 如果很多,并且时间紧迫,甚是尴尬. ...
随机推荐
- [转]moveTaskToback退后台
http://blog.csdn.net/dacainiao007/article/details/17352367 方法:public boolean moveTaskToBack(boolean ...
- ymPrompt消息组件
<script src="jb51.net/prompt/jquery-1.7.1.js" type="text/javascript"></ ...
- 获取root权限
1.用root建立一个普通用户mary,并切换到mary. < 2.我们首先测试一下当前用户的权限 3.进入到/tmp,新建目录abc. 4.执行下列相关命令.并保证最后一行后面的两块红色部分为 ...
- SQLSERVER拯救某个时间点被误删除的数据
SQLSERVER拯救某个时间点被误删除的数据 转载自:http://blog.csdn.net/dba_huangzj/article/details/8491327 要拯救某个时间点被误删除的数据 ...
- 个性二维码开源专题<液化/圆角/效果>
基础方法: ChangeFillShape //修改填充形状 ChangeFillShape(...) // 摘要: // 修改填充形状 // // 参数: // g: // 图形画板 // // F ...
- 跟我一起学WCF(8)——WCF中Session、实例管理详解
一.引言 由前面几篇博文我们知道,WCF是微软基于SOA建立的一套在分布式环境中各个相对独立的应用进行交流(Communication)的框架,它实现了最新的基于WS-*规范.按照SOA的原则,相对独 ...
- 点击div外面该div消失(二)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS原型链简单图解
JS中原型链,说简单也简单. 首先明确: 函数(Function)才有prototype属性,对象(除Object)拥有__proto__. 首先,我画了一张图. 所谓原型链,指的就是图中的proto ...
- paip.复制文件 文件操作 api的设计uapi java python php 最佳实践
paip.复制文件 文件操作 api的设计uapi java python php 最佳实践 =====uapi copy() =====java的无,要自己写... ====php copy ...
- Python快速上手JSON指南
什么是JSON? 网上对JSON有很多教程,有各种各样的解释.一言以蔽之,JSON本质上是一种语法,这种语法的作用是把数据以字符串的形式存储.传递,多用于Web编程. JSON的典型示例 '{ &qu ...