Advanced Functional JavaScript
Advanced Functional JavaScript
Introduction 1.1
Everything is Reducible 1.2
Define Composition 1.3
Redefine Map 1.4
Redefine Filter 1.5
Composing Map & Filter 1.6
Transducers 1.7
1
Introduction
2
Everything is Reducible
[actions].reduce(reducerFn, initial_state);
//-> final_state
Length of an array
Reverse an array
3
Everything is Reducible
4
Define Composition
compose(. . .)
compose(h,g,f)(x) = h(g(f(x)))
/**
* usage:
* let inc_one = (x) => x + 1;
* let inc_two = (x) => x + 2;
* let inc_three = compose(inc_one, inc_two);
* inc_three(5); //-> 8
*/
function compose() {
var argsOuter = arguments;
return function() {
var arg = arguments;
var out = undefined;
for(var i = argsOuter.length-1; i >= 0; i--) {
if (out === undefined) {
out = argsOuter[i].apply(null, arg)
} else {
out = argsOuter[i].call(null, out);
}
}
return out;
}
}
5
Define Composition
6
Redefine Map
Classical map
7
Redefine Map
8
Redefine Filter
Classical filter
9
Redefine Filter
10
Composing Map & Filter
11
Transducers
Transducers
Mapping & Filtering functions defined with no array behavior dependencies
can be called transducers.
We can easily compose mapping & filtering transducers and use them as
Reducing Functions
12
Transducers
References
Transducers are coming
CSP and transducers in JavaScript
Transducers.js: A JavaScript Library for Transformation of Data
13