Functional JavaScript 笔记

Functional JavaScript

Get Started with FP

  1. CSV parse:
    return _.reduce(str.split("\n"), function(table, row){
    table.push(_.map(row.split(","), function(cell){return cell.trim();})); return table;}, []); };
  2. 作者是ClojureScript的作者?

First-class Functions and Applicative Programming

  1. return _.reduceRight(arguments, function(truth,f){ return truth && f(); }, true);
  2. 传递null作为第一个参数给apply:意味着this引用全局对象...
  3. p44 传统的SQL Select实际上对应关系代数里的Project
  4. JS的lambda语法太麻烦,给写FP风格的代码带来了麻烦
    1. restrict( project( as(library, {ed: 'edition'}), ['title','isbn','edition']),
      function(book){ return book.edition>1; } );

Variable Scope and Closures

High-order Functions

Function-Building Functions

  1. function partial(fun /*, pargs */){
    var pargs = _.rest(arguments);
    return function(/* arguments */){
    var args = cat(pargs, _.toArray(arguments));
    return fun.apply(fun, args);
    } }

Recursion

  1. trampoline(避免mutual-recursive溢出):
    1. function even(n){ if(n==0) return true; else return partial1(odd, n-1); }
      1. ==> odd(20000001)()()...();
    2. function trampoline(fun /*, args */){
      var result = fun.apply(fun, _.rest(arguments)); //又来了
      while(_.isFunction(result)) result = result();
      return result; }
  2. generator(惰性无限序列)
    1. head-tail()抽象,其中,tail封装了“剩余序列的计算”
    2. function genTake(n, gen){
      var doTake = function(x, g, ret){ if(x==0)return ret; else return partial(doTake, x-1, genTail(g), cat(ret, genHead(g)));}
      return trampoline(doTake, n, gen, []);

Purity, Immutability and Policies for Change

  1. Object#freeze => deepFreeze
  2. API:让对象修改方法返回新对象实例

Flow-based Programming

  1. _.chain
  2. LazyChain
    1. thunk:a function waiting to be called
    2. #force()
  3. jQuery $.Deferred()
  4. Pipelining
  5. Action(Monad):flowing in context?
    1. lift
    2. actions

Programming without Class

  1. Mixins
    1. var polyToString = dispatch( function(s){ return _.isString(s)? s : undefined; }, ...
  2. var MyMixin = {
    setValue: function(v){ ... this._value = v; this.notify(oldVal, v); }
  3. _.extend(MyClass.prototype, MyMixin);
  4. var CAS = function(val){ MyClass.call(this, val); }
    1. var CASMixin = ...

Appendix

  1. Underscore-contrib
  2. RxJS
  3. Bilby's multimethods
  4. allong.es:support for stateful iterators
  5. *Reducers:inspired by Clojure's reducer
  6. ClojureScript(作者就是写《The Joy of Clojure》的?)
  7. CoffeeScript
    1. Literate编程
    2. Varargs
    3. 列表理解
    4. 解构赋值
  8. Roy:inspired by ML
  9. Elm(+不用作字符串拼接,需要用++):FRP 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值