fear 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -60
- data/.travis.yml +8 -4
- data/CHANGELOG.md +7 -1
- data/Gemfile +5 -3
- data/Gemfile.lock +18 -20
- data/README.md +28 -14
- data/Rakefile +61 -60
- data/examples/pattern_extracting.rb +8 -6
- data/examples/pattern_matching_binary_tree_set.rb +4 -2
- data/examples/pattern_matching_number_in_words.rb +46 -42
- data/fear.gemspec +29 -27
- data/lib/fear.rb +44 -37
- data/lib/fear/await.rb +33 -0
- data/lib/fear/awaitable.rb +28 -0
- data/lib/fear/either.rb +2 -0
- data/lib/fear/either_api.rb +2 -0
- data/lib/fear/either_pattern_match.rb +2 -0
- data/lib/fear/empty_partial_function.rb +3 -1
- data/lib/fear/extractor.rb +30 -28
- data/lib/fear/extractor/anonymous_array_splat_matcher.rb +2 -0
- data/lib/fear/extractor/any_matcher.rb +2 -0
- data/lib/fear/extractor/array_head_matcher.rb +2 -0
- data/lib/fear/extractor/array_matcher.rb +2 -0
- data/lib/fear/extractor/array_splat_matcher.rb +2 -0
- data/lib/fear/extractor/empty_list_matcher.rb +2 -0
- data/lib/fear/extractor/extractor_matcher.rb +5 -3
- data/lib/fear/extractor/grammar.rb +5 -3
- data/lib/fear/extractor/identifier_matcher.rb +2 -0
- data/lib/fear/extractor/matcher.rb +5 -3
- data/lib/fear/extractor/matcher/and.rb +3 -1
- data/lib/fear/extractor/named_array_splat_matcher.rb +2 -0
- data/lib/fear/extractor/pattern.rb +7 -5
- data/lib/fear/extractor/typed_identifier_matcher.rb +2 -0
- data/lib/fear/extractor/value_matcher.rb +2 -0
- data/lib/fear/extractor_api.rb +2 -0
- data/lib/fear/failure.rb +2 -0
- data/lib/fear/failure_pattern_match.rb +2 -0
- data/lib/fear/for.rb +4 -2
- data/lib/fear/for_api.rb +3 -1
- data/lib/fear/future.rb +141 -64
- data/lib/fear/future_api.rb +2 -0
- data/lib/fear/left.rb +3 -1
- data/lib/fear/left_pattern_match.rb +2 -0
- data/lib/fear/none.rb +4 -2
- data/lib/fear/none_pattern_match.rb +2 -0
- data/lib/fear/option.rb +3 -1
- data/lib/fear/option_api.rb +2 -0
- data/lib/fear/option_pattern_match.rb +2 -0
- data/lib/fear/partial_function.rb +10 -8
- data/lib/fear/partial_function/and_then.rb +4 -2
- data/lib/fear/partial_function/any.rb +2 -0
- data/lib/fear/partial_function/combined.rb +3 -1
- data/lib/fear/partial_function/empty.rb +2 -0
- data/lib/fear/partial_function/guard.rb +7 -5
- data/lib/fear/partial_function/guard/and.rb +2 -0
- data/lib/fear/partial_function/guard/and3.rb +2 -0
- data/lib/fear/partial_function/guard/or.rb +2 -0
- data/lib/fear/partial_function/lifted.rb +2 -0
- data/lib/fear/partial_function/or_else.rb +3 -1
- data/lib/fear/partial_function_class.rb +3 -1
- data/lib/fear/pattern_match.rb +3 -1
- data/lib/fear/pattern_matching_api.rb +3 -1
- data/lib/fear/promise.rb +11 -3
- data/lib/fear/right.rb +3 -1
- data/lib/fear/right_biased.rb +4 -2
- data/lib/fear/right_pattern_match.rb +2 -0
- data/lib/fear/some.rb +2 -0
- data/lib/fear/some_pattern_match.rb +2 -0
- data/lib/fear/struct.rb +235 -0
- data/lib/fear/success.rb +2 -0
- data/lib/fear/success_pattern_match.rb +2 -0
- data/lib/fear/try.rb +2 -0
- data/lib/fear/try_api.rb +2 -0
- data/lib/fear/try_pattern_match.rb +2 -0
- data/lib/fear/unit.rb +6 -2
- data/lib/fear/utils.rb +4 -2
- data/lib/fear/version.rb +4 -1
- data/spec/fear/done_spec.rb +7 -5
- data/spec/fear/either/mixin_spec.rb +4 -2
- data/spec/fear/either_pattern_match_spec.rb +10 -8
- data/spec/fear/extractor/array_matcher_spec.rb +65 -63
- data/spec/fear/extractor/extractor_matcher_spec.rb +64 -62
- data/spec/fear/extractor/grammar_array_spec.rb +5 -3
- data/spec/fear/extractor/identified_matcher_spec.rb +18 -16
- data/spec/fear/extractor/identifier_matcher_spec.rb +26 -24
- data/spec/fear/extractor/pattern_spec.rb +17 -15
- data/spec/fear/extractor/typed_identifier_matcher_spec.rb +23 -21
- data/spec/fear/extractor/value_matcher_number_spec.rb +29 -27
- data/spec/fear/extractor/value_matcher_string_spec.rb +27 -25
- data/spec/fear/extractor/value_matcher_symbol_spec.rb +24 -22
- data/spec/fear/extractor_api_spec.rb +70 -68
- data/spec/fear/extractor_spec.rb +23 -21
- data/spec/fear/failure_spec.rb +59 -57
- data/spec/fear/for_spec.rb +19 -17
- data/spec/fear/future_spec.rb +456 -240
- data/spec/fear/guard_spec.rb +26 -24
- data/spec/fear/left_spec.rb +60 -58
- data/spec/fear/none_spec.rb +36 -34
- data/spec/fear/option/mixin_spec.rb +9 -7
- data/spec/fear/option_pattern_match_spec.rb +10 -8
- data/spec/fear/partial_function/empty_spec.rb +12 -10
- data/spec/fear/partial_function_and_then_spec.rb +39 -37
- data/spec/fear/partial_function_composition_spec.rb +46 -44
- data/spec/fear/partial_function_or_else_spec.rb +92 -90
- data/spec/fear/partial_function_spec.rb +46 -44
- data/spec/fear/pattern_match_spec.rb +31 -29
- data/spec/fear/promise_spec.rb +19 -17
- data/spec/fear/right_biased/left.rb +28 -26
- data/spec/fear/right_biased/right.rb +51 -49
- data/spec/fear/right_spec.rb +58 -56
- data/spec/fear/some_spec.rb +30 -28
- data/spec/fear/success_spec.rb +50 -48
- data/spec/fear/try/mixin_spec.rb +5 -3
- data/spec/fear/try_pattern_match_spec.rb +10 -8
- data/spec/fear/utils_spec.rb +16 -14
- data/spec/spec_helper.rb +7 -5
- data/spec/struct_spec.rb +226 -0
- metadata +18 -13
@@ -1,15 +1,17 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fear"
|
2
4
|
|
3
5
|
User = Struct.new(:id, :name, :admin)
|
4
6
|
|
5
7
|
matcher = Fear.matcher do |m|
|
6
|
-
m.xcase(
|
8
|
+
m.xcase("User(_, name, true)") do |name:|
|
7
9
|
puts "Hi #{name}, you are welcome"
|
8
10
|
end
|
9
|
-
m.xcase(
|
10
|
-
puts
|
11
|
+
m.xcase("User(_, _, false)") do
|
12
|
+
puts "Only admins allowed here"
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
matcher.
|
15
|
-
matcher.
|
16
|
+
matcher.(User.new(1, "Jane", true))
|
17
|
+
matcher.(User.new(1, "John", false))
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fear"
|
2
4
|
|
3
5
|
# @example Usage
|
4
6
|
# set = BinaryTreeSet.new
|
@@ -87,7 +89,7 @@ class BinaryTreeSet
|
|
87
89
|
# @param position [Position]
|
88
90
|
# @return [Fear::Option<BinaryTreeSet>]
|
89
91
|
private def leaf(position)
|
90
|
-
if subtrees.
|
92
|
+
if subtrees.has_key?(position)
|
91
93
|
Fear.some(subtrees[position])
|
92
94
|
else
|
93
95
|
Fear.none
|
@@ -1,54 +1,58 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fear"
|
2
4
|
|
3
5
|
class ToWords
|
6
|
+
NUMBERS = {
|
7
|
+
0 => "zero",
|
8
|
+
1 => "one",
|
9
|
+
2 => "two",
|
10
|
+
3 => "three",
|
11
|
+
4 => "four",
|
12
|
+
5 => "five",
|
13
|
+
6 => "six",
|
14
|
+
7 => "seven",
|
15
|
+
8 => "eight",
|
16
|
+
9 => "nine",
|
17
|
+
10 => "ten",
|
18
|
+
11 => "eleven",
|
19
|
+
12 => "twelve",
|
20
|
+
13 => "thirteen",
|
21
|
+
14 => "fourteen",
|
22
|
+
15 => "fifteen",
|
23
|
+
16 => "sixteen",
|
24
|
+
17 => "seventeen",
|
25
|
+
18 => "eighteen",
|
26
|
+
19 => "nineteen",
|
27
|
+
20 => "twenty",
|
28
|
+
30 => "thirty",
|
29
|
+
40 => "forty",
|
30
|
+
50 => "fifty",
|
31
|
+
60 => "sixty",
|
32
|
+
70 => "seventy",
|
33
|
+
80 => "eighty",
|
34
|
+
90 => "ninety",
|
35
|
+
}.freeze
|
36
|
+
|
4
37
|
CONVERTER = Fear.matcher do |m|
|
5
|
-
|
6
|
-
0 => 'zero',
|
7
|
-
1 => 'one',
|
8
|
-
2 => 'two',
|
9
|
-
3 => 'three',
|
10
|
-
4 => 'four',
|
11
|
-
5 => 'five',
|
12
|
-
6 => 'six',
|
13
|
-
7 => 'seven',
|
14
|
-
8 => 'eight',
|
15
|
-
9 => 'nine',
|
16
|
-
10 => 'ten',
|
17
|
-
11 => 'eleven',
|
18
|
-
12 => 'twelve',
|
19
|
-
13 => 'thirteen',
|
20
|
-
14 => 'fourteen',
|
21
|
-
15 => 'fifteen',
|
22
|
-
16 => 'sixteen',
|
23
|
-
17 => 'seventeen',
|
24
|
-
18 => 'eighteen',
|
25
|
-
19 => 'nineteen',
|
26
|
-
20 => 'twenty',
|
27
|
-
30 => 'thirty',
|
28
|
-
40 => 'forty',
|
29
|
-
50 => 'fifty',
|
30
|
-
60 => 'sixty',
|
31
|
-
70 => 'seventy',
|
32
|
-
80 => 'eighty',
|
33
|
-
90 => 'ninety',
|
34
|
-
}.each_pair do |number, in_words|
|
38
|
+
NUMBERS.each_pair do |number, in_words|
|
35
39
|
m.case(number) { in_words }
|
36
40
|
end
|
37
|
-
m.case(->(n) { n < 0 }) { |n| "minus #{CONVERTER.
|
38
|
-
m.case(->(n) { n < 100 }) { |n| "#{CONVERTER.
|
39
|
-
m.case(->(n) { n < 200 }) { |n| "one hundred #{CONVERTER.
|
40
|
-
m.case(->(n) { n < 1_000 }) { |n| "#{CONVERTER.
|
41
|
-
m.case(->(n) { n < 2_000 }) { |n| "one thousand #{CONVERTER.
|
42
|
-
m.case(->(n) { n < 1_000_000 }) { |n| "#{CONVERTER.
|
41
|
+
m.case(->(n) { n < 0 }) { |n| "minus #{CONVERTER.(-n)}" }
|
42
|
+
m.case(->(n) { n < 100 }) { |n| "#{CONVERTER.((n / 10) * 10)}-#{CONVERTER.(n % 10)}" }
|
43
|
+
m.case(->(n) { n < 200 }) { |n| "one hundred #{CONVERTER.(n % 100)}" }
|
44
|
+
m.case(->(n) { n < 1_000 }) { |n| "#{CONVERTER.(n / 100)} hundreds #{CONVERTER.(n % 100)}" }
|
45
|
+
m.case(->(n) { n < 2_000 }) { |n| "one thousand #{CONVERTER.(n % 1000)}" }
|
46
|
+
m.case(->(n) { n < 1_000_000 }) { |n| "#{CONVERTER.(n / 1_000)} thousands #{CONVERTER.(n % 1_000)}" }
|
43
47
|
m.else { |n| raise "#{n} too big " }
|
44
48
|
end
|
45
49
|
|
46
50
|
def self.call(number)
|
47
|
-
Fear.case(Integer, &:itself).and_then(CONVERTER).
|
51
|
+
Fear.case(Integer, &:itself).and_then(CONVERTER).(number)
|
48
52
|
end
|
49
53
|
end
|
50
54
|
|
51
|
-
ToWords.
|
52
|
-
ToWords.
|
53
|
-
ToWords.
|
54
|
-
ToWords.
|
55
|
+
ToWords.(99) #=> 'ninety-nine'
|
56
|
+
ToWords.(133) #=> 'one hundred thirty-three
|
57
|
+
ToWords.(777) #=> 'seven hundreds seventy-seven'
|
58
|
+
ToWords.(254_555) #=> 'two hundreds fifty-four thousands five hundreds fifty-five'
|
data/fear.gemspec
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
2
4
|
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
|
5
|
-
require
|
7
|
+
require "fear/version"
|
6
8
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
14
|
-
spec.license
|
9
|
+
spec.name = "fear"
|
10
|
+
spec.version = Fear::VERSION
|
11
|
+
spec.authors = ["Tema Bolshakov"]
|
12
|
+
spec.email = ["abolshakov@spbtv.com"]
|
13
|
+
spec.summary = "%q{Ruby port of some Scala's monads.}"
|
14
|
+
spec.description = "Ruby port of some Scala's monads."
|
15
|
+
spec.homepage = "https://github.com/bolshakov/fear"
|
16
|
+
spec.license = "MIT"
|
15
17
|
|
16
|
-
spec.files
|
17
|
-
spec.executables
|
18
|
-
spec.test_files
|
19
|
-
spec.require_paths = [
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin\/}) { |f| File.basename(f) }
|
20
|
+
spec.test_files = spec.files.grep(%r{^spec\/})
|
21
|
+
spec.require_paths = ["lib"]
|
20
22
|
|
21
23
|
spec.post_install_message = <<-MSG
|
22
24
|
Fear v0.11.0 introduces backwards-incompatible changes.
|
@@ -24,18 +26,18 @@ Gem::Specification.new do |spec|
|
|
24
26
|
Successfully installed fear-#{Fear::VERSION}
|
25
27
|
MSG
|
26
28
|
|
27
|
-
spec.add_runtime_dependency
|
28
|
-
spec.add_runtime_dependency
|
29
|
+
spec.add_runtime_dependency "lru_redux"
|
30
|
+
spec.add_runtime_dependency "treetop"
|
29
31
|
|
30
|
-
spec.add_development_dependency
|
31
|
-
spec.add_development_dependency
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
34
|
-
spec.add_development_dependency
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
spec.add_development_dependency
|
38
|
-
spec.add_development_dependency
|
39
|
-
spec.add_development_dependency
|
40
|
-
spec.add_development_dependency
|
32
|
+
spec.add_development_dependency "benchmark-ips"
|
33
|
+
spec.add_development_dependency "bundler"
|
34
|
+
spec.add_development_dependency "concurrent-ruby"
|
35
|
+
spec.add_development_dependency "dry-matcher"
|
36
|
+
spec.add_development_dependency "dry-monads"
|
37
|
+
spec.add_development_dependency "qo"
|
38
|
+
spec.add_development_dependency "rake", "~> 12.3"
|
39
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
40
|
+
spec.add_development_dependency "rubocop-rspec", "1.33.0"
|
41
|
+
spec.add_development_dependency "ruby_coding_standard"
|
42
|
+
spec.add_development_dependency "yard"
|
41
43
|
end
|
data/lib/fear.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fear/either_api"
|
4
|
+
require "fear/extractor_api"
|
5
|
+
require "fear/for_api"
|
6
|
+
require "fear/future_api"
|
7
|
+
require "fear/option_api"
|
8
|
+
require "fear/pattern_matching_api"
|
9
|
+
require "fear/try_api"
|
10
|
+
require "fear/version"
|
9
11
|
|
10
12
|
module Fear
|
11
13
|
Error = Class.new(StandardError)
|
@@ -22,40 +24,45 @@ module Fear
|
|
22
24
|
extend PatternMatchingApi
|
23
25
|
extend TryApi
|
24
26
|
|
25
|
-
autoload :EmptyPartialFunction,
|
26
|
-
autoload :PartialFunction,
|
27
|
-
autoload :PartialFunctionClass,
|
28
|
-
autoload :PatternMatch,
|
29
|
-
autoload :Extractor,
|
27
|
+
autoload :EmptyPartialFunction, "fear/empty_partial_function"
|
28
|
+
autoload :PartialFunction, "fear/partial_function"
|
29
|
+
autoload :PartialFunctionClass, "fear/partial_function_class"
|
30
|
+
autoload :PatternMatch, "fear/pattern_match"
|
31
|
+
autoload :Extractor, "fear/extractor"
|
32
|
+
|
33
|
+
autoload :Unit, "fear/unit"
|
34
|
+
autoload :For, "fear/for"
|
35
|
+
autoload :RightBiased, "fear/right_biased"
|
36
|
+
autoload :Utils, "fear/utils"
|
30
37
|
|
31
|
-
autoload :
|
32
|
-
autoload :
|
33
|
-
autoload :
|
34
|
-
autoload :
|
38
|
+
autoload :None, "fear/none"
|
39
|
+
autoload :NoneClass, "fear/none"
|
40
|
+
autoload :NonePatternMatch, "fear/none_pattern_match"
|
41
|
+
autoload :Option, "fear/option"
|
42
|
+
autoload :OptionPatternMatch, "fear/option_pattern_match"
|
43
|
+
autoload :Some, "fear/some"
|
44
|
+
autoload :SomePatternMatch, "fear/some_pattern_match"
|
35
45
|
|
36
|
-
autoload :
|
37
|
-
autoload :
|
38
|
-
autoload :
|
39
|
-
autoload :
|
40
|
-
autoload :
|
41
|
-
autoload :
|
42
|
-
autoload :SomePatternMatch, 'fear/some_pattern_match'
|
46
|
+
autoload :Failure, "fear/failure"
|
47
|
+
autoload :FailurePatternMatch, "fear/failure_pattern_match"
|
48
|
+
autoload :Success, "fear/success"
|
49
|
+
autoload :SuccessPatternMatch, "fear/success_pattern_match"
|
50
|
+
autoload :Try, "fear/try"
|
51
|
+
autoload :TryPatternMatch, "fear/try_pattern_match"
|
43
52
|
|
44
|
-
autoload :
|
45
|
-
autoload :
|
46
|
-
autoload :
|
47
|
-
autoload :
|
48
|
-
autoload :
|
49
|
-
autoload :
|
53
|
+
autoload :Either, "fear/either"
|
54
|
+
autoload :EitherPatternMatch, "fear/either_pattern_match"
|
55
|
+
autoload :Left, "fear/left"
|
56
|
+
autoload :LeftPatternMatch, "fear/left_pattern_match"
|
57
|
+
autoload :Right, "fear/right"
|
58
|
+
autoload :RightPatternMatch, "fear/right_pattern_match"
|
50
59
|
|
51
|
-
autoload :
|
52
|
-
autoload :
|
53
|
-
autoload :
|
54
|
-
autoload :
|
55
|
-
autoload :Right, 'fear/right'
|
56
|
-
autoload :RightPatternMatch, 'fear/right_pattern_match'
|
60
|
+
autoload :Await, "fear/await"
|
61
|
+
autoload :Awaitable, "fear/awaitable"
|
62
|
+
autoload :Future, "fear/future"
|
63
|
+
autoload :Promise, "fear/promise"
|
57
64
|
|
58
|
-
autoload :
|
65
|
+
autoload :Struct, "fear/struct"
|
59
66
|
|
60
67
|
module Mixin
|
61
68
|
include Either::Mixin
|
data/lib/fear/await.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Fear
|
4
|
+
# You're strongly discouraged to use this module since it may lead to deadlocks,
|
5
|
+
# and reduced performance. Although, blocking may be useful in some cases (e.g. in tests)
|
6
|
+
#
|
7
|
+
# @see https://stackoverflow.com/questions/38155159/why-doesnt-scalas-future-have-a-get-getmaxduration-method-forcing-us-to
|
8
|
+
module Await
|
9
|
+
# Blocks until +Fear::Awaitable+ reached completed state and returns itself
|
10
|
+
# or raises +TimeoutError+
|
11
|
+
#
|
12
|
+
# @param awaitable [Fear::Awaitable]
|
13
|
+
# @param at_most [Fixnum] timeout in seconds
|
14
|
+
# @return [Fear::Awaitable]
|
15
|
+
# @raise [Timeout::Error]
|
16
|
+
#
|
17
|
+
module_function def ready(awaitable, at_most)
|
18
|
+
awaitable.__ready__(at_most)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Blocks until +Fear::Awaitable+ reached completed state and returns its value
|
22
|
+
# or raises +TimeoutError+
|
23
|
+
#
|
24
|
+
# @param awaitable [Fear::Awaitable]
|
25
|
+
# @param at_most [Fixnum] timeout in seconds
|
26
|
+
# @return [any]
|
27
|
+
# @raise [Timeout::Error]
|
28
|
+
#
|
29
|
+
module_function def result(awaitable, at_most)
|
30
|
+
awaitable.__result__(at_most)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Fear
|
4
|
+
# An object which may eventually be completed and awaited using blocking methods.
|
5
|
+
#
|
6
|
+
# @abstract
|
7
|
+
# @api private
|
8
|
+
# @see Fear::Await
|
9
|
+
module Awaitable
|
10
|
+
# Await +completed+ state of this +Awaitable+
|
11
|
+
#
|
12
|
+
# @param at_most [Fixnum] maximum timeout in seconds
|
13
|
+
# @return [Fear::Awaitable]
|
14
|
+
# @raise [Timeout::Error]
|
15
|
+
def __ready__(_at_most)
|
16
|
+
raise NotImplementedError
|
17
|
+
end
|
18
|
+
|
19
|
+
# Await and return the result of this +Awaitable+
|
20
|
+
#
|
21
|
+
# @param at_most [Fixnum] maximum timeout in seconds
|
22
|
+
# @return [any]
|
23
|
+
# @raise [Timeout::Error]
|
24
|
+
def __result__(_at_most)
|
25
|
+
raise NotImplementedError
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/fear/either.rb
CHANGED
data/lib/fear/either_api.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Fear
|
2
4
|
# Use singleton version of EmptyPartialFunction -- PartialFunction::EMPTY
|
3
5
|
# @api private
|
@@ -28,7 +30,7 @@ module Fear
|
|
28
30
|
end
|
29
31
|
|
30
32
|
def to_s
|
31
|
-
|
33
|
+
"Empty partial function"
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
data/lib/fear/extractor.rb
CHANGED
@@ -1,24 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "treetop"
|
4
|
+
require "fear/extractor/grammar"
|
5
|
+
Treetop.load File.expand_path("extractor/grammar.treetop", __dir__)
|
4
6
|
|
5
7
|
module Fear
|
6
8
|
# @api private
|
7
9
|
module Extractor
|
8
|
-
autoload :Pattern,
|
9
|
-
autoload :Matcher,
|
10
|
+
autoload :Pattern, "fear/extractor/pattern"
|
11
|
+
autoload :Matcher, "fear/extractor/matcher"
|
10
12
|
|
11
|
-
autoload :AnonymousArraySplatMatcher,
|
12
|
-
autoload :AnyMatcher,
|
13
|
-
autoload :ArrayHeadMatcher,
|
14
|
-
autoload :ArrayMatcher,
|
15
|
-
autoload :ArraySplatMatcher,
|
16
|
-
autoload :EmptyListMatcher,
|
17
|
-
autoload :ExtractorMatcher,
|
18
|
-
autoload :IdentifierMatcher,
|
19
|
-
autoload :NamedArraySplatMatcher,
|
20
|
-
autoload :TypedIdentifierMatcher,
|
21
|
-
autoload :ValueMatcher,
|
13
|
+
autoload :AnonymousArraySplatMatcher, "fear/extractor/anonymous_array_splat_matcher"
|
14
|
+
autoload :AnyMatcher, "fear/extractor/any_matcher"
|
15
|
+
autoload :ArrayHeadMatcher, "fear/extractor/array_head_matcher"
|
16
|
+
autoload :ArrayMatcher, "fear/extractor/array_matcher"
|
17
|
+
autoload :ArraySplatMatcher, "fear/extractor/array_splat_matcher"
|
18
|
+
autoload :EmptyListMatcher, "fear/extractor/empty_list_matcher"
|
19
|
+
autoload :ExtractorMatcher, "fear/extractor/extractor_matcher"
|
20
|
+
autoload :IdentifierMatcher, "fear/extractor/identifier_matcher"
|
21
|
+
autoload :NamedArraySplatMatcher, "fear/extractor/named_array_splat_matcher"
|
22
|
+
autoload :TypedIdentifierMatcher, "fear/extractor/typed_identifier_matcher"
|
23
|
+
autoload :ValueMatcher, "fear/extractor/value_matcher"
|
22
24
|
|
23
25
|
ExtractorNotFound = Class.new(Error)
|
24
26
|
|
@@ -26,7 +28,7 @@ module Fear
|
|
26
28
|
@registry = PartialFunction::EMPTY
|
27
29
|
|
28
30
|
EXTRACTOR_NOT_FOUND = proc do |klass|
|
29
|
-
raise ExtractorNotFound,
|
31
|
+
raise ExtractorNotFound, "could not find extractor for " + klass.inspect
|
30
32
|
end
|
31
33
|
|
32
34
|
class << self
|
@@ -58,7 +60,7 @@ module Fear
|
|
58
60
|
|
59
61
|
@mutex.synchronize do
|
60
62
|
keys.uniq.each do |key|
|
61
|
-
@registry = BUILD_EXTRACTOR.
|
63
|
+
@registry = BUILD_EXTRACTOR.(key, extractor).or_else(@registry)
|
62
64
|
end
|
63
65
|
end
|
64
66
|
self
|
@@ -81,16 +83,16 @@ module Fear
|
|
81
83
|
end
|
82
84
|
|
83
85
|
# Multiple arguments extractor example
|
84
|
-
register_extractor(
|
85
|
-
if other.class.name ==
|
86
|
+
register_extractor("Date", proc do |other|
|
87
|
+
if other.class.name == "Date"
|
86
88
|
Fear.some([other.year, other.month, other.day])
|
87
89
|
else
|
88
90
|
Fear.none
|
89
91
|
end
|
90
92
|
end)
|
91
|
-
register_extractor(Struct, Fear.case(Struct, &:to_a).lift)
|
93
|
+
register_extractor(::Struct, Fear.case(::Struct, &:to_a).lift)
|
92
94
|
# No argument boolean extractor example
|
93
|
-
register_extractor(
|
95
|
+
register_extractor("IsEven", proc do |int|
|
94
96
|
if int.is_a?(Integer) && int.even?
|
95
97
|
Fear.some([])
|
96
98
|
else
|
@@ -98,11 +100,11 @@ module Fear
|
|
98
100
|
end
|
99
101
|
end)
|
100
102
|
# Single argument extractor example
|
101
|
-
register_extractor(
|
102
|
-
register_extractor(
|
103
|
-
register_extractor(
|
104
|
-
register_extractor(
|
105
|
-
register_extractor(
|
106
|
-
register_extractor(
|
103
|
+
register_extractor("Fear::Some", "Some", Some::EXTRACTOR)
|
104
|
+
register_extractor("Fear::None", "None", NoneClass::EXTRACTOR)
|
105
|
+
register_extractor("Fear::Right", "Right", Right::EXTRACTOR)
|
106
|
+
register_extractor("Fear::Left", "Left", Left::EXTRACTOR)
|
107
|
+
register_extractor("Fear::Success", "Success", Success::EXTRACTOR)
|
108
|
+
register_extractor("Fear::Failure", "Failure", Failure::EXTRACTOR)
|
107
109
|
end
|
108
110
|
end
|