Module: Either
- Includes:
- Comparable, Control::Monad, UnionType
- Defined in:
- lib/data.either.rb
Overview
‘Either a b` is either `Left a` or `Right b`
Class Method Summary collapse
- .lefts(list_of_either) ⇒ value
- .partition(list_of_either) ⇒ [l], [r]
- .rights(list_of_either) ⇒ value
-
.try ⇒ Either
try something could raise exception, and wrap value into Right, exception into Left.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
comparable - Left < Right.
-
#bimap(lfn, rfn) ⇒ Either
‘bimap` accept 2 lambdas, if it’s [Right], apply the 2nd lambda, otherwise apply to the first lambda.
- #each(&block) ⇒ Object
-
#flat_map ⇒ Either
it override Monad#flat_map, as Haskell’s ‘>flat_map` method if it’s Right, pass the value to #flat_map’s block, and flat the result of the block.
-
#get_or_else(e) ⇒ Object
(also: #|)
get value ‘a` out from `Right a`, otherwise return `e` “`ruby Right.new(1).get_or_else(2) # => 1 Right.new(1) | 2 # => 1 “`.
-
#initialize(v = nil) ⇒ Either
Either only contain one value @v.
- #inspect ⇒ String (also: #to_s)
- #left? ⇒ Boolean
-
#left_map ⇒ Either
the opposit of #map, apply function to ‘Left e`, do nothing if it’s ‘Right a`.
-
#map ⇒ Either
overide of Functor’s ‘fmap`, apply function on `Right a`’s value ‘a`, do nothing if it’s ‘Left e`.
- #or_else(e) ⇒ Object
- #right? ⇒ Boolean
- #to_a ⇒ Object
-
#~@ ⇒ Either
(also: #swap)
swap type of [Either].
Methods included from UnionType
Methods included from Helper
Methods included from Control::Monad
Methods included from Control::Applicative
Class Method Details
.lefts(list_of_either) ⇒ value
205 206 207 208 |
# File 'lib/data.either.rb', line 205 def lefts(list_of_either) list_of_either.select(&:left?) .map { |left| left.when(Left: ->(l) { l }) } end |
.partition(list_of_either) ⇒ [l], [r]
216 217 218 219 220 221 222 |
# File 'lib/data.either.rb', line 216 def partition(list_of_either) list_of_either.inject([[], []]) do |acc, x| x.when(Left: ->(l) { acc[0].push(l) }, Right: ->(r) { acc[1].push(r) }) acc end end |
.rights(list_of_either) ⇒ value
194 195 196 197 |
# File 'lib/data.either.rb', line 194 def rights(list_of_either) list_of_either.select(&:right?) .map { |right| right.get_or_else(nil) } end |
Instance Method Details
#<=>(other) ⇒ Object
comparable
-
Left < Right
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/data.either.rb', line 166 def <=>(other) other =~ case self when Right { Right: ->(o) { @v <=> o }, Left: ->(_) { 1 } } else { Right: ->(_) { -1 }, Left: ->(o) { @v <=> o } } end end |
#bimap(lfn, rfn) ⇒ Either
‘bimap` accept 2 lambdas, if it’s [Right], apply the 2nd lambda, otherwise apply to the first lambda
“‘ ruby
Right.new(1).bimap ->(x){x-1}, ->(x){x+1} # => 2
Left.new(1).bimap ->(x){x-1}, ->(x){x+1}) # => 0
“‘
106 107 108 109 110 111 112 113 |
# File 'lib/data.either.rb', line 106 def bimap(lfn, rfn) case self when Right Right.new(rfn.call(@v)) else Left.new(lfn.call(@v)) end end |
#each(&block) ⇒ Object
151 152 153 |
# File 'lib/data.either.rb', line 151 def each(&block) bimap(->(_) {}, &block) end |
#flat_map ⇒ Either
it override Monad#flat_map, as Haskell’s ‘>flat_map` method if it’s Right, pass the value to #flat_map’s block, and flat the result of the block.
when it’s Left, do nothing “‘ ruby expect(Right.new(1).flat_map { |x| Left.new } ).to eq(Left.new) expect(Left.new(1).flat_map { |x| Left.new } ).to eq(Left.new(1)) “`
125 126 127 128 129 130 131 132 |
# File 'lib/data.either.rb', line 125 def flat_map case self when Right yield @v else self end end |
#get_or_else(e) ⇒ Object Also known as: |
get value ‘a` out from `Right a`, otherwise return `e` “`ruby Right.new(1).get_or_else(2) # => 1 Right.new(1) | 2 # => 1 “`
47 48 49 50 51 52 53 54 |
# File 'lib/data.either.rb', line 47 def get_or_else(e) case self when Right @v else e end end |
#initialize(v = nil) ⇒ Either
Either only contain one value @v
27 28 29 |
# File 'lib/data.either.rb', line 27 def initialize(v = nil) @v = v end |
#inspect ⇒ String Also known as: to_s
178 179 180 181 182 183 184 185 |
# File 'lib/data.either.rb', line 178 def inspect case self when Left "#<Left #{@v.inspect}>" else "#<Right #{@v.inspect}>" end end |
#left? ⇒ Boolean
33 34 35 |
# File 'lib/data.either.rb', line 33 def left? false end |
#left_map ⇒ Either
90 91 92 93 94 95 96 97 |
# File 'lib/data.either.rb', line 90 def left_map case self when Left Left.new(yield @v) else self end end |
#map ⇒ Either
74 75 76 77 78 79 80 81 |
# File 'lib/data.either.rb', line 74 def map case self when Right Right.new(yield @v) else self end end |
#or_else(e) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/data.either.rb', line 58 def or_else(e) case self when Right else e end end |
#right? ⇒ Boolean
38 39 40 |
# File 'lib/data.either.rb', line 38 def right? false end |
#to_a ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/data.either.rb', line 155 def to_a case self when Right [@v] else [] end end |