fear 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +4 -60
  3. data/.travis.yml +8 -4
  4. data/CHANGELOG.md +7 -1
  5. data/Gemfile +5 -3
  6. data/Gemfile.lock +18 -20
  7. data/README.md +28 -14
  8. data/Rakefile +61 -60
  9. data/examples/pattern_extracting.rb +8 -6
  10. data/examples/pattern_matching_binary_tree_set.rb +4 -2
  11. data/examples/pattern_matching_number_in_words.rb +46 -42
  12. data/fear.gemspec +29 -27
  13. data/lib/fear.rb +44 -37
  14. data/lib/fear/await.rb +33 -0
  15. data/lib/fear/awaitable.rb +28 -0
  16. data/lib/fear/either.rb +2 -0
  17. data/lib/fear/either_api.rb +2 -0
  18. data/lib/fear/either_pattern_match.rb +2 -0
  19. data/lib/fear/empty_partial_function.rb +3 -1
  20. data/lib/fear/extractor.rb +30 -28
  21. data/lib/fear/extractor/anonymous_array_splat_matcher.rb +2 -0
  22. data/lib/fear/extractor/any_matcher.rb +2 -0
  23. data/lib/fear/extractor/array_head_matcher.rb +2 -0
  24. data/lib/fear/extractor/array_matcher.rb +2 -0
  25. data/lib/fear/extractor/array_splat_matcher.rb +2 -0
  26. data/lib/fear/extractor/empty_list_matcher.rb +2 -0
  27. data/lib/fear/extractor/extractor_matcher.rb +5 -3
  28. data/lib/fear/extractor/grammar.rb +5 -3
  29. data/lib/fear/extractor/identifier_matcher.rb +2 -0
  30. data/lib/fear/extractor/matcher.rb +5 -3
  31. data/lib/fear/extractor/matcher/and.rb +3 -1
  32. data/lib/fear/extractor/named_array_splat_matcher.rb +2 -0
  33. data/lib/fear/extractor/pattern.rb +7 -5
  34. data/lib/fear/extractor/typed_identifier_matcher.rb +2 -0
  35. data/lib/fear/extractor/value_matcher.rb +2 -0
  36. data/lib/fear/extractor_api.rb +2 -0
  37. data/lib/fear/failure.rb +2 -0
  38. data/lib/fear/failure_pattern_match.rb +2 -0
  39. data/lib/fear/for.rb +4 -2
  40. data/lib/fear/for_api.rb +3 -1
  41. data/lib/fear/future.rb +141 -64
  42. data/lib/fear/future_api.rb +2 -0
  43. data/lib/fear/left.rb +3 -1
  44. data/lib/fear/left_pattern_match.rb +2 -0
  45. data/lib/fear/none.rb +4 -2
  46. data/lib/fear/none_pattern_match.rb +2 -0
  47. data/lib/fear/option.rb +3 -1
  48. data/lib/fear/option_api.rb +2 -0
  49. data/lib/fear/option_pattern_match.rb +2 -0
  50. data/lib/fear/partial_function.rb +10 -8
  51. data/lib/fear/partial_function/and_then.rb +4 -2
  52. data/lib/fear/partial_function/any.rb +2 -0
  53. data/lib/fear/partial_function/combined.rb +3 -1
  54. data/lib/fear/partial_function/empty.rb +2 -0
  55. data/lib/fear/partial_function/guard.rb +7 -5
  56. data/lib/fear/partial_function/guard/and.rb +2 -0
  57. data/lib/fear/partial_function/guard/and3.rb +2 -0
  58. data/lib/fear/partial_function/guard/or.rb +2 -0
  59. data/lib/fear/partial_function/lifted.rb +2 -0
  60. data/lib/fear/partial_function/or_else.rb +3 -1
  61. data/lib/fear/partial_function_class.rb +3 -1
  62. data/lib/fear/pattern_match.rb +3 -1
  63. data/lib/fear/pattern_matching_api.rb +3 -1
  64. data/lib/fear/promise.rb +11 -3
  65. data/lib/fear/right.rb +3 -1
  66. data/lib/fear/right_biased.rb +4 -2
  67. data/lib/fear/right_pattern_match.rb +2 -0
  68. data/lib/fear/some.rb +2 -0
  69. data/lib/fear/some_pattern_match.rb +2 -0
  70. data/lib/fear/struct.rb +235 -0
  71. data/lib/fear/success.rb +2 -0
  72. data/lib/fear/success_pattern_match.rb +2 -0
  73. data/lib/fear/try.rb +2 -0
  74. data/lib/fear/try_api.rb +2 -0
  75. data/lib/fear/try_pattern_match.rb +2 -0
  76. data/lib/fear/unit.rb +6 -2
  77. data/lib/fear/utils.rb +4 -2
  78. data/lib/fear/version.rb +4 -1
  79. data/spec/fear/done_spec.rb +7 -5
  80. data/spec/fear/either/mixin_spec.rb +4 -2
  81. data/spec/fear/either_pattern_match_spec.rb +10 -8
  82. data/spec/fear/extractor/array_matcher_spec.rb +65 -63
  83. data/spec/fear/extractor/extractor_matcher_spec.rb +64 -62
  84. data/spec/fear/extractor/grammar_array_spec.rb +5 -3
  85. data/spec/fear/extractor/identified_matcher_spec.rb +18 -16
  86. data/spec/fear/extractor/identifier_matcher_spec.rb +26 -24
  87. data/spec/fear/extractor/pattern_spec.rb +17 -15
  88. data/spec/fear/extractor/typed_identifier_matcher_spec.rb +23 -21
  89. data/spec/fear/extractor/value_matcher_number_spec.rb +29 -27
  90. data/spec/fear/extractor/value_matcher_string_spec.rb +27 -25
  91. data/spec/fear/extractor/value_matcher_symbol_spec.rb +24 -22
  92. data/spec/fear/extractor_api_spec.rb +70 -68
  93. data/spec/fear/extractor_spec.rb +23 -21
  94. data/spec/fear/failure_spec.rb +59 -57
  95. data/spec/fear/for_spec.rb +19 -17
  96. data/spec/fear/future_spec.rb +456 -240
  97. data/spec/fear/guard_spec.rb +26 -24
  98. data/spec/fear/left_spec.rb +60 -58
  99. data/spec/fear/none_spec.rb +36 -34
  100. data/spec/fear/option/mixin_spec.rb +9 -7
  101. data/spec/fear/option_pattern_match_spec.rb +10 -8
  102. data/spec/fear/partial_function/empty_spec.rb +12 -10
  103. data/spec/fear/partial_function_and_then_spec.rb +39 -37
  104. data/spec/fear/partial_function_composition_spec.rb +46 -44
  105. data/spec/fear/partial_function_or_else_spec.rb +92 -90
  106. data/spec/fear/partial_function_spec.rb +46 -44
  107. data/spec/fear/pattern_match_spec.rb +31 -29
  108. data/spec/fear/promise_spec.rb +19 -17
  109. data/spec/fear/right_biased/left.rb +28 -26
  110. data/spec/fear/right_biased/right.rb +51 -49
  111. data/spec/fear/right_spec.rb +58 -56
  112. data/spec/fear/some_spec.rb +30 -28
  113. data/spec/fear/success_spec.rb +50 -48
  114. data/spec/fear/try/mixin_spec.rb +5 -3
  115. data/spec/fear/try_pattern_match_spec.rb +10 -8
  116. data/spec/fear/utils_spec.rb +16 -14
  117. data/spec/spec_helper.rb +7 -5
  118. data/spec/struct_spec.rb +226 -0
  119. metadata +18 -13
@@ -1,15 +1,17 @@
1
- require 'fear'
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('User(_, name, true)') do |name:|
8
+ m.xcase("User(_, name, true)") do |name:|
7
9
  puts "Hi #{name}, you are welcome"
8
10
  end
9
- m.xcase('User(_, _, false)') do
10
- puts 'Only admins allowed here'
11
+ m.xcase("User(_, _, false)") do
12
+ puts "Only admins allowed here"
11
13
  end
12
14
  end
13
15
 
14
- matcher.call User.new(1, 'Jane', true)
15
- matcher.call User.new(1, 'John', false)
16
+ matcher.(User.new(1, "Jane", true))
17
+ matcher.(User.new(1, "John", false))
@@ -1,4 +1,6 @@
1
- require 'fear'
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.key?(position)
92
+ if subtrees.has_key?(position)
91
93
  Fear.some(subtrees[position])
92
94
  else
93
95
  Fear.none
@@ -1,54 +1,58 @@
1
- require 'fear'
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.call(-n)}" }
38
- m.case(->(n) { n < 100 }) { |n| "#{CONVERTER.call((n / 10) * 10)}-#{CONVERTER.call(n % 10)}" }
39
- m.case(->(n) { n < 200 }) { |n| "one hundred #{CONVERTER.call(n % 100)}" }
40
- m.case(->(n) { n < 1_000 }) { |n| "#{CONVERTER.call(n / 100)} hundreds #{CONVERTER.call(n % 100)}" }
41
- m.case(->(n) { n < 2_000 }) { |n| "one thousand #{CONVERTER.call(n % 1000)}" }
42
- m.case(->(n) { n < 1_000_000 }) { |n| "#{CONVERTER.call(n / 1_000)} thousands #{CONVERTER.call(n % 1_000)}" }
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).call(number)
51
+ Fear.case(Integer, &:itself).and_then(CONVERTER).(number)
48
52
  end
49
53
  end
50
54
 
51
- ToWords.call(99) #=> 'ninety-nine'
52
- ToWords.call(133) #=> 'one hundred thirty-three
53
- ToWords.call(777) #=> 'seven hundreds seventy-seven'
54
- ToWords.call(254_555) #=> 'two hundreds fifty-four thousands five hundreds fifty-five'
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'
@@ -1,22 +1,24 @@
1
- lib = File.expand_path('lib', __dir__)
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 'fear/version'
7
+ require "fear/version"
6
8
  Gem::Specification.new do |spec|
7
- spec.name = 'fear'
8
- spec.version = Fear::VERSION
9
- spec.authors = ['Tema Bolshakov']
10
- spec.email = ['abolshakov@spbtv.com']
11
- spec.summary = "%q{Ruby port of some Scala's monads.}"
12
- spec.description = "Ruby port of some Scala's monads."
13
- spec.homepage = 'https://github.com/bolshakov/fear'
14
- spec.license = 'MIT'
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 = `git ls-files -z`.split("\x0")
17
- spec.executables = spec.files.grep(%r{^bin\/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^spec\/})
19
- spec.require_paths = ['lib']
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 'lru_redux'
28
- spec.add_runtime_dependency 'treetop'
29
+ spec.add_runtime_dependency "lru_redux"
30
+ spec.add_runtime_dependency "treetop"
29
31
 
30
- spec.add_development_dependency 'benchmark-ips'
31
- spec.add_development_dependency 'bundler'
32
- spec.add_development_dependency 'concurrent-ruby'
33
- spec.add_development_dependency 'dry-matcher'
34
- spec.add_development_dependency 'dry-monads'
35
- spec.add_development_dependency 'qo'
36
- spec.add_development_dependency 'rake', '~> 10.0'
37
- spec.add_development_dependency 'rspec', '~> 3.1'
38
- spec.add_development_dependency 'rubocop', '0.65.0'
39
- spec.add_development_dependency 'rubocop-rspec', '1.32.0'
40
- spec.add_development_dependency 'yard'
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
@@ -1,11 +1,13 @@
1
- require 'fear/either_api'
2
- require 'fear/extractor_api'
3
- require 'fear/for_api'
4
- require 'fear/future_api'
5
- require 'fear/option_api'
6
- require 'fear/pattern_matching_api'
7
- require 'fear/try_api'
8
- require 'fear/version'
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, 'fear/empty_partial_function'
26
- autoload :PartialFunction, 'fear/partial_function'
27
- autoload :PartialFunctionClass, 'fear/partial_function_class'
28
- autoload :PatternMatch, 'fear/pattern_match'
29
- autoload :Extractor, 'fear/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 :Unit, 'fear/unit'
32
- autoload :For, 'fear/for'
33
- autoload :RightBiased, 'fear/right_biased'
34
- autoload :Utils, 'fear/utils'
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 :None, 'fear/none'
37
- autoload :NoneClass, 'fear/none'
38
- autoload :NonePatternMatch, 'fear/none_pattern_match'
39
- autoload :Option, 'fear/option'
40
- autoload :OptionPatternMatch, 'fear/option_pattern_match'
41
- autoload :Some, 'fear/some'
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 :Failure, 'fear/failure'
45
- autoload :FailurePatternMatch, 'fear/failure_pattern_match'
46
- autoload :Success, 'fear/success'
47
- autoload :SuccessPatternMatch, 'fear/success_pattern_match'
48
- autoload :Try, 'fear/try'
49
- autoload :TryPatternMatch, 'fear/try_pattern_match'
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 :Either, 'fear/either'
52
- autoload :EitherPatternMatch, 'fear/either_pattern_match'
53
- autoload :Left, 'fear/left'
54
- autoload :LeftPatternMatch, 'fear/left_pattern_match'
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 :Future, 'fear/future'
65
+ autoload :Struct, "fear/struct"
59
66
 
60
67
  module Mixin
61
68
  include Either::Mixin
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fear
2
4
  # Represents a value of one of two possible types (a disjoint union.)
3
5
  # An instance of +Either+ is either an instance of +Left+ or +Right+.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fear
2
4
  module EitherApi
3
5
  # @param value [any]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Fear
2
4
  # Either pattern matcher
3
5
  #
@@ -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
- 'Empty partial function'
33
+ "Empty partial function"
32
34
  end
33
35
  end
34
36
 
@@ -1,24 +1,26 @@
1
- require 'treetop'
2
- require 'fear/extractor/grammar'
3
- Treetop.load File.expand_path('extractor/grammar.treetop', __dir__)
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, 'fear/extractor/pattern'
9
- autoload :Matcher, 'fear/extractor/matcher'
10
+ autoload :Pattern, "fear/extractor/pattern"
11
+ autoload :Matcher, "fear/extractor/matcher"
10
12
 
11
- autoload :AnonymousArraySplatMatcher, 'fear/extractor/anonymous_array_splat_matcher'
12
- autoload :AnyMatcher, 'fear/extractor/any_matcher'
13
- autoload :ArrayHeadMatcher, 'fear/extractor/array_head_matcher'
14
- autoload :ArrayMatcher, 'fear/extractor/array_matcher'
15
- autoload :ArraySplatMatcher, 'fear/extractor/array_splat_matcher'
16
- autoload :EmptyListMatcher, 'fear/extractor/empty_list_matcher'
17
- autoload :ExtractorMatcher, 'fear/extractor/extractor_matcher'
18
- autoload :IdentifierMatcher, 'fear/extractor/identifier_matcher'
19
- autoload :NamedArraySplatMatcher, 'fear/extractor/named_array_splat_matcher'
20
- autoload :TypedIdentifierMatcher, 'fear/extractor/typed_identifier_matcher'
21
- autoload :ValueMatcher, 'fear/extractor/value_matcher'
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, 'could not find extractor for ' + klass.inspect
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.call(key, extractor).or_else(@registry)
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('Date', proc do |other|
85
- if other.class.name == 'Date'
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('IsEven', proc do |int|
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('Fear::Some', 'Some', Some::EXTRACTOR)
102
- register_extractor('Fear::None', 'None', NoneClass::EXTRACTOR)
103
- register_extractor('Fear::Right', 'Right', Right::EXTRACTOR)
104
- register_extractor('Fear::Left', 'Left', Left::EXTRACTOR)
105
- register_extractor('Fear::Success', 'Success', Success::EXTRACTOR)
106
- register_extractor('Fear::Failure', 'Failure', Failure::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