Class: Fear::Either::LeftProjection
- Inherits:
-
Object
- Object
- Fear::Either::LeftProjection
- Defined in:
- lib/fear/either/left_projection.rb
Overview
Projects an ‘Either` into a `Left`.
Instance Attribute Summary collapse
- #either ⇒ Fear::Either writeonly
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#any? {|value| ... } ⇒ Boolean
Returns
false
ifFear::Right
or returns the result of the application of the given predicate to theFear::Light
value. -
#each {|value| ... } ⇒ Fear::Either
(also: #apply)
Performs the given block if this is a
Fear::Left
. -
#find {|value| ... } ⇒ Fear::Option<Fear::Either>
(also: #detect)
Returns
Fear::None
if this is aFear::Right
or if the given predicate does not hold for the left value, otherwise, returns aFear::Left
. -
#flat_map {|value| ... } ⇒ Fear::Either
Returns the given block applied to the value from this
Fear::Left
or returns this if this is aFear::Right
. -
#get_or_else(*args) ⇒ Object
Returns the value from this
Fear::Left
or evaluates the given default argument if this is aFear::Right
. -
#include?(other_value) ⇒ Boolean
Returns
true
ifFear::Left
has an element that is equal (as determined by ==) toother_value
,false
otherwise. -
#initialize(either) ⇒ LeftProjection
constructor
A new instance of LeftProjection.
-
#map {|value| ... } ⇒ Object
Maps the block argument through
Fear::Left
. -
#select {|value| ... } ⇒ Fear::Either
Returns
Fear::Right
of value if the given predicate does not hold for the left value, otherwise, returnsFear::Left
. -
#to_a ⇒ Array
Returns an array containing the
Fear::Left
value or an empty array if this is aFear::Right
. -
#to_option ⇒ Fear::Option
Returns an
Fear::Some
containing theFear::Left
value or aFear::None
if this is aFear::Right
.
Constructor Details
#initialize(either) ⇒ LeftProjection
Returns a new instance of LeftProjection.
17 18 19 |
# File 'lib/fear/either/left_projection.rb', line 17 def initialize(either) @either = either end |
Instance Attribute Details
#either=(value) ⇒ Fear::Either
13 14 15 |
# File 'lib/fear/either/left_projection.rb', line 13 def either @either end |
Instance Method Details
#==(other) ⇒ Boolean
224 225 226 |
# File 'lib/fear/either/left_projection.rb', line 224 def ==(other) other.is_a?(self.class) && other.either == either end |
#any? {|value| ... } ⇒ Boolean
Returns false
if Fear::Right
or returns the result of the application of the given predicate to the Fear::Light
value.
169 170 171 172 173 174 175 176 |
# File 'lib/fear/either/left_projection.rb', line 169 def any?(&predicate) case either in Fear::Left(value) predicate.(value) in Fear::Right false end end |
#each {|value| ... } ⇒ Fear::Either Also known as: apply
Performs the given block if this is a Fear::Left
.
76 77 78 79 80 81 82 83 84 |
# File 'lib/fear/either/left_projection.rb', line 76 def each case either in Fear::Left(value) yield(value) either in Fear::Right either end end |
#find {|value| ... } ⇒ Fear::Option<Fear::Either> Also known as: detect
Returns Fear::None
if this is a Fear::Right
or if the given predicate does not hold for the left value, otherwise, returns a Fear::Left
.
212 213 214 215 216 217 218 219 |
# File 'lib/fear/either/left_projection.rb', line 212 def find(&predicate) case either in Fear::Left(value) if predicate.(value) Fear.some(either) in Fear::Either Fear.none end end |
#flat_map {|value| ... } ⇒ Fear::Either
Returns the given block applied to the value from this Fear::Left
or returns this if this is a Fear::Right
.
116 117 118 119 120 121 122 123 |
# File 'lib/fear/either/left_projection.rb', line 116 def flat_map case either in Fear::Left(value) yield(value) in Fear::Right either end end |
#get_or_else(&default) ⇒ Object #get_or_else(default) ⇒ Object
Returns the value from this Fear::Left
or evaluates the given default argument if this is a Fear::Right
.
54 55 56 57 58 59 60 61 |
# File 'lib/fear/either/left_projection.rb', line 54 def get_or_else(*args) case either in Fear::Left(value) value in Fear::Right args.fetch(0) { yield } end end |
#include?(other_value) ⇒ Boolean
Returns true
if Fear::Left
has an element that is equal (as determined by ==) to other_value
, false
otherwise.
30 31 32 33 34 35 36 37 |
# File 'lib/fear/either/left_projection.rb', line 30 def include?(other_value) case either in Fear::Left(x) x == other_value in Fear::Right false end end |
#map {|value| ... } ⇒ Object
Maps the block argument through Fear::Left
.
95 96 97 98 99 100 101 102 |
# File 'lib/fear/either/left_projection.rb', line 95 def map case either in Fear::Left(value) Fear.left(yield(value)) in Fear::Right either end end |
#select {|value| ... } ⇒ Fear::Either
Returns Fear::Right
of value if the given predicate does not hold for the left value, otherwise, returns Fear::Left
.
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/fear/either/left_projection.rb', line 190 def select(&predicate) case either in Fear::Right either in Fear::Left(value) if predicate.(value) either in Fear::Left either.swap end end |
#to_a ⇒ Array
Returns an array containing the Fear::Left
value or an empty array if this is a Fear::Right
.
149 150 151 152 153 154 155 156 |
# File 'lib/fear/either/left_projection.rb', line 149 def to_a case either in Fear::Left(value) [value] in Fear::Right [] end end |
#to_option ⇒ Fear::Option
Returns an Fear::Some
containing the Fear::Left
value or a Fear::None
if this is a Fear::Right
.
132 133 134 135 136 137 138 139 |
# File 'lib/fear/either/left_projection.rb', line 132 def to_option case either in Fear::Left(value) Fear.some(value) in Fear::Right Fear.none end end |