32. Around “Hello World” in 30 Days 2. The Method
WTF’s a fold?
# sum an array of numbers
[1, 2, 3, 4, 5].inject {|sum, i| sum + i }
# => 15
33. Around “Hello World” in 30 Days 2. The Method
WTF’s a fold?
# collect an array of squares
[1, 2, 3, 4, 5].inject([]) do |coll, i|
coll + [i * i]
end
# => [1, 4, 9, 16, 25]
34. Around “Hello World” in 30 Days 2. The Method
C0ffeeScript
fold: (values, lambda, init...) ->
if init[0]?
result = init[0]
else
result = values[0]
values = values[1..values.length]
for value in values
result = lambda(result, value)
result
35. Around “Hello World” in 30 Days 2. The Method
C0ffeeScript
sum: (values...) -> fold values,
(a, b) -> a + b
x: sum 1, 2, 3, 4, 5
// x == 15
36. Around “Hello World” in 30 Days 2. The Method
C0ffeeScript
(function(){
var fold;
var __slice = Array.prototype.slice;
fold = function fold(values, lambda) {
var _a, _b, _c, _d, init, result, value;
init = __slice.call(arguments, 2, arguments.length - 0);
if ((typeof (_a = init[0]) !== "undefined" && _a !== null)) {
result = init[0];
} else {
result = values[0];
values = values.slice(1, values.length + 1);
}
...
37. Around “Hello World” in 30 Days 2. The Method
Lua
function fold(values, lambda, init)
index, value = next(values, nil)
if init then
result = init
else
result = value
index, value = next(values, index)
end
39. Around “Hello World” in 30 Days 2. The Method
Lua
while index do
result = lambda(result, value)
index, value = next(values, index)
end
return result
end
40. Around “Hello World” in 30 Days 2. The Method
Lua
function sum(values)
return fold(values, function (a, b)
return a + b end)
end
-- sum the set of numbers
sum({1, 2, 3, 4})
41. Around “Hello World” in 30 Days 2. The Method
Lua
function map(values, lambda)
return fold(values, function (c, i)
c[i] = lambda(i)
return c end,
{})
end
-- get the list of squares
map({1, 2, 3, 4}, function (x)
return x * x end)
42. Around “Hello World” in 30 Days 2. The Method
Scheme
(define fold
(lambda (arr f result)
(cond
((null? arr) result)
(else (fold (cdr arr) f (f result (car arr)))))))
43. Around “Hello World” in 30 Days 2. The Method
Scheme
(define fold
(lambda (arr f . result)
(cond
((and (null? arr) (null? result) `()))
((null? arr) (car result))
((null? result) (fold (cdr arr) f (car arr)))
(else (fold (cdr arr) f (f (car result) (car arr)))))))
44. Around “Hello World” in 30 Days 2. The Method
Scheme
> (fold `(1 2 3 4) (lambda (x y) (+ x y)))
10
> (fold `(1 2 3 4)
(lambda (squares x) (cons (* x x) squares)) `())
(16 9 4 1)
45. Around “Hello World” in 30 Days 2. The Method
Erlang
-module(fold).
-export([fold/2, fold/3]).
fold([First | Rest], Fun, Result) ->
fold(Rest, Fun, Fun(Result, First));
fold([], Fun, Result) ->
Result.
86. Around “Hello World” in 30 Days
Thanks!
http://speakerrate.com/talks/3249
@deisinger
Editor's Notes
reduce, accumulate, compress or inject
higher-order functions that iterate an arbitrary function over a data structure in some order and build up a return value
reduce, accumulate, compress or inject
higher-order functions that iterate an arbitrary function over a data structure in some order and build up a return value
The desire to get better and better at something
Not the desire to BE GOOD at something
Hungary
optimum experience
Time passes quickly
self-consciousness disolves
activity not too easy, not too hard
7 weeks of basic training at west point before starting
1 in 20 drops out
not strength, athleticism, intelligence, or leadership ability
“perseverance and passion for long-term goals”
Carol Dweck
People who view intelligence as fluid do much better
Carol Dweck
You worked so hard vs. you are so smart