-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_procs.rb
47 lines (37 loc) · 996 Bytes
/
06_procs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
square = Proc.new {|x| x**2 }
square.call(7)
square.(3)
square[4]
# Procs are closures, which means that they
# remember the entire context (scope) in
# which they were created
power = 3
expo = Proc.new{|x| x**power }
expo[3] #=> 27
# This remains true, even when the proc
# is called in a context which doesn't
# have (direct) access to the proc's
# enclosed scope
class ExpoMaker
def self.make_cube
pwr = 3
Proc.new {|x| x**pwr }
end
end
cube = ExpoMaker.make_cube
cube[2] #=> 8
# Procs have currying!
add = Proc.new {|a,b| a + b }
add[2,3] #=> 5
increment = add.curry.call(1)
increment[100] #=> 101
# Ruby also has Lambdas, how are they different?
# - Lambdas enforce arity (correct number of
# arguments)
# Create `ladd` with same implementation
# as `add`, then call `add.(2)` and
# . compare error with `ladd.(2)`
# - Lambdas support default arguments!
# - return from Lambda just returns from
# . the lambda (Procs return from the calling
# . function!)