7. reduce
• The grandaddy, most iterators can be made
out of reduce
• “Reduce a list to a new object”
8. map
• Bread and butter iterator
• Used to transform a list of elements from
one type to another
• Whatever is returned within the iterator is
the new object
9. pluck
• Iterates over a list of objects and returns a
new list made of the specified property
from each object
• A common use for map
• IE: plucking the name from an array of
people objects
10. filter
• Only allows values that pass a predicate
into the new list
• As with all these methods, it returns copies
of the data
11. find
• Accepts a predicate and returns the first
item that passes the predicate
12. Much, much more...
• Many predicates
(isArray, isNull, ...)
• keys / values
• groupBy
• result
• array methods
• union / intersection
• chain / compose
• extend / defaults
• simple templates
• pick / omit
13. FP vs OO
• The language of the... language
• How complexity is hidden
• How data is stored
31. Notation
//+ <method name> :: type -> type -> type
A function that take takes an argument and returns a
value of type
32. Notation
//+ <method name> :: type -> type -> type
A function that take takes an argument and returns a
value of type
integer -> integer
A function that takes in a single
argument of type integer
The return type of integer. Since
nothing follows, it is the final return
33. Notation
//+ <method name> :: type -> type -> type
A function that take takes an argument and returns a
value of type
35. //+ not :: a -> boolean
function not (x) {
return !x;
}
not(true) //> false
36. //+ not :: a -> boolean
function not (x) {
return !x;
}
not(true) //> false
The a (or any other lower case letter) means we
don’t care about the type.
37. //+ add :: a, a -> a
function add(x, y) {
return x + y;
}
add(1, 2); //> 3
38. //+ add :: a, a -> a
function add(x, y) {
return x + y;
}
add(1, 2); //> 3
I am abusing notation :(
39. “Taking a function that takes multiple arguments and
transforming it to a chain of functions that accept a single
argument.” - Wikipedia
41. //+ add :: a -> a -> a
function add(x) {
return function (y) {
return x + y;
};
}
add(1)(2); //> 3
ed
42. //+ add :: a -> a -> a
function add(x) {
return function (y) {
return x + y;
};
}
add(1)(2); //> 3
ed
43. //+ add :: a -> a -> a
//+ add :: 1 -> a -> a
//+ add1 :: integer -> integer
var add1 = add(1);
add1(2);
//> 3
44. //+ add :: a -> a -> a
//+ add :: ‘Mr. ‘ -> a -> a
//+ addTitle :: string -> string
var addTitle = add('Mr. ');
addTitle('Baggins');
//> Mr. Baggins
56. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val, index, list) {
return crnt + val;
}, 0, items);
//> 6
We don’t use these two
arguments so we can omit them
57. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val) {
return crnt + val;
}, 0, items);
//> 6
58. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val) {
return crnt + val;
}, 0, items);
//> 6
This function looks familiar...
59. //+ add :: a -> a -> a
var add = function(x, y) {
return x + y;
}.autoCurry();
60. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (x, y) {
return x + y;
}, 0, items);
//> 6
61. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (x, y) {
return x + y;
}, 0, items);
//> 6
Instead of inlining the function, just pass
the function’s pointer
62. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(add, 0, items);
//> 6
63. var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(add, 0, items);
//> 6
Cool! But isn’t reduce curried?
Can we make this into a generic function?
64. var items = [1, 2, 3];
//+ reduce :: add -> 0 -> array -> a
//+ sum :: array -> integer
var sum = reduce(add, 0);
sum(items);
//> 6