blob: 94814d11bcb5869cdb038418efda1631949cc7f2 (
plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
require_relative '../../spec_helper'
require_relative 'shared/compose'
describe "Proc#<<" do
it "returns a Proc that is the composition of self and the passed Proc" do
upcase = proc { |s| s.upcase }
succ = proc { |s| s.succ }
(succ << upcase).call('Ruby').should == "RUBZ"
end
it "calls passed Proc with arguments and then calls self with result" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f << g).call(2).should == 16
(g << f).call(2).should == 8
end
it "accepts any callable object" do
inc = proc { |n| n + 1 }
double = Object.new
def double.call(n); n * 2; end
(inc << double).call(3).should == 7
end
it_behaves_like :proc_compose, :<<, -> { proc { |s| s.upcase } }
describe "composition" do
it "is a Proc" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f << g).is_a?(Proc).should == true
(f << g).should_not.lambda?
end
ruby_version_is(''...'3.0') do
it "is a Proc when other is lambda" do
f = proc { |x| x * x }
g = -> x { x + x }
(f << g).is_a?(Proc).should == true
(f << g).should_not.lambda?
end
it "is a lambda when self is lambda" do
f = -> x { x * x }
g = proc { |x| x + x }
(f << g).is_a?(Proc).should == true
(f << g).should.lambda?
end
end
ruby_version_is('3.0') do
it "is a lambda when parameter is lambda" do
f = -> x { x * x }
g = proc { |x| x + x }
lambda_proc = -> x { x }
# lambda << proc
(f << g).is_a?(Proc).should == true
(f << g).should_not.lambda?
# lambda << lambda
(f << lambda_proc).is_a?(Proc).should == true
(f << lambda_proc).should.lambda?
# proc << lambda
(g << f).is_a?(Proc).should == true
(g << f).should.lambda?
end
end
it "may accept multiple arguments" do
inc = proc { |n| n + 1 }
mul = proc { |n, m| n * m }
(inc << mul).call(2, 3).should == 7
end
it "passes blocks to the second proc" do
ScratchPad.record []
one = proc { |&arg| arg.call :one if arg }
two = proc { |&arg| arg.call :two if arg }
(one << two).call { |x| ScratchPad << x }
ScratchPad.recorded.should == [:two]
end
end
end
describe "Proc#>>" do
it "returns a Proc that is the composition of self and the passed Proc" do
upcase = proc { |s| s.upcase }
succ = proc { |s| s.succ }
(succ >> upcase).call('Ruby').should == "RUBZ"
end
it "calls passed Proc with arguments and then calls self with result" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f >> g).call(2).should == 8
(g >> f).call(2).should == 16
end
it "accepts any callable object" do
inc = proc { |n| n + 1 }
double = Object.new
def double.call(n); n * 2; end
(inc >> double).call(3).should == 8
end
it_behaves_like :proc_compose, :>>, -> { proc { |s| s.upcase } }
describe "composition" do
it "is a Proc" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f >> g).is_a?(Proc).should == true
(f >> g).should_not.lambda?
end
it "is a Proc when other is lambda" do
f = proc { |x| x * x }
g = -> x { x + x }
(f >> g).is_a?(Proc).should == true
(f >> g).should_not.lambda?
end
it "is a lambda when self is lambda" do
f = -> x { x * x }
g = proc { |x| x + x }
(f >> g).is_a?(Proc).should == true
(f >> g).should.lambda?
end
it "may accept multiple arguments" do
inc = proc { |n| n + 1 }
mul = proc { |n, m| n * m }
(mul >> inc).call(2, 3).should == 7
end
it "passes blocks to the first proc" do
ScratchPad.record []
one = proc { |&arg| arg.call :one if arg }
two = proc { |&arg| arg.call :two if arg }
(one >> two).call { |x| ScratchPad << x }
ScratchPad.recorded.should == [:one]
end
end
end
|