[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在入库时候, 会产生很多的代码修正工作, 需要开发者一个一个的修改. 如果很多,并且时间紧迫,甚是尴尬. ...
随机推荐
- Install Solr+tomcat
1. chose the release you want: http://archive.apache.org/dist/lucene/solr/. 1) copy [solr_home]/dist ...
- 过滤器(servlet.filter)和拦截器(springmvc.interceptor)区别
①拦截器是基于java的反射机制的,而过滤器是基于函数回调. ②拦截器不依赖与servlet容器,过滤器依赖与servlet容器. ③拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求 ...
- 【leetcode】Linked List Cycle
这个题真是坑死了,只怪自己不好吧.一开始审题,以为是给定一个首尾相连的链表,查看其中是否有循环(原谅我的无知吧!!).然后在那写啊写啊写,还在纠结是局部循环还是前一半和后一半一样这样的循环,blah ...
- 简单sql操作
----------------------------- 数据库的有关SQL语句 -------------------------1.数据库 创建 create database data_nam ...
- AS3从入门到放弃
工作久了,在技术上肯定有自己的一些见解.一直以来都懒得写下来,总觉得尤其写博客的时间,还不如自己学一点新东西.但不能总找这样的接口啊,于是乎开始了这篇博客. 工作了三年,有一年半的时间是在做AS3,在 ...
- node-webkit教程(14)禁用缓存
1.在开发者工具中禁用缓存 上面这张图,是在node-webkit 中 在开发工具中配置禁用缓存的选项. 使用这个选项可以有效的禁用所有页面缓存. 2. 在配置文件中,配置webkit 缓存禁用和启用 ...
- [Hyper-V]制作一个干净的操作系统模板
描述: 在Hyper-V里创建虚拟机的时候,我们可以先来创建一个干净的操作系统,将其制作为操作系统模板,该虚拟机的磁盘文件也将被视作基础磁盘以方便基于它创建差异化磁盘 安装其它虚拟机的时候就可以差异化 ...
- [ACM_其他] Square Ice (poj1099 规律)
Description Square Ice is a two-dimensional arrangement of water molecules H2O, with oxygen at the v ...
- ActiveMQ第四弹:在HermesJMS中创建ActiveMQ Session
Hermes JMS是一个开源免费的跨平台的JMS消息监听工具.它可以很方便和各种JMS框架集成和交互,可以用来监听.发送.接收.修改.存储消息等.这篇文章将讲解HermesJMS如何集成Active ...
- thrift之TTransport层的堵塞的套接字I/O传输类TSocket
本节将介绍第一个实现具体传输功能的类TSocket,这个类是基于TCP socket实现TTransport的接口.下面具体介绍这个类的相关函数功能实现. 1.构造函数 分析一个类的功能首先看它的定义 ...