[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在入库时候, 会产生很多的代码修正工作, 需要开发者一个一个的修改. 如果很多,并且时间紧迫,甚是尴尬. ...
随机推荐
- Centos6.5 下安装PostgreSQL9.4数据库
一.安装PostgreSQL源 CentOS 6.x 32bit rpm -Uvh http://yum.postgresql.org/9.4/redhat/rhel-6-i386/pgdg-cent ...
- WPF快速入门系列(4)——深入解析WPF绑定
一.引言 WPF绑定使得原本需要多行代码实现的功能,现在只需要简单的XAML代码就可以完成之前多行后台代码实现的功能.WPF绑定可以理解为一种关系,该关系告诉WPF从一个源对象提取一些信息,并将这些信 ...
- 作业七:团队项目——Alpha版本冲刺阶段-01
昨天进展:准备开发环境,安装软件. 今天安排:软件框架设计. 小组一共三人,陈芝航因家里有事,与我们进行了QQ视屏会议.
- [51单片机] HC-SR04超声波测距仪 基础代码
>_<:超声波测距仪模块: >_<:51单片机,11.0592MHz晶振,将采集数据发送到串口的基础例子: >_<:代码: /******************* ...
- [JS5] 利用onload执行脚本
<html> <head> <title>利用onload执行脚本</title> <SCRIPT TYPE="text/JavaScr ...
- IOS Storyboard使用-模拟登录、注册、混合使用
最近分析IOS的占有率,发现5.0以下的少之又少了,故而决定新的App用 Storyboard开发,找了很多资料都是点上的,这个简单的demo是测试代码,发上来,供新手参考. 模拟登录.注册.和显示主 ...
- AngularJS快速入门指南06:过滤器
thead>tr>th, table.reference>tbody>tr>th, table.reference>tfoot>tr>th, table ...
- JQuery以JSON方式提交数据到服务端
JQuery将Ajax数据请求进行了封装,从而使得该操作实现起来容易许多.以往我们要写很多的代码来实现该功能,现在只需要调用$.ajax()方法,并指明请求的方式.地址.数据类型,以及回调方法等.下面 ...
- [BTS] Error biztalk arguments null exception string reference not set to an instance of a string. parameter name
biztalk arguments null exception string reference not set to an instance of a string. parameter name ...
- Nginx - Windows下Nginx初入门
公司刚使用nginx,预先学习下.鉴于机器没有Linux环境,在Windows熟悉下. 下载 目前(2015-07-11),nginx的稳定版本是1.8.0,在官网下载先,windows版的nginx ...