module Maybe
The `Either` union type represents values with two possibilities:
`Either a b` is either `Left a` or `Right b`
Public Class Methods
Either only contain one value @v @return [Either]
# File lib/data.maybe.rb, line 12 def initialize(v = nil) @v = v end
Public Instance Methods
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)) “` @return [Either]
# File lib/data.maybe.rb, line 52 def flat_map case self when Just yield @v else self end end
get value `a` out from `Right a`, otherwise return `e`
# File lib/data.maybe.rb, line 17 def get_or_else(e) case self when Just @v else e end end
overide of Functor's `fmap`, apply function on `Right a`'s value `a`, do nothing if it's `Left e`
“` ruby
Right.new(1).map {|x| x + 1} # => #<Right value=2> Left.new(1).map {|x| x + 1} # => #<Left value=1>
“` @return [Either]
# File lib/data.maybe.rb, line 33 def map case self when Just Just.new(yield @v) else self end end
@return [String]
# File lib/data.maybe.rb, line 81 def to_s case self when Just "#<Just #{@v}>" else '#<Nothing>' end end
similar to Scala's `match` for case class
will pattern match the value out and pass to matched lambda
“` ruby Right.new(1).when({Right: ->x{x+1} }) # => 2 Right.new(1).when({Left: ->x{x+1}) # => nil Right.new(1).when({Left: ->x{x+1}, _: ->x{x-1} }) # => 0 “` @return [Either]
# File lib/data.maybe.rb, line 71 def when(what) current_class = self.class.to_s.to_sym if what.include? current_class what[current_class].call(@v) elsif what.include? :_ what[:_].call(@v) end end