-
Notifications
You must be signed in to change notification settings - Fork 0
/
funkify.rb
58 lines (48 loc) · 1.02 KB
/
funkify.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
48
49
50
51
52
53
54
55
56
57
58
require 'funkify'
require 'graph/function'
Graph::Function.as_gif(File.expand_path('../funkify.gif', __FILE__))
tiny_int_generator = proc {|size| Array.new(size) { rand(-9...9) } }
comparison = Graph::Function::Comparison.new(tiny_int_generator)
module Composition
refine Proc do
def compose
proc do |i,x|
self.(i.(x))
end.curry
end
def % other
self.compose.(other)
end
end
end
class Composer
def double
proc {|a| a * 2 }
end
def triple
proc {|a| a * 3 }
end
end
class MyFunkyClass
include Funkify
# alternatively, if we invoke auto_curry without a parameter
# then all subsequent methods will be autocurried
auto_curry
def mult(x, y)
x * y
end
def negate(x)
-x
end
end
def double_map(a)
a.map(&Composer.new.double).map(&Composer.new.triple)
end
def funkify(a)
a.map(&(MyFunkyClass.new.negate * MyFunkyClass.new.mult(10)))
end
using Composition
def composed(a)
a.map(&(Composer.new.double % Composer.new.triple))
end
comparison.of(method(:double_map), method(:composed), method(:funkify))