module Rstructural::Either

Constants

Left

Public Class Methods

left(obj) click to toggle source
# File lib/rstructural/either.rb, line 10
def self.left(obj)
  Left.new(obj)
end
right(obj) click to toggle source
# File lib/rstructural/either.rb, line 14
def self.right(obj)
  Right.new(obj)
end
try(catch_nil: false, &block) click to toggle source
# File lib/rstructural/either.rb, line 18
def self.try(catch_nil: false, &block)
  result = block.call
  if catch_nil && result.nil?
    Left.new(nil)
  else
    Right.new(result)
  end
rescue => e
  Left.new(e)
end

Public Instance Methods

flat_map(&f) click to toggle source
# File lib/rstructural/either.rb, line 61
def flat_map(&f)
  case self
  in Left
    self
  in Right[value]
    f.call(value)
  end
end
flat_map_left(&f) click to toggle source
# File lib/rstructural/either.rb, line 70
def flat_map_left(&f)
  case self
  in Left[value]
    f.call(value)
  in Right
    self
  end
end
left?() click to toggle source
# File lib/rstructural/either.rb, line 39
def left?
  !right?
end
left_or_else(default = nil) { || ... } click to toggle source
# File lib/rstructural/either.rb, line 101
def left_or_else(default = nil)
  case self
  in Left[value]
    value
  in Right
    if block_given?
      yield
    else
      default
    end
  end
end
map(&f) click to toggle source
# File lib/rstructural/either.rb, line 43
def map(&f)
  case self
  in Left
    self
  in Right[value]
    Right.new(f.call(value))
  end
end
map_left(&f) click to toggle source
# File lib/rstructural/either.rb, line 52
def map_left(&f)
  case self
  in Left[value]
    Left.new(f.call(value))
  in Right
    self
  end
end
right?() click to toggle source
# File lib/rstructural/either.rb, line 30
def right?
  case self
  in Left
    false
  in Right
    true
  end
end
right_or_else(default = nil) { || ... } click to toggle source
# File lib/rstructural/either.rb, line 88
def right_or_else(default = nil)
  case self
  in Right[value]
    value
  in Left
    if block_given?
      yield
    else
      default
    end
  end
end
swap() click to toggle source
# File lib/rstructural/either.rb, line 79
def swap
  case self
  in Left[value]
    Right.new(value)
  in Right[value]
    Left.new(value)
  end
end