Weitere ähnliche Inhalte Ähnlich wie Functional Patterns for the non-mathematician (20) Kürzlich hochgeladen (20) Functional Patterns for the non-mathematician3. // associative
add(add(1, 2), 4) == add(1, add(2, 4))
// commutative
add(4, 1) == add(1, 4)
// identity
add(n, 0) == n
// distributive
multiply(2, add(3,4)) == add(multiply(2, 3), multiply(2, 4))
5. var inc = new Increaser(4);
inc.increaseBy(2);
inc.value();
// 6
8. var reverseCap = compose(capitalize, reverse)
reverseCap(“hello”)
//=> “Olleh”
Composition
9. var reverseCap = compose(capitalize, reverse)
reverseCap(“hello”)
//=> “Olleh”
Composition
10. var reverseCap = compose(capitalize, reverse)(“hello”)
//=> “Olleh”
Composition
“hello”“olleh”“Olleh”
13. var i = compose(g, h)
compose(f, i)
Composition
(associativity)
14. var getFromDb = compose(pluck('rows'), User.findAll)
var cleanUpData = compose(capitalize, pluck('name'))
var renderTemplate = TemplateEngine.render(‘users_table')
var makePage = compose(renderTemplate, map(cleanUpData), getFromDb)
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
15. var getFromDb = compose(pluck('rows'), User.findAll)
var cleanUpData = compose(capitalize, pluck('name'))
var renderTemplate = TemplateEngine.render(‘users_table')
var makePage = compose(renderTemplate, map(cleanUpData), getFromDb)
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
makePage({limit: 20})
Perfect world
17. function (property, x) {
return x[property];
}
Getters/Setters
function (property, value, x) {
x[property] = value;
return x;
}
19. var user = {id: 1, name: ‘Alicia'}
var L = makeLenses([‘name’])
view(L.name, user) // 'Alicia'
set(L.name, 'Ally', user) // {id: 1, name: 'Ally'}
over(L.name, toUpperCase, user) // {id: 1, name: 'ALICIA'}
Lenses
20. var user = {id: 1, name: {first: ‘doris’, last: ‘day’ }}
var L = makeLenses([‘name’, ‘first’])
var firstNameChar = compose(L.name, L.first, _1)
over(firstNameChar, toUpperCase, user)
//=> {id: 1, name: {first: ‘Doris’, last: ‘day’ }}
Lenses
21. view(l, set(l, b, a)) == b
set(l, view(l, a), a) == a
set(l, c, set(l, b, a)) == set(l, c, a)
Lens laws
23. if(x !== null && x !== undefined) {
return f(x)
}
Null checking
25. var fmap = function(f, mappable) {
return mappable.map(f)
}
Null checking
26. fmap(function(x) { return x.toUpperCase() }, Maybe(‘hi’))
//=> Maybe(‘HI’)
fmap(function(x) { return toUpperCase(x); }, Maybe(null))
//=> Maybe(null)
Null checking
27. fmap(function(x) { return x.toUpperCase() }, Maybe(‘hi’))
//=> Maybe(‘HI’)
fmap(function(x) { return x.toUpperCase() }, Maybe(null))
//=> Maybe(null)
Null checking
29. var id = function(x) { return x; }
fmap(id, x) == id(x)
Fmap laws
(identity)
32. if(x !== null && x !== undefined) {
return f(x)
} else {
throw ‘Some Error!’
}
Error Handling
34. Either(‘need an int’, 3)
//=> Right(3)
fmap(function(x) { return x + 1; }, Either(‘need an int’, undefined))
//=> Left(‘need an int’)
Error Handling
35. Either(‘need an int’, 3)
//=> Right(3)
Either(‘need an int’, undefined))
//=> Left(‘need an int’)
Error Handling
36. fmap(function(x) { return x + 1; }, Right(2))
//=> Right(3)
fmap(function(x) { return x + 1; }, Either(‘need an int’, undefined))
//=> Left(‘need an int’)
Error Handling
37. fmap(function(x) { return x + 1; }, Right(2))
//=> Right(3)
fmap(function(x) { return x + 1; }, Left(‘need an int’))
//=> Left(‘need an int’)
Error Handling
42. var p = new Promise();
fmap(function(x) { return log(reverse(x)) }, p)
//=> Promise()
p.resolve([1,2,3])
//=>[3, 2, 1]
Future values
45. if(x !== null && x !== undefined) {
var y = f(x)
if(y !== null && y !== undefined) {
return g(y)
}
}
Nesting
48. var getField = compose(Maybe, document.querySelector)
var getValue = compose(Maybe, pluck(‘value’))
var greet = compose(fmap(fmap(concat(‘hello’))), fmap(getValue), getField)
greet(‘#name’)
//=> Maybe(Maybe(‘hello chris’))
var greet = compose(fmap(concat(‘hello’)), mjoin, fmap(getValue), getField)
greet(‘#name’)
//=> Maybe(‘hello chris’)
Nesting
49. var getField = compose(Maybe, document.querySelector)
var getValue = compose(Maybe, pluck(‘value’))
var greet = compose(fmap(fmap(concat(‘hello’))), fmap(getValue), getField)
greet(‘#name’)
//=> Maybe(Maybe(‘hello chris’))
var greet = compose(fmap(concat(‘hello’)), mjoin, fmap(getValue), getField)
greet(‘#name’)
//=> Maybe(‘hello chris’)
Nesting
54. Multiple null args
var notNull = function(x) {
return x !== null && x !== undefined
}
if(notNull(x) && notNull(y)) {
return f(x, y)
}
55. Multiple Async fn’s
var y,z;
f(x, function(result) {
y = result;
if(z) {
return h(y, z)
})
})
g(x, function(result) {
z = result;
if(y) {
return h(y, z)
})
})
60. var tweets_p = Http.get(‘/twitter/tweets’)
var photos_p = Http.get(‘/flickr/photos’)
var makeCollage = _.curry(function (tweets, photos){})
liftA2(makeCollage, tweets_p, photos_p)
Multiple values
61. // identity
ap(A(id), m) == m
// composition
ap(ap(ap(A(compose), f), g), w) == ap(f, ap(g, w))
// homomorphism
ap(A(f), A(x)) == A(f(x))
// interchange
ap(u, A(x)) == ap(A(function(f) { return f(x); }), u)
Applicative laws
76. // left identity
mappend(mempty, x) == x
// right identity
mappend(x, mempty) == x
// associativity
mappend(mappend(x, y), z) == mappend(x, mappend(y, z))
Monoid laws
81. first(reverse)([‘Stan’, ‘Lee']) // [‘natS’, ‘Lee’]
second(reverse)([‘Stan’, ‘Lee']) // [‘Stan’, ‘eeL’]
ampersand(reverse, toUpperCase)(‘Stan’) // [‘natS’, ‘STAN’]
asterisk(reverse, toUpperCase)([‘Stan’, ‘Lee']) // [‘natS’, ‘LEE’]
Arrows
82. A(id) == id
A(compose(f, g)) == A(compose(f, A(g)))
first(A(f)) == A(first(f))
first(compose(f, g)) == compose(first(f), first(g))
compose(first(f), A(pluck(0))) == compose(A(pluck(0)), f)
compose(first(f), A(asterisk(id, g)) == compose(A(asterisk(id, g)), first(f))
compose(first(first(f)), A(assoc) == compose(A(assoc), first(f))
Arrow laws