Functional JavaScriptDirectory
- 1 Get Started with FP
- 2 first-class Functions and applicative programming
- 3 Variable Scope and Closures
- 4 High-order Functions
- 5 function-building Functions
- 6 recursion
- 7 Purity, immutability and policies for change
- 8 flow-based Programming
- 9 Programming without Class
- Ten Appendix
Get Started with FP
- CSV Parse:
-
return _.reduce (str.split ("\ n"), function (table, row) {
Table.push (_.map (Row.split (","), function (cell) {return Cell.trim ();})); return table;}, []); };
- The author is the author of Clojurescript?
first-class Functions and applicative programming
- Return _.reduceright (arguments, function (truth,f) {return truth && f ();}, True);
- Pass NULL as the first argument to apply: This means that this refers to the global object ...
- P44 Traditional SQL Select actually corresponds to project in the relational algebra
- JS's lambda syntax is too cumbersome to write FP-style code that brings trouble
- Restrict (Project (as (library, {ed: ' Edition '), [' Title ', ' ISBN ', ' edition ']),
function (book) {return book.edition>1;});
Variable Scope and ClosuresHigh-order Functionsfunction-building Functions
- function partial (fun/*, Pargs */) {
var pargs = _.rest (arguments);
return function (/* arguments */) {
var args = Cat (Pargs, _.toarray (arguments));
Return fun.apply (fun, args);
} }
recursion
- Trampoline (Avoid mutual-recursive overflow):
- function even (n) {if (n==0) return true; else return Partial1 (odd , n-1); }
- ==> odd (20000001) () () ... ();
- Function Trampoline (fun/*, args */) {
var result = fun. Apply (fun, _.rest (arguments)); Here we go.
while (_.isfunction (result)) result = result ();
return result; }
- generator (lazy infinite sequence)
- head-tail () abstraction, where tail encapsulates the calculation of the remaining series
- 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
- Object#freeze = Deepfreeze
- API: Let object modification method return new Object instance
flow-based Programming
- _.chain
- Lazychain
- thunk:a function waiting to be called
- #force ()
- JQuery $. Deferred ()
- Pipelining
- Action (Monad): Flowing in context?
- Lift
- Actions
Programming without Class
- Mixins
- var polytostring = dispatch (function (s) {return _.isstring (s)? s:undefined;}, ...
- var mymixin = {
Setvalue:function (v) {... this._value = v; this.notify (Oldval, v);}
- _.extend (Myclass.prototype, mymixin);
- var CAS = function (val) {Myclass.call (this, Val);}
- var casmixin = ...
Appendix
- Underscore-contrib
- RxJS
- Bilby ' s Multimethods
- Allong.es:support for stateful iterators
- *reducers:inspired by Clojure ' s reducer
- Clojurescript (the author wrote "The Joy of Clojure"?) )
- Coffeescript
- Literate programming
- Varargs
- List understanding
- Deconstruction Assignment
- Roy:inspired by ML
- ELM (+ + + not required for string stitching): FRP
Functional JavaScript Notes