Class: MonadOxide::Option
- Inherits:
-
Object
- Object
- MonadOxide::Option
- Defined in:
- lib/option.rb
Overview
An Option is a chainable series of sequential transformations. The Option structure contains a ‘Some<A> | None`. This is the central location for documentation between both `Some’ and ‘None’. It is best to think of any given ‘Some’ or ‘None’ as an ‘Option’ instead. All methods on ‘Some’ are present on ‘None’ and vice versa. This way we can interchange one for the other during virtually any call.
This is a base-class only, and you should never see instances of these in the wild.
Instance Method Summary collapse
-
#and_then(f = nil) { ... } ⇒ Some<B> | None
For ‘Some’, invokes ‘f’ or the block with the data and returns the Option returned from that.
-
#initialize(data) ⇒ Option
constructor
A new instance of Option.
-
#inspect_none(f = nil) { ... } ⇒ Option
In the case of ‘None’, applies ‘f’ or the block and returns the same ‘None’.
-
#inspect_some(f = nil) { ... } ⇒ Option<A>
In the case of ‘Some’, applies ‘f’ or the block over the data and returns the same ‘Some’.
-
#map(f = nil) { ... } ⇒ Option<B>
In the case of ‘Some’, applies ‘f’ or the block over the data and returns a new ‘Some’ with the returned value.
-
#map_none(f = nil) { ... } ⇒ Option<B>
In the case of ‘None’, applies ‘f’ or the block and returns a new ‘Option’ with the returned value.
-
#match(matcher) ⇒ R
Use pattern matching to work with both Some and None variants.
-
#none? ⇒ Boolean
Determine if this is a MonadOxide::None.
-
#or_else(f = nil) { ... } ⇒ Some<B> | None
For ‘None’, invokes ‘f’ or the block and returns the Option returned from that.
-
#some? ⇒ Boolean
Determine if this is a MonadOxide::Some.
-
#unwrap ⇒ A
Dangerously access the ‘Option’ data.
-
#unwrap_none ⇒ nil
Dangerously access the ‘Option’ data.
Constructor Details
#initialize(data) ⇒ Option
Returns a new instance of Option.
48 49 50 |
# File 'lib/option.rb', line 48 def initialize(data) raise NoMethodError.new('Do not use Option directly. See Some and None.') end |
Instance Method Details
#and_then(f = nil) { ... } ⇒ Some<B> | None
For ‘Some’, invokes ‘f’ or the block with the data and returns the Option returned from that.
For ‘None’, returns itself and the function/block are ignored.
This method is used for control flow based on ‘Option’ values.
‘and_then’ is desirable for chaining together other Option based operations, or doing transformations where flipping from a ‘Some’ to a ‘None’ is desired. In cases where there is little/no risk of a ‘None’ state, @see Option#map.
The ‘None’ equivalent operation is @see Option#or_else.
The return type is enforced.
145 146 147 |
# File 'lib/option.rb', line 145 def and_then(f=nil, &block) raise OptionMethodNotImplementedError.new() end |
#inspect_none(f = nil) { ... } ⇒ Option
In the case of ‘None’, applies ‘f’ or the block and returns the same ‘None’. No changes are applied. This is ideal for logging. For ‘Some’, this method falls through.
117 118 119 |
# File 'lib/option.rb', line 117 def inspect_none(f=nil, &block) raise OptionMethodNotImplementedError.new() end |
#inspect_some(f = nil) { ... } ⇒ Option<A>
In the case of ‘Some’, applies ‘f’ or the block over the data and returns the same ‘Some’. No changes are applied. This is ideal for logging. For ‘None’, this method falls through.
104 105 106 |
# File 'lib/option.rb', line 104 def inspect_some(f=nil, &block) raise OptionMethodNotImplementedError.new() end |
#map(f = nil) { ... } ⇒ Option<B>
In the case of ‘Some’, applies ‘f’ or the block over the data and returns a new ‘Some’ with the returned value. For ‘None’, this method falls through.
78 79 80 |
# File 'lib/option.rb', line 78 def map(f=nil, &block) raise OptionMethodNotImplementedError.new() end |
#map_none(f = nil) { ... } ⇒ Option<B>
In the case of ‘None’, applies ‘f’ or the block and returns a new ‘Option’ with the returned value. For ‘Some’, this method falls through.
91 92 93 |
# File 'lib/option.rb', line 91 def map_none(f=nil, &block) raise OptionMethodNotImplementedError.new() end |
#match(matcher) ⇒ R
Use pattern matching to work with both Some and None variants. This is useful when it is desirable to have both variants handled in the same location. It can also be useful when either variant can coerced into a non-Option type.
Ruby has no built-in pattern matching, but the next best thing is a Hash using the Option classes themselves as the keys.
Tests for this are found in Some and None’s tests.
upon.
210 211 212 213 214 215 216 |
# File 'lib/option.rb', line 210 def match(matcher) if self.kind_of?(None) matcher[self.class].call() else matcher[self.class].call(@data) end end |
#none? ⇒ Boolean
Determine if this is a MonadOxide::None. otherwise.
64 65 66 |
# File 'lib/option.rb', line 64 def none?() false end |
#or_else(f = nil) { ... } ⇒ Some<B> | None
For ‘None’, invokes ‘f’ or the block and returns the Option returned from that.
For ‘Some’, returns itself and the function/block are ignored.
This method is used for control flow based on ‘Option’ values.
‘or_else’ is desirable for chaining together other Option based operations, or doing transformations where flipping from a ‘None’ to a ‘Some’ is desired.
The ‘Some’ equivalent operation is @see Option#and_then.
The return type is enforced.
172 173 174 |
# File 'lib/option.rb', line 172 def or_else(f=nil, &block) raise OptionMethodNotImplementedError.new() end |
#some? ⇒ Boolean
Determine if this is a MonadOxide::Some. otherwise.
56 57 58 |
# File 'lib/option.rb', line 56 def some?() false end |
#unwrap ⇒ A
Dangerously access the ‘Option’ data. If this is a ‘None’, an exception will be raised. It is recommended to use this for tests only.
181 182 183 |
# File 'lib/option.rb', line 181 def unwrap() raise OptionMethodNotImplementedError.new() end |
#unwrap_none ⇒ nil
Dangerously access the ‘Option’ data. If this is a ‘Some’, an exception will be raised. It is recommended to use this for tests only.
190 191 192 |
# File 'lib/option.rb', line 190 def unwrap_none() raise OptionMethodNotImplementedError.new() end |