fear 2.0.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +105 -0
- data/.simplecov +2 -2
- data/.standard.yml +1 -0
- data/CHANGELOG.md +8 -0
- data/Gemfile +14 -2
- data/Gemfile.lock +84 -78
- data/LICENSE.txt +1 -1
- data/README.md +18 -70
- data/Rakefile +55 -142
- data/benchmarks/dry_do_vs_fear_for.txt +7 -6
- data/benchmarks/dry_some_fmap_vs_fear_some_map.txt +7 -6
- data/benchmarks/factorial.txt +7 -9
- data/benchmarks/fear_gaurd_and1_vs_new.txt +7 -6
- data/benchmarks/fear_gaurd_and2_vs_and.txt +8 -7
- data/benchmarks/fear_gaurd_and3_vs_and_and.txt +7 -6
- data/benchmarks/fear_pattern_matching_construction_vs_execution.txt +7 -6
- data/benchmarks/pattern_matching_dry_vs_qo_vs_fear_try.txt +8 -10
- data/examples/pattern_extracting.rb +2 -2
- data/examples/pattern_matching_number_in_words.rb +12 -12
- data/fear.gemspec +5 -21
- data/lib/fear/either/left_projection.rb +237 -0
- data/lib/fear/either/pattern_match.rb +49 -0
- data/lib/fear/either.rb +21 -12
- data/lib/fear/either_api.rb +2 -4
- data/lib/fear/empty_partial_function.rb +3 -3
- data/lib/fear/failure/pattern_match.rb +14 -0
- data/lib/fear/failure.rb +5 -5
- data/lib/fear/for.rb +1 -1
- data/lib/fear/for_api.rb +1 -3
- data/lib/fear/future.rb +6 -6
- data/lib/fear/future_api.rb +0 -5
- data/lib/fear/left/pattern_match.rb +15 -0
- data/lib/fear/left.rb +4 -4
- data/lib/fear/none.rb +0 -87
- data/lib/fear/none_class/pattern_match.rb +16 -0
- data/lib/fear/none_class.rb +85 -0
- data/lib/fear/option/pattern_match.rb +47 -0
- data/lib/fear/option.rb +2 -6
- data/lib/fear/option_api.rb +1 -3
- data/lib/fear/partial_function/and_then.rb +2 -2
- data/lib/fear/partial_function/combined.rb +3 -3
- data/lib/fear/partial_function/empty.rb +3 -5
- data/lib/fear/partial_function/guard.rb +0 -4
- data/lib/fear/partial_function/or_else.rb +2 -4
- data/lib/fear/partial_function.rb +0 -9
- data/lib/fear/partial_function_class.rb +2 -2
- data/lib/fear/pattern_match.rb +5 -4
- data/lib/fear/pattern_matching_api.rb +1 -4
- data/lib/fear/right/pattern_match.rb +15 -0
- data/lib/fear/right.rb +4 -4
- data/lib/fear/right_biased.rb +2 -0
- data/lib/fear/some/pattern_match.rb +15 -0
- data/lib/fear/some.rb +3 -3
- data/lib/fear/success/pattern_match.rb +16 -0
- data/lib/fear/success.rb +5 -5
- data/lib/fear/try/pattern_match.rb +29 -0
- data/lib/fear/try.rb +3 -7
- data/lib/fear/try_api.rb +1 -3
- data/lib/fear/utils.rb +1 -1
- data/lib/fear/version.rb +1 -1
- data/lib/fear.rb +3 -14
- data/spec/fear/awaitable_spec.rb +0 -2
- data/spec/fear/either/left_projection_spec.rb +289 -0
- data/spec/fear/{either_pattern_match_spec.rb → either/pattern_match_spec.rb} +7 -7
- data/spec/fear/either_spec.rb +1 -1
- data/spec/fear/left_spec.rb +1 -1
- data/spec/fear/{option_pattern_match_spec.rb → option/pattern_match_spec.rb} +6 -6
- data/spec/fear/option_spec.rb +2 -2
- data/spec/fear/partial_function/any_spec.rb +3 -3
- data/spec/fear/partial_function/empty_spec.rb +2 -2
- data/spec/fear/partial_function_and_then_spec.rb +5 -5
- data/spec/fear/partial_function_composition_spec.rb +6 -6
- data/spec/fear/partial_function_or_else_spec.rb +13 -13
- data/spec/fear/partial_function_spec.rb +7 -7
- data/spec/fear/pattern_match_spec.rb +5 -5
- data/spec/fear/right_spec.rb +1 -1
- data/spec/fear/{try_pattern_match_spec.rb → try/try_pattern_match_spec.rb} +7 -7
- data/spec/fear/try_api_spec.rb +2 -2
- data/spec/fear/utils_spec.rb +3 -3
- data/spec/support/.keep +0 -0
- metadata +27 -296
- data/.github/workflows/rubocop.yml +0 -39
- data/.github/workflows/spec.yml +0 -43
- data/.rubocop.yml +0 -7
- data/benchmarks/fear_pattern_extracting_with_vs_without_cache.txt +0 -11
- data/benchmarks/pattern_matching_qo_vs_fear_pattern_extraction.txt +0 -11
- data/benchmarks/pattern_matching_qo_vs_fear_try_execution.txt +0 -11
- data/lib/dry/types/fear/option.rb +0 -125
- data/lib/dry/types/fear.rb +0 -8
- data/lib/fear/either_pattern_match.rb +0 -52
- data/lib/fear/failure_pattern_match.rb +0 -12
- data/lib/fear/left_pattern_match.rb +0 -11
- data/lib/fear/none_pattern_match.rb +0 -14
- data/lib/fear/option_pattern_match.rb +0 -50
- data/lib/fear/right_pattern_match.rb +0 -13
- data/lib/fear/some_pattern_match.rb +0 -13
- data/lib/fear/struct.rb +0 -237
- data/lib/fear/success_pattern_match.rb +0 -14
- data/lib/fear/try_pattern_match.rb +0 -32
- data/spec/dry/types/fear/option/constrained_spec.rb +0 -22
- data/spec/dry/types/fear/option/core_spec.rb +0 -77
- data/spec/dry/types/fear/option/default_spec.rb +0 -21
- data/spec/dry/types/fear/option/hash_spec.rb +0 -58
- data/spec/dry/types/fear/option/option_spec.rb +0 -97
- data/spec/struct_pattern_matching_spec.rb +0 -36
- data/spec/struct_spec.rb +0 -194
- data/spec/support/dry_types.rb +0 -6
@@ -0,0 +1,289 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Fear::Either::LeftProjection do
|
4
|
+
subject(:projection) { described_class.new(either) }
|
5
|
+
|
6
|
+
describe "#include?" do
|
7
|
+
context "on Fear::Right" do
|
8
|
+
let(:either) { Fear.right("value") }
|
9
|
+
|
10
|
+
it { is_expected.not_to include("value") }
|
11
|
+
end
|
12
|
+
|
13
|
+
context "on Fear::Left" do
|
14
|
+
let(:either) { Fear.left("value") }
|
15
|
+
|
16
|
+
it { is_expected.to include("value") }
|
17
|
+
it { is_expected.not_to include("another values") }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#get_or_else" do
|
22
|
+
context "on Fear::Right" do
|
23
|
+
let(:either) { Fear.right("value") }
|
24
|
+
|
25
|
+
context "with block" do
|
26
|
+
subject { projection.get_or_else { "default" } }
|
27
|
+
|
28
|
+
it "returns default value" do
|
29
|
+
is_expected.to eq("default")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with default argument" do
|
34
|
+
subject { projection.get_or_else("default") }
|
35
|
+
|
36
|
+
it "returns default value" do
|
37
|
+
is_expected.to eq("default")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "with false argument" do
|
42
|
+
subject { projection.get_or_else(false) }
|
43
|
+
|
44
|
+
it "returns default value" do
|
45
|
+
is_expected.to eq(false)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "with nil argument" do
|
50
|
+
subject { projection.get_or_else(nil) }
|
51
|
+
|
52
|
+
it "returns default value" do
|
53
|
+
is_expected.to eq(nil)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "on Fear::Left" do
|
59
|
+
let(:either) { Fear.left("value") }
|
60
|
+
|
61
|
+
context "with block" do
|
62
|
+
subject { projection.get_or_else { "default" } }
|
63
|
+
|
64
|
+
it "returns value" do
|
65
|
+
is_expected.to eq("value")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "with default argument" do
|
70
|
+
subject { projection.get_or_else("default") }
|
71
|
+
|
72
|
+
it "returns value" do
|
73
|
+
is_expected.to eq("value")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "with false argument" do
|
78
|
+
subject { projection.get_or_else(false) }
|
79
|
+
|
80
|
+
it "returns value" do
|
81
|
+
is_expected.to eq("value")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "with nil argument" do
|
86
|
+
subject { projection.get_or_else(nil) }
|
87
|
+
|
88
|
+
it "returns value" do
|
89
|
+
is_expected.to eq("value")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#each" do
|
96
|
+
context "on Fear::Right" do
|
97
|
+
let(:either) { Fear.right(42) }
|
98
|
+
|
99
|
+
it "does not yield control and returns either" do
|
100
|
+
expect do |block|
|
101
|
+
expect(projection.each(&block)).to eq(either)
|
102
|
+
end.not_to yield_control
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context "on Fear::Left" do
|
107
|
+
let(:either) { Fear.left(42) }
|
108
|
+
|
109
|
+
it "yields block and return either" do
|
110
|
+
expect do |block|
|
111
|
+
expect(projection.each(&block)).to eq(either)
|
112
|
+
end.to yield_with_args(42)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "#map" do
|
118
|
+
subject { projection.map(&:length) }
|
119
|
+
|
120
|
+
context "on Fear::Right" do
|
121
|
+
let(:either) { Fear.right("value") }
|
122
|
+
|
123
|
+
it "returns self" do
|
124
|
+
is_expected.to eq(either)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "on Fear::Left" do
|
129
|
+
let(:either) { Fear.left("value") }
|
130
|
+
|
131
|
+
it "perform transformation" do
|
132
|
+
is_expected.to be_left_of(5)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#flat_map" do
|
138
|
+
context "on Fear::Right" do
|
139
|
+
subject { projection.flat_map { Fear.right(_1 * 2) } }
|
140
|
+
|
141
|
+
let(:either) { Fear.right("value") }
|
142
|
+
|
143
|
+
it "returns self" do
|
144
|
+
is_expected.to eq(either)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "on Fear::Left" do
|
149
|
+
subject(:either) { Fear.left(21) }
|
150
|
+
|
151
|
+
context "block returns neither left, nor right" do
|
152
|
+
subject { proc { projection.flat_map { 42 } } }
|
153
|
+
|
154
|
+
it "fails with TypeError" do
|
155
|
+
is_expected.to raise_error(TypeError)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
context "block returns Right" do
|
160
|
+
subject { projection.flat_map { |e| Fear.right(e * 2) } }
|
161
|
+
|
162
|
+
it "maps to block result" do
|
163
|
+
is_expected.to be_right_of(42)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
context "block returns Left" do
|
168
|
+
subject { projection.flat_map { |e| Fear.left(e * 2) } }
|
169
|
+
|
170
|
+
it "maps to block result" do
|
171
|
+
is_expected.to be_left_of(42)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
describe "#to_option" do
|
178
|
+
subject { projection.to_option }
|
179
|
+
|
180
|
+
context "on Fear::Right" do
|
181
|
+
let(:either) { Fear.right("value") }
|
182
|
+
|
183
|
+
it { is_expected.to be_none }
|
184
|
+
end
|
185
|
+
|
186
|
+
context "on Fear::Left" do
|
187
|
+
let(:either) { Fear.left("value") }
|
188
|
+
|
189
|
+
it { is_expected.to be_some_of("value") }
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
describe "#to_a" do
|
194
|
+
subject { projection.to_a }
|
195
|
+
|
196
|
+
context "on Fear::Right" do
|
197
|
+
let(:either) { Fear.right("value") }
|
198
|
+
|
199
|
+
it { is_expected.to eq([]) }
|
200
|
+
end
|
201
|
+
|
202
|
+
context "on Fear::Left" do
|
203
|
+
let(:either) { Fear.left("value") }
|
204
|
+
|
205
|
+
it { is_expected.to eq(["value"]) }
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
describe "#any?" do
|
210
|
+
subject { projection.any?(&predicate) }
|
211
|
+
|
212
|
+
context "on Fear::Right" do
|
213
|
+
let(:predicate) { ->(v) { v == "value" } }
|
214
|
+
let(:either) { Fear.right("value") }
|
215
|
+
|
216
|
+
it { is_expected.to eq(false) }
|
217
|
+
end
|
218
|
+
|
219
|
+
context "on Fear::Left" do
|
220
|
+
let(:either) { Fear.left("value") }
|
221
|
+
|
222
|
+
context "matches predicate" do
|
223
|
+
let(:predicate) { ->(v) { v == "value" } }
|
224
|
+
|
225
|
+
it { is_expected.to eq(true) }
|
226
|
+
end
|
227
|
+
|
228
|
+
context "does not match predicate" do
|
229
|
+
let(:predicate) { ->(v) { v != "value" } }
|
230
|
+
|
231
|
+
it { is_expected.to eq(false) }
|
232
|
+
end
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe "select" do
|
237
|
+
subject { projection.select(&predicate) }
|
238
|
+
|
239
|
+
context "on Fear::Right" do
|
240
|
+
let(:either) { Fear.right("value") }
|
241
|
+
let(:predicate) { ->(v) { v == "value" } }
|
242
|
+
|
243
|
+
it { is_expected.to be_right_of("value") }
|
244
|
+
end
|
245
|
+
|
246
|
+
context "on Fear::Left" do
|
247
|
+
let(:either) { Fear.left("value") }
|
248
|
+
|
249
|
+
context "predicate evaluates to true" do
|
250
|
+
let(:predicate) { ->(v) { v == "value" } }
|
251
|
+
|
252
|
+
it { is_expected.to be_left_of("value") }
|
253
|
+
end
|
254
|
+
|
255
|
+
context "predicate evaluates to false" do
|
256
|
+
let(:predicate) { ->(v) { v != "value" } }
|
257
|
+
|
258
|
+
it { is_expected.to be_right_of("value") }
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe "#find" do
|
264
|
+
subject { projection.find(&predicate) }
|
265
|
+
|
266
|
+
context "on Fear::Right" do
|
267
|
+
let(:either) { Fear.right("value") }
|
268
|
+
let(:predicate) { ->(v) { v == "value" } }
|
269
|
+
|
270
|
+
it { is_expected.to be_none }
|
271
|
+
end
|
272
|
+
|
273
|
+
context "on Fear::Left" do
|
274
|
+
let(:either) { Fear.left("value") }
|
275
|
+
|
276
|
+
context "predicate evaluates to true" do
|
277
|
+
let(:predicate) { ->(v) { v == "value" } }
|
278
|
+
|
279
|
+
it { is_expected.to be_some_of(either) }
|
280
|
+
end
|
281
|
+
|
282
|
+
context "predicate evaluates to false" do
|
283
|
+
let(:predicate) { ->(v) { v != "value" } }
|
284
|
+
|
285
|
+
it { is_expected.to be_none }
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.describe Fear::
|
3
|
+
RSpec.describe Fear::Either::PatternMatch do
|
4
4
|
context "Right" do
|
5
5
|
let(:matcher) do
|
6
6
|
described_class.new do |m|
|
@@ -10,10 +10,10 @@ RSpec.describe Fear::EitherPatternMatch do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it do
|
13
|
-
expect(matcher.(Fear.right(4))).to eq("4 is even")
|
14
|
-
expect(matcher.(Fear.right(3))).to eq("3 is odd")
|
13
|
+
expect(matcher.call(Fear.right(4))).to eq("4 is even")
|
14
|
+
expect(matcher.call(Fear.right(3))).to eq("3 is odd")
|
15
15
|
expect do
|
16
|
-
matcher.(Fear.left(44))
|
16
|
+
matcher.call(Fear.left(44))
|
17
17
|
end.to raise_error(Fear::MatchError)
|
18
18
|
end
|
19
19
|
end
|
@@ -27,10 +27,10 @@ RSpec.describe Fear::EitherPatternMatch do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it do
|
30
|
-
expect(matcher.(Fear.left(4))).to eq("4 is even")
|
31
|
-
expect(matcher.(Fear.left(3))).to eq("3 is odd")
|
30
|
+
expect(matcher.call(Fear.left(4))).to eq("4 is even")
|
31
|
+
expect(matcher.call(Fear.left(3))).to eq("3 is odd")
|
32
32
|
expect do
|
33
|
-
matcher.(Fear.right(44))
|
33
|
+
matcher.call(Fear.right(44))
|
34
34
|
end.to raise_error(Fear::MatchError)
|
35
35
|
end
|
36
36
|
end
|
data/spec/fear/either_spec.rb
CHANGED
data/spec/fear/left_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.describe Fear::
|
3
|
+
RSpec.describe Fear::Option::PatternMatch do
|
4
4
|
context "Some" do
|
5
5
|
let(:matcher) do
|
6
6
|
described_class.new do |m|
|
@@ -10,10 +10,10 @@ RSpec.describe Fear::OptionPatternMatch do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it do
|
13
|
-
expect(matcher.(Fear.some(4))).to eq("4 is even")
|
14
|
-
expect(matcher.(Fear.some(3))).to eq("3 is odd")
|
13
|
+
expect(matcher.call(Fear.some(4))).to eq("4 is even")
|
14
|
+
expect(matcher.call(Fear.some(3))).to eq("3 is odd")
|
15
15
|
expect do
|
16
|
-
matcher.(Fear.none)
|
16
|
+
matcher.call(Fear.none)
|
17
17
|
end.to raise_error(Fear::MatchError)
|
18
18
|
end
|
19
19
|
end
|
@@ -26,9 +26,9 @@ RSpec.describe Fear::OptionPatternMatch do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
it do
|
29
|
-
expect(matcher.(Fear.none)).to eq("nil")
|
29
|
+
expect(matcher.call(Fear.none)).to eq("nil")
|
30
30
|
expect do
|
31
|
-
matcher.(Fear.some(3))
|
31
|
+
matcher.call(Fear.some(3))
|
32
32
|
end.to raise_error(Fear::MatchError)
|
33
33
|
end
|
34
34
|
end
|
data/spec/fear/option_spec.rb
CHANGED
@@ -62,7 +62,7 @@ RSpec.describe Fear::Option do
|
|
62
62
|
|
63
63
|
context "some mapped to nil" do
|
64
64
|
let(:option) { Fear.some(42) }
|
65
|
-
let(:filter_map) { ->(*) {
|
65
|
+
let(:filter_map) { ->(*) {} }
|
66
66
|
|
67
67
|
it { is_expected.to be_none }
|
68
68
|
end
|
@@ -97,7 +97,7 @@ RSpec.describe Fear::Option do
|
|
97
97
|
end
|
98
98
|
|
99
99
|
describe "#matcher" do
|
100
|
-
subject(:result) { matcher.(value) }
|
100
|
+
subject(:result) { matcher.call(value) }
|
101
101
|
|
102
102
|
let(:matcher) do
|
103
103
|
described_class.matcher do |m|
|
@@ -18,8 +18,8 @@ RSpec.describe Fear::PartialFunction::Any do
|
|
18
18
|
describe ".to_proc" do
|
19
19
|
subject(:any_proc) { any.to_proc }
|
20
20
|
|
21
|
-
it { expect(any_proc.(42)).to eq(true) }
|
22
|
-
it { expect(any_proc.("foo")).to eq(true) }
|
23
|
-
it { expect(any_proc.(Object.new)).to eq(true) }
|
21
|
+
it { expect(any_proc.call(42)).to eq(true) }
|
22
|
+
it { expect(any_proc.call("foo")).to eq(true) }
|
23
|
+
it { expect(any_proc.call(Object.new)).to eq(true) }
|
24
24
|
end
|
25
25
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
RSpec.describe Fear::PartialFunction::
|
3
|
+
RSpec.describe Fear::PartialFunction::Empty do
|
4
4
|
describe "#defined?" do
|
5
5
|
subject { described_class.defined_at?(42) }
|
6
6
|
|
@@ -8,7 +8,7 @@ RSpec.describe Fear::PartialFunction::EMPTY do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "#call" do
|
11
|
-
subject { -> { described_class.(42) } }
|
11
|
+
subject { -> { described_class.call(42) } }
|
12
12
|
|
13
13
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 42") }
|
14
14
|
end
|
@@ -23,13 +23,13 @@ RSpec.describe Fear::PartialFunction, "#and_then" do
|
|
23
23
|
|
24
24
|
describe "#call" do
|
25
25
|
context "defined" do
|
26
|
-
subject { pf_and_f.(4) }
|
26
|
+
subject { pf_and_f.call(4) }
|
27
27
|
|
28
28
|
it { is_expected.to eq("f: pf: 4") }
|
29
29
|
end
|
30
30
|
|
31
31
|
context "not defined" do
|
32
|
-
subject { -> { pf_and_f.(3) } }
|
32
|
+
subject { -> { pf_and_f.call(3) } }
|
33
33
|
|
34
34
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 3") }
|
35
35
|
end
|
@@ -86,7 +86,7 @@ RSpec.describe Fear::PartialFunction, "#and_then" do
|
|
86
86
|
|
87
87
|
describe "#call" do
|
88
88
|
context "first defined, second defined on result of first" do
|
89
|
-
subject { first_and_then_second.(6) }
|
89
|
+
subject { first_and_then_second.call(6) }
|
90
90
|
|
91
91
|
let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
|
92
92
|
let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
|
@@ -95,7 +95,7 @@ RSpec.describe Fear::PartialFunction, "#and_then" do
|
|
95
95
|
end
|
96
96
|
|
97
97
|
context "first defined, second not defined on result of first" do
|
98
|
-
subject { -> { first_and_then_second.(4) } }
|
98
|
+
subject { -> { first_and_then_second.call(4) } }
|
99
99
|
|
100
100
|
let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
|
101
101
|
let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
|
@@ -104,7 +104,7 @@ RSpec.describe Fear::PartialFunction, "#and_then" do
|
|
104
104
|
end
|
105
105
|
|
106
106
|
context "first not defined" do
|
107
|
-
subject { -> { first_and_then_second.(3) } }
|
107
|
+
subject { -> { first_and_then_second.call(3) } }
|
108
108
|
|
109
109
|
let(:first) { Fear.case(->(x) { x.even? }) { |x| "first: #{x}" } }
|
110
110
|
let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| "second: #{x}" } }
|
@@ -60,13 +60,13 @@ RSpec.describe Fear::PartialFunction do
|
|
60
60
|
it "two branches" do
|
61
61
|
first_branch = Fear.case(Integer, &:itself).and_then(Fear.case(1) { "one" })
|
62
62
|
second_branch = Fear.case(String, &:itself).and_then(
|
63
|
-
(Fear.case("zero") { 0 }).or_else(Fear.case("one") { 1 })
|
63
|
+
(Fear.case("zero") { 0 }).or_else(Fear.case("one") { 1 })
|
64
64
|
)
|
65
65
|
|
66
66
|
full = first_branch.or_else(second_branch)
|
67
|
-
expect(full.(1)).to eq("one")
|
68
|
-
expect(full.("zero")).to eq(0)
|
69
|
-
expect(full.("one")).to eq(1)
|
67
|
+
expect(full.call(1)).to eq("one")
|
68
|
+
expect(full.call("zero")).to eq(0)
|
69
|
+
expect(full.call("one")).to eq(1)
|
70
70
|
end
|
71
71
|
|
72
72
|
it "or else anh then" do
|
@@ -76,7 +76,7 @@ RSpec.describe Fear::PartialFunction do
|
|
76
76
|
|
77
77
|
f5 = f1.and_then(f3).or_else(f2)
|
78
78
|
|
79
|
-
expect(f5.(11)).to eq(10)
|
80
|
-
expect(f5.(3)).to eq(2)
|
79
|
+
expect(f5.call(11)).to eq(10)
|
80
|
+
expect(f5.call(3)).to eq(2)
|
81
81
|
end
|
82
82
|
end
|
@@ -34,25 +34,25 @@ RSpec.describe Fear::PartialFunction, "#or_else" do
|
|
34
34
|
|
35
35
|
describe "#call" do
|
36
36
|
context "first defined, second not" do
|
37
|
-
subject { first_or_else_second.(4) }
|
37
|
+
subject { first_or_else_second.call(4) }
|
38
38
|
|
39
39
|
it { is_expected.to eq("first: 4") }
|
40
40
|
end
|
41
41
|
|
42
42
|
context "first not defined, second defined" do
|
43
|
-
subject { first_or_else_second.(9) }
|
43
|
+
subject { first_or_else_second.call(9) }
|
44
44
|
|
45
45
|
it { is_expected.to eq("second: 9") }
|
46
46
|
end
|
47
47
|
|
48
48
|
context "first not defined, second not defined" do
|
49
|
-
subject { -> { first_or_else_second.(5) } }
|
49
|
+
subject { -> { first_or_else_second.call(5) } }
|
50
50
|
|
51
51
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 5") }
|
52
52
|
end
|
53
53
|
|
54
54
|
context "first and second defined" do
|
55
|
-
subject { first_or_else_second.(6) }
|
55
|
+
subject { first_or_else_second.call(6) }
|
56
56
|
|
57
57
|
it { is_expected.to eq("first: 6") }
|
58
58
|
end
|
@@ -124,31 +124,31 @@ RSpec.describe Fear::PartialFunction, "#or_else" do
|
|
124
124
|
|
125
125
|
describe "#call" do
|
126
126
|
context "first defined, second not" do
|
127
|
-
subject { first_or_else_second_or_else_third.(4) }
|
127
|
+
subject { first_or_else_second_or_else_third.call(4) }
|
128
128
|
|
129
129
|
it { is_expected.to eq("first: 4") }
|
130
130
|
end
|
131
131
|
|
132
132
|
context "first not defined, second defined" do
|
133
|
-
subject { first_or_else_second_or_else_third.(9) }
|
133
|
+
subject { first_or_else_second_or_else_third.call(9) }
|
134
134
|
|
135
135
|
it { is_expected.to eq("second: 9") }
|
136
136
|
end
|
137
137
|
|
138
138
|
context "first not defined, second not defined, third defined" do
|
139
|
-
subject { first_or_else_second_or_else_third.(7) }
|
139
|
+
subject { first_or_else_second_or_else_third.call(7) }
|
140
140
|
|
141
141
|
it { is_expected.to eq("third: 7") }
|
142
142
|
end
|
143
143
|
|
144
144
|
context "first not defined, second not defined, third not defined" do
|
145
|
-
subject { -> { first_or_else_second_or_else_third.(1) } }
|
145
|
+
subject { -> { first_or_else_second_or_else_third.call(1) } }
|
146
146
|
|
147
147
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 1") }
|
148
148
|
end
|
149
149
|
|
150
150
|
context "first, second and third defined" do
|
151
|
-
subject { first_or_else_second.(42) }
|
151
|
+
subject { first_or_else_second.call(42) }
|
152
152
|
|
153
153
|
it { is_expected.to eq("first: 42") }
|
154
154
|
end
|
@@ -221,25 +221,25 @@ RSpec.describe Fear::PartialFunction, "#or_else" do
|
|
221
221
|
|
222
222
|
describe "#call" do
|
223
223
|
context "first defined, second not" do
|
224
|
-
subject { first_or_else_second_and_then_function.(2) }
|
224
|
+
subject { first_or_else_second_and_then_function.call(2) }
|
225
225
|
|
226
226
|
it { is_expected.to eq("f: first: 2") }
|
227
227
|
end
|
228
228
|
|
229
229
|
context "first not defined, second defined" do
|
230
|
-
subject { first_or_else_second_and_then_function.(3) }
|
230
|
+
subject { first_or_else_second_and_then_function.call(3) }
|
231
231
|
|
232
232
|
it { is_expected.to eq("f: second: 3") }
|
233
233
|
end
|
234
234
|
|
235
235
|
context "first not defined, second not defined" do
|
236
|
-
subject { -> { first_or_else_second_and_then_function.(5) } }
|
236
|
+
subject { -> { first_or_else_second_and_then_function.call(5) } }
|
237
237
|
|
238
238
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 5") }
|
239
239
|
end
|
240
240
|
|
241
241
|
context "first defined, second defined" do
|
242
|
-
subject { first_or_else_second_and_then_function.(6) }
|
242
|
+
subject { first_or_else_second_and_then_function.call(6) }
|
243
243
|
|
244
244
|
it { is_expected.to eq("f: first: 6") }
|
245
245
|
end
|
@@ -80,13 +80,13 @@ RSpec.describe Fear::PartialFunction do
|
|
80
80
|
let(:partial_function) { Fear.case(->(v) { v != 0 }) { |x| 4 / x } }
|
81
81
|
|
82
82
|
context "defined" do
|
83
|
-
subject { lifted.(2) }
|
83
|
+
subject { lifted.call(2) }
|
84
84
|
|
85
85
|
it { is_expected.to eq(Fear::Some.new(2)) }
|
86
86
|
end
|
87
87
|
|
88
88
|
context "not defined" do
|
89
|
-
subject { lifted.(0) }
|
89
|
+
subject { lifted.call(0) }
|
90
90
|
|
91
91
|
it { is_expected.to eq(Fear::None) }
|
92
92
|
end
|
@@ -108,13 +108,13 @@ RSpec.describe Fear::PartialFunction do
|
|
108
108
|
let(:partial_function) { Fear.case(->(v) { v != 0 }) { |x| 4 / x } }
|
109
109
|
|
110
110
|
context "defined" do
|
111
|
-
subject { partial_function.(2) }
|
111
|
+
subject { partial_function.call(2) }
|
112
112
|
|
113
113
|
it { is_expected.to eq(2) }
|
114
114
|
end
|
115
115
|
|
116
116
|
context "not defined" do
|
117
|
-
subject { -> { partial_function.(0) } }
|
117
|
+
subject { -> { partial_function.call(0) } }
|
118
118
|
|
119
119
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 0") }
|
120
120
|
end
|
@@ -124,13 +124,13 @@ RSpec.describe Fear::PartialFunction do
|
|
124
124
|
let(:partial_function) { Fear.case(->(v) { v != 0 }) { |x| 4 / x }.to_proc }
|
125
125
|
|
126
126
|
context "defined" do
|
127
|
-
subject { partial_function.(2) }
|
127
|
+
subject { partial_function.call(2) }
|
128
128
|
|
129
129
|
it { is_expected.to eq(2) }
|
130
130
|
end
|
131
131
|
|
132
132
|
context "not defined" do
|
133
|
-
subject { -> { partial_function.(0) } }
|
133
|
+
subject { -> { partial_function.call(0) } }
|
134
134
|
|
135
135
|
it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 0") }
|
136
136
|
end
|
@@ -189,7 +189,7 @@ RSpec.describe Fear::PartialFunction do
|
|
189
189
|
end
|
190
190
|
|
191
191
|
shared_examples "#or_else" do |method_name|
|
192
|
-
subject { is_even.__send__(method_name, is_odd).(value) }
|
192
|
+
subject { is_even.__send__(method_name, is_odd).call(value) }
|
193
193
|
|
194
194
|
let(:is_even) { Fear.case(:even?.to_proc) { |x| "#{x} is even" } }
|
195
195
|
let(:is_odd) { Fear.case(:odd?.to_proc) { |x| "#{x} is odd" } }
|
@@ -11,26 +11,26 @@ RSpec.describe Fear::PatternMatch do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
context "Integer" do
|
14
|
-
subject { matcher.(4) }
|
14
|
+
subject { matcher.call(4) }
|
15
15
|
|
16
16
|
it { is_expected.to eq("4 is int") }
|
17
17
|
end
|
18
18
|
|
19
19
|
context "String" do
|
20
|
-
subject { matcher.("4") }
|
20
|
+
subject { matcher.call("4") }
|
21
21
|
|
22
22
|
it { is_expected.to eq("4 is str") }
|
23
23
|
end
|
24
24
|
|
25
25
|
context "Symbol" do
|
26
|
-
subject { matcher.(:a) }
|
26
|
+
subject { matcher.call(:a) }
|
27
27
|
|
28
28
|
it { is_expected.to eq("a is something else") }
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
context "else before other branches" do
|
33
|
-
subject { matcher.(4) }
|
33
|
+
subject { matcher.call(4) }
|
34
34
|
|
35
35
|
let(:matcher) do
|
36
36
|
described_class.new do |m|
|
@@ -43,7 +43,7 @@ RSpec.describe Fear::PatternMatch do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
context "several else branches" do
|
46
|
-
subject { matcher.(4) }
|
46
|
+
subject { matcher.call(4) }
|
47
47
|
|
48
48
|
let(:matcher) do
|
49
49
|
described_class.new do |m|
|