10. We can curry
var replace = curry(function(regex, x, replaceable) {
return replaceable.replace(regex, x);
});
var squish = replace(/s+/g, '');
squish("I like to move it move it"); // Iliketomoveitmoveit
11. We can compose
var wackyText = compose(capitalize, reverse, squish)
wackyText(“turtle power") // Rewopeltrut
12. Pointfree
var clientApp = compose(render, doThings, httpGet(‘/posts'))
var serverApp = compose(sendJSON, doThings, Db.all(‘posts’))
var shellApp = compose(display, doThings, prompt("what's up?"))
26. Pointfree
var getChildren = function(el) {
return el.childNodes;
}
var getAllChildren = function(els) {
return _.map(els, function(el) {
return getChildren(el);
});
}
var getChildren = get('childNodes')
var getAllChildren = map(getChildren)
27. Pointfree
var getChildren = function(el) {
return el.childNodes;
}
var getAllChildren = function(els) {
return _.map(els, function(el) {
return getChildren(el);
});
}
var getChildren = get('childNodes')
var getAllChildren = map(getChildren)
28. Pointfree
var getChildren = function(el) {
return el.childNodes;
}
var getAllChildren = function(els) {
return _.map(els, function(el) {
return getChildren(el);
});
}
var getChildren = get('childNodes')
var getAllChildren = map(getChildren)
GOOD
29.
30. Pointfree
var grandChildren = function(selector) {
var el = document.querySelector(selector);
var children = getChildren(el);
return map(getChildren, children);
}
var grandChildren = compose( map(getChildren)
, getChildren
, document.querySelector
)
31. Pointfree
var grandChildren = function(selector) {
var el = document.querySelector(selector);
var children = getChildren(el);
return map(getChildren, children);
}
var grandChildren = compose( map(getChildren)
, getChildren
, document.querySelector
)
GOOD
32.
33. Pointfree
var video = { src: 'http://youtube.com?v=23423'
, description: 'family matters ep1'
, screenshots: [ { url: 'i.ytimg.com/OrIxGlo.webp', size: ‘120x120' }
, { url: 'i.ytimg.com/3rAxRdb.webp', size: ‘1020x764' }
]
}
var thumb = compose(_.first, _.get(‘screenshots'))
var thumbUrl = compose(_.get('url'), thumb)
var thumbWithHost = compose(concat('http://youtube.com'), thumbUrl)
thumbWithHost(video)
// http://youtube.com/i.ytimg.com/OrIxaBJ9Glo.webp
34. Pointfree
var video = { src: 'http://youtube.com?v=23423'
, description: 'family matters ep1'
, screenshots: [ { url: 'i.ytimg.com/OrIxGlo.webp', size: ‘120x120' }
, { url: 'i.ytimg.com/3rAxRdb.webp', size: ‘1020x764' }
]
}
var thumbUrl = compose(_.get('url'), thumb)
var thumb = compose(_.first, _.get(‘screenshots'))
var thumbWithHost = compose(concat('http://youtube.com'), thumbUrl)
thumbWithHost(video)
// TypeError
35. Pointfree
var video = { src: 'http://youtube.com?v=23423'
, description: 'family matters ep1'
, screenshots: [ { url: 'i.ytimg.com/OrIxGlo.webp', size: ‘120x120' }
, { url: 'i.ytimg.com/3rAxRdb.webp', size: ‘1020x764' }
]
}
var thumbUrl = compose(_.get('url'), thumb)
var thumb = compose(_.first, _.get(‘screenshots'))
var thumbWithHost = compose(concat('http://youtube.com'), thumbUrl)
thumbWithHost(video)
// TypeError
Meh
66. Typeclass
We can flatten/un-nest them
M(M(a)) -> M(a)
[['hello']] -> ['hello']
Just(Just(true)) -> Just(true)
67. Typeclass
We can apply functions within them
M(a -> b) -> M(a) -> M(b)
68. Typeclass
We can apply functions within them
M(a -> b) -> M(a) -> M(b)
var finished = curry(function(click, anim) { alert(“Finished!"); });
EventStream.of(finished).ap(button_clicks).ap(animation_done);
96. Typeclass
do
a <- Just 3
b <- Just 1
return a + b
Just(3).chain(function(a) {
return Just(1).chain(function(b) {
return Maybe.of(a + b);
});
};
Haskell
JavaScript
97. Typeclass
do
a <- Just 3
b <- Just 1
return a + b
Haskell
JavaScript liftM2(add, Just(3), Just(1))
115. Shortcut Fusion
// g :: forall b. (t -> b -> b) -> b -> b
reduce(c, n, build(g)) = g(c, n)
116. Shortcut Fusion
//build :: (forall b. (a -> b -> b) -> b -> b) -> [a]
var build = function(g){
return g(concat, []);
}
//+ map :: (a -> b) -> [a] -> [b]
var map = curry(function(f, xs){
return build(function(c, n){
return reduce(function(acc, x){ return c(f(x), acc); }, n, xs);
});
});
117. Shortcut Fusion
var sum = reduce(add, 0);
var sqr = function(x) {return x * x }
var sumSqs = compose(sum, map(sqr))
// reduce(function(acc, x){ return add(sqr(x), acc) }, 0);
118. Compile while you
compose
//+ doorman :: [User] -> User
var doorman = compose(first, filter(gte(21)), map(_.get('age')));
119.
120. var addTwenty = memoize(function(x) {
return x + 20;
})
Memoization