Class: Collins::Option
- Inherits:
-
Object
- Object
- Collins::Option
- Defined in:
- lib/collins/option.rb
Overview
This is pretty much a straight rip off of the scala version
Represents optional values. Instances of ‘Option` are either an instance of `Some` or `None`
Instance Method Summary collapse
-
#defined? ⇒ Boolean
True if the value is defined.
-
#empty? ⇒ Boolean
True if the value is undefined.
-
#exists?(&predicate) ⇒ Boolean
Return true if non-empty and predicate is true for the value.
-
#filter {|predicate| ... } ⇒ Option<Object>
Convert to ‘None` if predicate fails.
-
#filter_not(&predicate) ⇒ Option<Object>
Inverse of ‘filter` operation.
-
#flat_map(&block) ⇒ Option<Object>
Same as map, but flatten the results.
-
#foreach(&f) ⇒ NilClass
Apply the block specified to the value if non-empty.
-
#get ⇒ Object
Value, if defined.
-
#get_or_else(*default) { ... } ⇒ Object
The value associated with this option, or the default.
-
#map {|block| ... } ⇒ Option<Object>
If the option value is defined, apply the specified block to that value.
-
#or_else(*default) ⇒ Option<Object>
Return this ‘Option` if non-empty, otherwise return the result of evaluating the default.
Instance Method Details
#defined? ⇒ Boolean
Returns True if the value is defined.
34 35 36 |
# File 'lib/collins/option.rb', line 34 def defined? !empty? end |
#empty? ⇒ Boolean
Returns True if the value is undefined.
29 30 31 |
# File 'lib/collins/option.rb', line 29 def empty? raise NotImplementedError.new("empty? not implemented") end |
#exists?(&predicate) ⇒ Boolean
Return true if non-empty and predicate is true for the value
90 91 92 |
# File 'lib/collins/option.rb', line 90 def exists? &predicate !empty? && predicate.call(get) end |
#filter {|predicate| ... } ⇒ Option<Object>
Convert to ‘None` if predicate fails
Returns this option if it is non-empty and applying the predicate to this options returns true. Otherwise return ‘None`.
149 150 151 152 153 154 155 |
# File 'lib/collins/option.rb', line 149 def filter &predicate if empty? || predicate.call(get) then self else None.new end end |
#filter_not(&predicate) ⇒ Option<Object>
Inverse of ‘filter` operation.
Returns this option if it is non-empty and applying the predicate to this option returns false. Otherwise return ‘None`.
164 165 166 167 168 169 170 |
# File 'lib/collins/option.rb', line 164 def filter_not &predicate if empty? || !predicate.call(get) then self else None.new end end |
#flat_map(&block) ⇒ Option<Object>
Same as map, but flatten the results
This is useful when operating on an object that will return an ‘Option`.
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/collins/option.rb', line 128 def flat_map &block if empty? then None.new else res = block.call(get) if res.is_a?(Some) then res else Some.new(res) end end end |
#foreach(&f) ⇒ NilClass
Apply the block specified to the value if non-empty
96 97 98 99 100 101 |
# File 'lib/collins/option.rb', line 96 def foreach &f if self.defined? then f.call(get) end nil end |
#get ⇒ Object
Returns Value, if defined.
40 41 42 |
# File 'lib/collins/option.rb', line 40 def get raise NotImplementedError.new("get not implemented") end |
#get_or_else(*default) { ... } ⇒ Object
The value associated with this option, or the default
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/collins/option.rb', line 55 def get_or_else *default if empty? then if block_given? then yield else default.first end else get end end |
#map {|block| ... } ⇒ Option<Object>
If the option value is defined, apply the specified block to that value
111 112 113 114 115 116 117 |
# File 'lib/collins/option.rb', line 111 def map &block if empty? then None.new else Some.new(block.call(get)) end end |
#or_else(*default) ⇒ Option<Object>
Return this ‘Option` if non-empty, otherwise return the result of evaluating the default
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/collins/option.rb', line 71 def or_else *default if empty? then res = if block_given? then yield else default.first end if res.is_a?(Option) then res else ::Collins::Option(res) end else self end end |