fear 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/spec.yml +1 -5
  3. data/CHANGELOG.md +8 -0
  4. data/Gemfile +14 -2
  5. data/Gemfile.lock +48 -61
  6. data/LICENSE.txt +1 -1
  7. data/README.md +18 -70
  8. data/Rakefile +32 -119
  9. data/benchmarks/dry_do_vs_fear_for.txt +7 -6
  10. data/benchmarks/dry_some_fmap_vs_fear_some_map.txt +7 -6
  11. data/benchmarks/factorial.txt +7 -9
  12. data/benchmarks/fear_gaurd_and1_vs_new.txt +7 -6
  13. data/benchmarks/fear_gaurd_and2_vs_and.txt +8 -7
  14. data/benchmarks/fear_gaurd_and3_vs_and_and.txt +7 -6
  15. data/benchmarks/fear_pattern_matching_construction_vs_execution.txt +7 -6
  16. data/benchmarks/pattern_matching_dry_vs_qo_vs_fear_try.txt +8 -10
  17. data/fear.gemspec +4 -19
  18. data/lib/fear/either/left_projection.rb +237 -0
  19. data/lib/fear/either/pattern_match.rb +49 -0
  20. data/lib/fear/either.rb +20 -11
  21. data/lib/fear/either_api.rb +2 -4
  22. data/lib/fear/empty_partial_function.rb +1 -1
  23. data/lib/fear/failure/pattern_match.rb +14 -0
  24. data/lib/fear/failure.rb +1 -1
  25. data/lib/fear/for_api.rb +0 -2
  26. data/lib/fear/future.rb +1 -1
  27. data/lib/fear/future_api.rb +0 -5
  28. data/lib/fear/left/pattern_match.rb +15 -0
  29. data/lib/fear/left.rb +1 -1
  30. data/lib/fear/none.rb +0 -87
  31. data/lib/fear/none_class/pattern_match.rb +16 -0
  32. data/lib/fear/none_class.rb +85 -0
  33. data/lib/fear/option/pattern_match.rb +47 -0
  34. data/lib/fear/option.rb +1 -5
  35. data/lib/fear/option_api.rb +1 -3
  36. data/lib/fear/partial_function/empty.rb +3 -5
  37. data/lib/fear/partial_function/guard.rb +0 -4
  38. data/lib/fear/partial_function/or_else.rb +0 -2
  39. data/lib/fear/partial_function.rb +0 -9
  40. data/lib/fear/partial_function_class.rb +1 -1
  41. data/lib/fear/pattern_match.rb +3 -2
  42. data/lib/fear/pattern_matching_api.rb +0 -3
  43. data/lib/fear/right/pattern_match.rb +15 -0
  44. data/lib/fear/right.rb +1 -1
  45. data/lib/fear/right_biased.rb +2 -0
  46. data/lib/fear/some/pattern_match.rb +15 -0
  47. data/lib/fear/some.rb +1 -1
  48. data/lib/fear/success/pattern_match.rb +16 -0
  49. data/lib/fear/success.rb +1 -1
  50. data/lib/fear/try/pattern_match.rb +29 -0
  51. data/lib/fear/try.rb +3 -7
  52. data/lib/fear/try_api.rb +0 -2
  53. data/lib/fear/version.rb +1 -1
  54. data/lib/fear.rb +3 -14
  55. data/spec/fear/awaitable_spec.rb +0 -2
  56. data/spec/fear/either/left_projection_spec.rb +289 -0
  57. data/spec/fear/{either_pattern_match_spec.rb → either/pattern_match_spec.rb} +1 -1
  58. data/spec/fear/{option_pattern_match_spec.rb → option/pattern_match_spec.rb} +1 -1
  59. data/spec/fear/partial_function/empty_spec.rb +1 -1
  60. data/spec/fear/{try_pattern_match_spec.rb → try/try_pattern_match_spec.rb} +1 -1
  61. data/spec/support/.keep +0 -0
  62. metadata +29 -255
  63. data/benchmarks/fear_pattern_extracting_with_vs_without_cache.txt +0 -11
  64. data/benchmarks/pattern_matching_qo_vs_fear_pattern_extraction.txt +0 -11
  65. data/benchmarks/pattern_matching_qo_vs_fear_try_execution.txt +0 -11
  66. data/lib/dry/types/fear/option.rb +0 -125
  67. data/lib/dry/types/fear.rb +0 -8
  68. data/lib/fear/either_pattern_match.rb +0 -52
  69. data/lib/fear/failure_pattern_match.rb +0 -12
  70. data/lib/fear/left_pattern_match.rb +0 -11
  71. data/lib/fear/none_pattern_match.rb +0 -14
  72. data/lib/fear/option_pattern_match.rb +0 -50
  73. data/lib/fear/right_pattern_match.rb +0 -13
  74. data/lib/fear/some_pattern_match.rb +0 -13
  75. data/lib/fear/struct.rb +0 -237
  76. data/lib/fear/success_pattern_match.rb +0 -14
  77. data/lib/fear/try_pattern_match.rb +0 -32
  78. data/spec/dry/types/fear/option/constrained_spec.rb +0 -22
  79. data/spec/dry/types/fear/option/core_spec.rb +0 -77
  80. data/spec/dry/types/fear/option/default_spec.rb +0 -21
  81. data/spec/dry/types/fear/option/hash_spec.rb +0 -58
  82. data/spec/dry/types/fear/option/option_spec.rb +0 -97
  83. data/spec/struct_pattern_matching_spec.rb +0 -36
  84. data/spec/struct_spec.rb +0 -194
  85. 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::EitherPatternMatch do
3
+ RSpec.describe Fear::Either::PatternMatch do
4
4
  context "Right" do
5
5
  let(:matcher) do
6
6
  described_class.new do |m|
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe Fear::OptionPatternMatch do
3
+ RSpec.describe Fear::Option::PatternMatch do
4
4
  context "Some" do
5
5
  let(:matcher) do
6
6
  described_class.new do |m|
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe Fear::PartialFunction::EMPTY do
3
+ RSpec.describe Fear::PartialFunction::Empty do
4
4
  describe "#defined?" do
5
5
  subject { described_class.defined_at?(42) }
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe Fear::TryPatternMatch do
3
+ RSpec.describe Fear::Try::PatternMatch do
4
4
  context "Success" do
5
5
  let(:matcher) do
6
6
  described_class.new do |m|
File without changes