Class: Deterministic::Option
- Includes:
- Monad, PatternMatching
- Defined in:
- lib/deterministic/option.rb
Overview
Abstract parent of Some and None
Defined Under Namespace
Modules: PatternMatching Classes: None, Some
Class Method Summary collapse
Instance Method Summary collapse
-
#+(other) ⇒ Object
Add the inner values of two Some.
- #map(proc = nil, &block) ⇒ Object
- #none? ⇒ Boolean
- #some? ⇒ Boolean
- #value_or(default) ⇒ Object
-
#value_to_a ⇒ Object
Convert the inner value to an Array.
Methods included from PatternMatching
Methods included from Monad
#==, #bind, #fmap, #initialize, #inspect, #join, #to_s, #value
Class Method Details
.any?(expr) ⇒ Boolean
30 31 32 |
# File 'lib/deterministic/option.rb', line 30 def any?(expr) to_option(expr) { expr.nil? || not(expr.respond_to?(:empty?)) || expr.empty? } end |
.some?(expr) ⇒ Boolean
26 27 28 |
# File 'lib/deterministic/option.rb', line 26 def some?(expr) to_option(expr) { expr.nil? } end |
Instance Method Details
#+(other) ⇒ Object
Add the inner values of two Some
54 55 56 57 58 59 60 61 62 |
# File 'lib/deterministic/option.rb', line 54 def +(other) return other if none? fmap { |v| other.match { some { v + other.value } none { self } } } end |
#map(proc = nil, &block) ⇒ Object
43 44 45 46 |
# File 'lib/deterministic/option.rb', line 43 def map(proc=nil, &block) return self if none? bind(proc || block) end |
#value_or(default) ⇒ Object
72 73 74 75 |
# File 'lib/deterministic/option.rb', line 72 def value_or(default) return default if none? return value end |
#value_to_a ⇒ Object
Convert the inner value to an Array
49 50 51 |
# File 'lib/deterministic/option.rb', line 49 def value_to_a map { self.class.new(Array(value)) } end |