fear 0.11.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.rubocop.yml +18 -0
- data/.travis.yml +0 -3
- data/CHANGELOG.md +12 -1
- data/Gemfile +1 -0
- data/{gemfiles/dry_equalizer_0.2.1.gemfile.lock → Gemfile.lock} +21 -12
- data/README.md +594 -241
- data/Rakefile +166 -219
- data/benchmarks/README.md +1 -0
- data/benchmarks/dry_do_vs_fear_for.txt +11 -0
- data/benchmarks/dry_some_fmap_vs_fear_some_map.txt +11 -0
- data/benchmarks/factorial.txt +16 -0
- data/benchmarks/fear_gaurd_and1_vs_new.txt +13 -0
- data/benchmarks/fear_gaurd_and2_vs_and.txt +13 -0
- data/benchmarks/fear_gaurd_and3_vs_and_and.txt +13 -0
- data/benchmarks/fear_pattern_extracting_with_vs_without_cache.txt +11 -0
- data/benchmarks/fear_pattern_matching_construction_vs_execution.txt +13 -0
- data/benchmarks/pattern_matching_dry_vs_qo_vs_fear_try.txt +14 -0
- data/benchmarks/pattern_matching_qo_vs_fear_pattern_extraction.txt +11 -0
- data/benchmarks/pattern_matching_qo_vs_fear_try_execution.txt +11 -0
- data/examples/pattern_extracting.rb +15 -0
- data/examples/pattern_matching_binary_tree_set.rb +96 -0
- data/examples/pattern_matching_number_in_words.rb +54 -0
- data/fear.gemspec +4 -2
- data/lib/fear.rb +21 -4
- data/lib/fear/either.rb +77 -59
- data/lib/fear/either_api.rb +21 -0
- data/lib/fear/empty_partial_function.rb +1 -1
- data/lib/fear/extractor.rb +108 -0
- data/lib/fear/extractor/anonymous_array_splat_matcher.rb +8 -0
- data/lib/fear/extractor/any_matcher.rb +15 -0
- data/lib/fear/extractor/array_head_matcher.rb +34 -0
- data/lib/fear/extractor/array_matcher.rb +38 -0
- data/lib/fear/extractor/array_splat_matcher.rb +14 -0
- data/lib/fear/extractor/empty_list_matcher.rb +18 -0
- data/lib/fear/extractor/extractor_matcher.rb +42 -0
- data/lib/fear/extractor/grammar.rb +201 -0
- data/lib/fear/extractor/grammar.treetop +129 -0
- data/lib/fear/extractor/identifier_matcher.rb +16 -0
- data/lib/fear/extractor/matcher.rb +54 -0
- data/lib/fear/extractor/matcher/and.rb +36 -0
- data/lib/fear/extractor/named_array_splat_matcher.rb +15 -0
- data/lib/fear/extractor/pattern.rb +55 -0
- data/lib/fear/extractor/typed_identifier_matcher.rb +24 -0
- data/lib/fear/extractor/value_matcher.rb +17 -0
- data/lib/fear/extractor_api.rb +33 -0
- data/lib/fear/failure.rb +32 -10
- data/lib/fear/for.rb +14 -69
- data/lib/fear/for_api.rb +66 -0
- data/lib/fear/future.rb +414 -0
- data/lib/fear/future_api.rb +19 -0
- data/lib/fear/left.rb +8 -0
- data/lib/fear/none.rb +17 -8
- data/lib/fear/option.rb +55 -49
- data/lib/fear/option_api.rb +38 -0
- data/lib/fear/partial_function.rb +9 -12
- data/lib/fear/partial_function/empty.rb +1 -1
- data/lib/fear/partial_function/guard.rb +8 -20
- data/lib/fear/partial_function/lifted.rb +1 -0
- data/lib/fear/partial_function_class.rb +10 -0
- data/lib/fear/pattern_match.rb +10 -0
- data/lib/fear/pattern_matching_api.rb +35 -11
- data/lib/fear/promise.rb +87 -0
- data/lib/fear/right.rb +8 -0
- data/lib/fear/some.rb +22 -3
- data/lib/fear/success.rb +22 -1
- data/lib/fear/try.rb +82 -67
- data/lib/fear/try_api.rb +31 -0
- data/lib/fear/unit.rb +28 -0
- data/lib/fear/version.rb +1 -1
- data/spec/fear/done_spec.rb +3 -3
- data/spec/fear/either/mixin_spec.rb +15 -0
- data/spec/fear/either_pattern_match_spec.rb +10 -12
- data/spec/fear/extractor/array_matcher_spec.rb +228 -0
- data/spec/fear/extractor/extractor_matcher_spec.rb +151 -0
- data/spec/fear/extractor/grammar_array_spec.rb +23 -0
- data/spec/fear/extractor/identified_matcher_spec.rb +47 -0
- data/spec/fear/extractor/identifier_matcher_spec.rb +66 -0
- data/spec/fear/extractor/pattern_spec.rb +32 -0
- data/spec/fear/extractor/typed_identifier_matcher_spec.rb +62 -0
- data/spec/fear/extractor/value_matcher_number_spec.rb +77 -0
- data/spec/fear/extractor/value_matcher_string_spec.rb +86 -0
- data/spec/fear/extractor/value_matcher_symbol_spec.rb +69 -0
- data/spec/fear/extractor_api_spec.rb +113 -0
- data/spec/fear/extractor_spec.rb +59 -0
- data/spec/fear/failure_spec.rb +73 -13
- data/spec/fear/for_spec.rb +35 -35
- data/spec/fear/future_spec.rb +466 -0
- data/spec/fear/guard_spec.rb +4 -4
- data/spec/fear/left_spec.rb +40 -14
- data/spec/fear/none_spec.rb +28 -12
- data/spec/fear/option/mixin_spec.rb +37 -0
- data/spec/fear/option_pattern_match_spec.rb +7 -9
- data/spec/fear/partial_function_spec.rb +25 -3
- data/spec/fear/pattern_match_spec.rb +33 -1
- data/spec/fear/promise_spec.rb +94 -0
- data/spec/fear/right_spec.rb +37 -9
- data/spec/fear/some_spec.rb +32 -6
- data/spec/fear/success_spec.rb +32 -4
- data/spec/fear/try/mixin_spec.rb +17 -0
- data/spec/fear/try_pattern_match_spec.rb +8 -10
- data/spec/spec_helper.rb +1 -1
- metadata +115 -20
- data/Appraisals +0 -32
- data/gemfiles/dry_equalizer_0.1.0.gemfile +0 -8
- data/gemfiles/dry_equalizer_0.1.0.gemfile.lock +0 -82
- data/gemfiles/dry_equalizer_0.2.1.gemfile +0 -8
- data/lib/fear/done.rb +0 -22
- data/spec/fear/option_spec.rb +0 -15
@@ -0,0 +1,19 @@
|
|
1
|
+
module Fear
|
2
|
+
# rubocop: disable Metrics/LineLength
|
3
|
+
module FutureApi
|
4
|
+
# Asynchronously evaluates the block
|
5
|
+
# @param options [Hash] options will be passed directly to underlying +Concurrent::Promise+
|
6
|
+
# @see https://ruby-concurrency.github.io/concurrent-ruby/1.1.5/Concurrent/Promise.html#constructor_details Constructor Details
|
7
|
+
# @return [Fear::Future]
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# require 'open-uri'
|
11
|
+
# f = Fear.future(executor: :io) { open('http://example.com') }
|
12
|
+
# f.map(&:read).each { |body| puts body }
|
13
|
+
#
|
14
|
+
def future(options = {}, &block)
|
15
|
+
Future.new(options, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
# rubocop: enable Metrics/LineLength
|
19
|
+
end
|
data/lib/fear/left.rb
CHANGED
data/lib/fear/none.rb
CHANGED
@@ -2,10 +2,17 @@ module Fear
|
|
2
2
|
# @api private
|
3
3
|
class NoneClass
|
4
4
|
include Option
|
5
|
-
include Dry::Equalizer()
|
6
5
|
include RightBiased::Left
|
7
6
|
include NonePatternMatch.mixin
|
8
7
|
|
8
|
+
EXTRACTOR = proc do |option|
|
9
|
+
if Fear::None === option
|
10
|
+
Fear.some([])
|
11
|
+
else
|
12
|
+
Fear.none
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
9
16
|
# @raise [NoSuchElementError]
|
10
17
|
def get
|
11
18
|
raise NoSuchElementError
|
@@ -31,16 +38,18 @@ module Fear
|
|
31
38
|
self
|
32
39
|
end
|
33
40
|
|
34
|
-
AS_STRING = 'Fear::None'.freeze
|
35
|
-
|
36
41
|
# @return [String]
|
37
|
-
def
|
38
|
-
|
42
|
+
def inspect
|
43
|
+
'#<Fear::NoneClass>'
|
39
44
|
end
|
40
45
|
|
41
46
|
# @return [String]
|
42
|
-
|
43
|
-
|
47
|
+
alias to_s inspect
|
48
|
+
|
49
|
+
# @param other [Any]
|
50
|
+
# @return [Boolean]
|
51
|
+
def ==(other)
|
52
|
+
other.is_a?(NoneClass)
|
44
53
|
end
|
45
54
|
|
46
55
|
# @param other
|
@@ -60,7 +69,7 @@ module Fear
|
|
60
69
|
None
|
61
70
|
end
|
62
71
|
|
63
|
-
def inherited
|
72
|
+
def inherited(*)
|
64
73
|
raise 'you are not allowed to inherit from NoneClass, use Fear::None instead'
|
65
74
|
end
|
66
75
|
end
|
data/lib/fear/option.rb
CHANGED
@@ -3,7 +3,7 @@ module Fear
|
|
3
3
|
# are either an instance of +Some+ or the object +None+.
|
4
4
|
#
|
5
5
|
# @example The most idiomatic way to use an +Option+ instance is to treat it as a collection
|
6
|
-
# name =
|
6
|
+
# name = Fear.option(params[:name])
|
7
7
|
# upper = name.map(&:strip).select { |n| n.length != 0 }.map(&:upcase)
|
8
8
|
# puts upper.get_or_else('')
|
9
9
|
#
|
@@ -11,13 +11,13 @@ module Fear
|
|
11
11
|
# having to check for the existence of a value.
|
12
12
|
#
|
13
13
|
# @example A less-idiomatic way to use +Option+ values is via pattern matching
|
14
|
-
#
|
14
|
+
# Fear.option(params[:name]).match do |m|
|
15
15
|
# m.some { |name| name.strip.upcase }
|
16
16
|
# m.none { 'No name value' }
|
17
17
|
# end
|
18
18
|
#
|
19
19
|
# @example or manually checking for non emptiness
|
20
|
-
# name =
|
20
|
+
# name = Fear.option(params[:name])
|
21
21
|
# if name.empty?
|
22
22
|
# puts 'No name value'
|
23
23
|
# else
|
@@ -31,21 +31,21 @@ module Fear
|
|
31
31
|
# @yieldreturn [any]
|
32
32
|
# @return [any]
|
33
33
|
# @example
|
34
|
-
#
|
35
|
-
#
|
34
|
+
# Fear.some(42).get_or_else { 24/2 } #=> 42
|
35
|
+
# Fear.none.get_or_else { 24/2 } #=> 12
|
36
36
|
# @overload get_or_else(default)
|
37
37
|
# @return [any]
|
38
38
|
# @example
|
39
|
-
#
|
40
|
-
#
|
39
|
+
# Fear.some(42).get_or_else(12) #=> 42
|
40
|
+
# Fear.none.get_or_else(12) #=> 12
|
41
41
|
#
|
42
42
|
# @!method or_else(&alternative)
|
43
43
|
# Returns this +Some+ or the given alternative if this is a +None+.
|
44
44
|
# @return [Option]
|
45
45
|
# @example
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
46
|
+
# Fear.some(42).or_else { Fear.some(21) } #=> Fear.some(42)
|
47
|
+
# Fear.none.or_else { Fear.some(21) } #=> Fear.some(21)
|
48
|
+
# Fear.none.or_else { None } #=> None
|
49
49
|
#
|
50
50
|
# @!method include?(other_value)
|
51
51
|
# Returns +true+ if it has an element that is equal
|
@@ -53,9 +53,9 @@ module Fear
|
|
53
53
|
# @param [any]
|
54
54
|
# @return [Boolean]
|
55
55
|
# @example
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
56
|
+
# Fear.some(17).include?(17) #=> true
|
57
|
+
# Fear.some(17).include?(7) #=> false
|
58
|
+
# Fear.none.include?(17) #=> false
|
59
59
|
#
|
60
60
|
# @!method each(&block)
|
61
61
|
# Performs the given block if this is a +Some+.
|
@@ -63,11 +63,11 @@ module Fear
|
|
63
63
|
# @yieldreturn [void]
|
64
64
|
# @return [Option] itself
|
65
65
|
# @example
|
66
|
-
#
|
66
|
+
# Fear.some(17).each do |value|
|
67
67
|
# puts value
|
68
68
|
# end #=> prints 17
|
69
69
|
#
|
70
|
-
#
|
70
|
+
# Fear.none.each do |value|
|
71
71
|
# puts value
|
72
72
|
# end #=> does nothing
|
73
73
|
#
|
@@ -77,8 +77,8 @@ module Fear
|
|
77
77
|
# @yieldparam [any] value
|
78
78
|
# @yieldreturn [any]
|
79
79
|
# @example
|
80
|
-
#
|
81
|
-
#
|
80
|
+
# Fear.some(42).map { |v| v/2 } #=> Fear.some(21)
|
81
|
+
# Fear.none.map { |v| v/2 } #=> None
|
82
82
|
#
|
83
83
|
# @!method flat_map(&block)
|
84
84
|
# Returns the given block applied to the value from this +Some+
|
@@ -87,8 +87,8 @@ module Fear
|
|
87
87
|
# @yieldreturn [Option]
|
88
88
|
# @return [Option]
|
89
89
|
# @example
|
90
|
-
#
|
91
|
-
#
|
90
|
+
# Fear.some(42).flat_map { |v| Fear.some(v/2) } #=> Fear.some(21)
|
91
|
+
# Fear.none.flat_map { |v| Fear.some(v/2) } #=> None
|
92
92
|
#
|
93
93
|
# @!method any?(&predicate)
|
94
94
|
# Returns +false+ if +None+ or returns the result of the
|
@@ -97,9 +97,9 @@ module Fear
|
|
97
97
|
# @yieldreturn [Boolean]
|
98
98
|
# @return [Boolean]
|
99
99
|
# @example
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
100
|
+
# Fear.some(12).any?( |v| v > 10) #=> true
|
101
|
+
# Fear.some(7).any?( |v| v > 10) #=> false
|
102
|
+
# Fear.none.any?( |v| v > 10) #=> false
|
103
103
|
#
|
104
104
|
# @!method select(&predicate)
|
105
105
|
# Returns self if it is nonempty and applying the predicate to this
|
@@ -108,9 +108,9 @@ module Fear
|
|
108
108
|
# @yieldreturn [Boolean]
|
109
109
|
# @return [Option]
|
110
110
|
# @example
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
111
|
+
# Fear.some(42).select { |v| v > 40 } #=> Fear.success(21)
|
112
|
+
# Fear.some(42).select { |v| v < 40 } #=> None
|
113
|
+
# Fear.none.select { |v| v < 40 } #=> None
|
114
114
|
#
|
115
115
|
# @!method reject(&predicate)
|
116
116
|
# Returns +Some+ if applying the predicate to this
|
@@ -119,9 +119,9 @@ module Fear
|
|
119
119
|
# @yieldreturn [Boolean]
|
120
120
|
# @return [Option]
|
121
121
|
# @example
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
122
|
+
# Fear.some(42).reject { |v| v > 40 } #=> None
|
123
|
+
# Fear.some(42).reject { |v| v < 40 } #=> Fear.some(42)
|
124
|
+
# Fear.none.reject { |v| v < 40 } #=> None
|
125
125
|
#
|
126
126
|
# @!method get
|
127
127
|
# @return [any] the +Option+'s value.
|
@@ -131,14 +131,14 @@ module Fear
|
|
131
131
|
# Returns +true+ if the +Option+ is +None+, +false+ otherwise.
|
132
132
|
# @return [Boolean]
|
133
133
|
# @example
|
134
|
-
#
|
135
|
-
#
|
134
|
+
# Fear.some(42).empty? #=> false
|
135
|
+
# Fear.none.empty? #=> true
|
136
136
|
#
|
137
137
|
# @!method match(&matcher)
|
138
138
|
# Pattern match against this +Option+
|
139
139
|
# @yield matcher [Fear::OptionPatternMatch]
|
140
140
|
# @example
|
141
|
-
#
|
141
|
+
# Fear.option(val).match do |m|
|
142
142
|
# m.some(Integer) do |x|
|
143
143
|
# x * 2
|
144
144
|
# end
|
@@ -166,8 +166,8 @@ module Fear
|
|
166
166
|
|
167
167
|
class << self
|
168
168
|
# Build pattern matcher to be used later, despite off
|
169
|
-
# +Option#match+ method,
|
170
|
-
# but build it instead.
|
169
|
+
# +Option#match+ method, it doesn't apply matcher immanently,
|
170
|
+
# but build it instead. Usually in sake of efficiency it's better
|
171
171
|
# to statically build matcher and reuse it later.
|
172
172
|
#
|
173
173
|
# @example
|
@@ -178,52 +178,58 @@ module Fear
|
|
178
178
|
# m.none { 'NaN' }
|
179
179
|
# m.else { 'error '}
|
180
180
|
# end
|
181
|
-
# matcher.call(
|
181
|
+
# matcher.call(Fear.some(42))
|
182
182
|
#
|
183
183
|
# @yieldparam [OptionPatternMatch]
|
184
184
|
# @return [Fear::PartialFunction]
|
185
185
|
def matcher(&matcher)
|
186
186
|
OptionPatternMatch.new(&matcher)
|
187
187
|
end
|
188
|
+
|
189
|
+
def match(value, &block)
|
190
|
+
matcher(&block).call(value)
|
191
|
+
end
|
188
192
|
end
|
189
193
|
|
190
194
|
# Include this mixin to access convenient factory methods.
|
191
195
|
# @example
|
192
196
|
# include Fear::Option::Mixin
|
193
197
|
#
|
194
|
-
# Option(17)
|
198
|
+
# Option(17) #=> #<Fear::Some get=17>
|
195
199
|
# Option(nil) #=> #<Fear::None>
|
196
|
-
# Some(17)
|
197
|
-
# None
|
200
|
+
# Some(17) #=> #<Fear::Some get=17>
|
201
|
+
# None() #=> #<Fear::None>
|
198
202
|
#
|
199
203
|
module Mixin
|
200
|
-
None = Fear::None
|
201
|
-
|
202
204
|
# An +Option+ factory which creates +Some+ if the argument is
|
203
205
|
# not +nil+, and +None+ if it is +nil+.
|
204
206
|
# @param value [any]
|
205
|
-
# @return [Some, None]
|
207
|
+
# @return [Fear::Some, Fear::None]
|
206
208
|
#
|
207
209
|
# @example
|
208
|
-
# Option(
|
210
|
+
# Option(17) #=> #<Fear::Some get=17>
|
211
|
+
# Option(nil) #=> #<Fear::None>
|
209
212
|
#
|
210
213
|
def Option(value)
|
211
|
-
|
212
|
-
None
|
213
|
-
else
|
214
|
-
Some(value)
|
215
|
-
end
|
214
|
+
Fear.option(value)
|
216
215
|
end
|
217
216
|
|
218
217
|
# @return [None]
|
218
|
+
# @example
|
219
|
+
# None() #=> #<Fear::None>
|
220
|
+
#
|
219
221
|
def None
|
220
|
-
Fear
|
222
|
+
Fear.none
|
221
223
|
end
|
222
224
|
|
223
225
|
# @param value [any] except nil
|
224
|
-
# @return [
|
226
|
+
# @return [Fear::Some]
|
227
|
+
# @example
|
228
|
+
# Some(17) #=> #<Fear::Some get=17>
|
229
|
+
# Some(nil) #=> #<Fear::Some get=nil>
|
230
|
+
#
|
225
231
|
def Some(value)
|
226
|
-
|
232
|
+
Fear.some(value)
|
227
233
|
end
|
228
234
|
end
|
229
235
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Fear
|
2
|
+
module OptionApi
|
3
|
+
# An +Option+ factory which creates +Some+ if the argument is
|
4
|
+
# not +nil+, and +None+ if it is +nil+.
|
5
|
+
# @param value [any]
|
6
|
+
# @return [Fear::Some, Fear::None]
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# Fear.option(nil) #=> #<Fear::None>
|
10
|
+
# Fear.option(17) #=> #<Fear::Some get=17>
|
11
|
+
#
|
12
|
+
def option(value)
|
13
|
+
if value.nil?
|
14
|
+
none
|
15
|
+
else
|
16
|
+
some(value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Fear::None]
|
21
|
+
# @example
|
22
|
+
# Fear.none #=> #<Fear::None>
|
23
|
+
#
|
24
|
+
def none
|
25
|
+
Fear::None
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param value [any]
|
29
|
+
# @return [Fear::Some]
|
30
|
+
# @example
|
31
|
+
# Fear.some(17) #=> #<Fear::Some get=17>
|
32
|
+
# Fear.some(nil) #=> #<Fear::Some get=nil>
|
33
|
+
#
|
34
|
+
def some(value)
|
35
|
+
Fear::Some.new(value)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -35,6 +35,13 @@ module Fear
|
|
35
35
|
# numbers = sample.map(is_even.or_else(is_odd).to_proc)
|
36
36
|
#
|
37
37
|
# @see https://github.com/scala/scala/commit/5050915eb620af3aa43d6ddaae6bbb83ad74900d
|
38
|
+
# @!method condition
|
39
|
+
# describes the domain of partial function
|
40
|
+
# @return [#===]
|
41
|
+
# @abstract
|
42
|
+
# @!method function
|
43
|
+
# @return [#call]
|
44
|
+
# @abstract
|
38
45
|
module PartialFunction
|
39
46
|
autoload :AndThen, 'fear/partial_function/and_then'
|
40
47
|
autoload :Any, 'fear/partial_function/any'
|
@@ -44,16 +51,6 @@ module Fear
|
|
44
51
|
autoload :Lifted, 'fear/partial_function/lifted'
|
45
52
|
autoload :OrElse, 'fear/partial_function/or_else'
|
46
53
|
|
47
|
-
# @param condition [#call] describes the domain of partial function
|
48
|
-
# @param function [Proc] function definition
|
49
|
-
def initialize(condition, &function)
|
50
|
-
@condition = condition
|
51
|
-
@function = function
|
52
|
-
end
|
53
|
-
attr_reader :condition, :function
|
54
|
-
private :condition
|
55
|
-
private :function
|
56
|
-
|
57
54
|
# Checks if a value is contained in the function's domain.
|
58
55
|
#
|
59
56
|
# @param arg [any]
|
@@ -151,7 +148,7 @@ module Fear
|
|
151
148
|
class << self
|
152
149
|
# Creates partial function guarded by several condition.
|
153
150
|
# All guards should match.
|
154
|
-
# @param guards [
|
151
|
+
# @param guards [<#===>]
|
155
152
|
# @param function [Proc]
|
156
153
|
# @return [Fear::PartialFunction]
|
157
154
|
def and(*guards, &function)
|
@@ -160,7 +157,7 @@ module Fear
|
|
160
157
|
|
161
158
|
# Creates partial function guarded by several condition.
|
162
159
|
# Any condition should match.
|
163
|
-
# @param guards [
|
160
|
+
# @param guards [<#===>]
|
164
161
|
# @param function [Proc]
|
165
162
|
# @return [Fear::PartialFunction]
|
166
163
|
def or(*guards, &function)
|
@@ -11,28 +11,21 @@ module Fear
|
|
11
11
|
class << self
|
12
12
|
# Optimized version for combination of two guardians
|
13
13
|
# Two guarding is a very common situation. For example checking for Some, and checking
|
14
|
-
# a value withing
|
14
|
+
# a value withing container.
|
15
15
|
#
|
16
16
|
def and2(c1, c2)
|
17
|
-
Guard::And.new(
|
18
|
-
(c1.is_a?(Symbol) ? c1.to_proc : c1),
|
19
|
-
(c2.is_a?(Symbol) ? c2.to_proc : c2),
|
20
|
-
)
|
17
|
+
Guard::And.new(c1, c2)
|
21
18
|
end
|
22
19
|
|
23
20
|
def and3(c1, c2, c3)
|
24
|
-
Guard::And3.new(
|
25
|
-
(c1.is_a?(Symbol) ? c1.to_proc : c1),
|
26
|
-
(c2.is_a?(Symbol) ? c2.to_proc : c2),
|
27
|
-
(c3.is_a?(Symbol) ? c3.to_proc : c3),
|
28
|
-
)
|
21
|
+
Guard::And3.new(c1, c2, c3)
|
29
22
|
end
|
30
23
|
|
31
24
|
def and1(c)
|
32
|
-
c
|
25
|
+
c
|
33
26
|
end
|
34
27
|
|
35
|
-
# @param conditions [
|
28
|
+
# @param conditions [<#===>]
|
36
29
|
# @return [Fear::PartialFunction::Guard]
|
37
30
|
def and(conditions)
|
38
31
|
case conditions.size
|
@@ -46,7 +39,7 @@ module Fear
|
|
46
39
|
end
|
47
40
|
end
|
48
41
|
|
49
|
-
# @param conditions [
|
42
|
+
# @param conditions [<#===>]
|
50
43
|
# @return [Fear::PartialFunction::Guard]
|
51
44
|
def or(conditions)
|
52
45
|
return Any if conditions.empty?
|
@@ -56,14 +49,9 @@ module Fear
|
|
56
49
|
end
|
57
50
|
end
|
58
51
|
|
59
|
-
# @param condition [
|
52
|
+
# @param condition [#===]
|
60
53
|
def initialize(condition)
|
61
|
-
@condition =
|
62
|
-
if condition.is_a?(Symbol)
|
63
|
-
condition.to_proc
|
64
|
-
else
|
65
|
-
condition
|
66
|
-
end
|
54
|
+
@condition = condition
|
67
55
|
end
|
68
56
|
attr_reader :condition
|
69
57
|
private :condition
|