Class: Left

Inherits:
Either show all
Defined in:
lib/totally_lazy/either.rb

Instance Method Summary collapse

Methods inherited from Either

#flatten, left, right

Constructor Details

#initialize(value) ⇒ Left

Returns a new instance of Left.



60
61
62
# File 'lib/totally_lazy/either.rb', line 60

def initialize(value)
  @value = value
end

Instance Method Details

#<=>(other) ⇒ Object



94
95
96
# File 'lib/totally_lazy/either.rb', line 94

def <=>(other)
  @value <=> other.left_value
end

#flat_map(fn = nil, &block) ⇒ Object

a function which returns an either



85
86
87
88
# File 'lib/totally_lazy/either.rb', line 85

def flat_map(fn=nil, &block) # a function which returns an either
  assert_funcs(fn, block_given?)
  self
end

#fold(seed, fn_left, fn_right) ⇒ Object



90
91
92
# File 'lib/totally_lazy/either.rb', line 90

def fold(seed, fn_left, fn_right)
  fn_left.(seed, @value)
end

#is_left?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/totally_lazy/either.rb', line 64

def is_left?
  true
end

#is_right?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/totally_lazy/either.rb', line 68

def is_right?
  false
end

#left_valueObject



72
73
74
# File 'lib/totally_lazy/either.rb', line 72

def left_value
  @value
end

#map_left(fn = nil, &block) ⇒ Object



80
81
82
83
# File 'lib/totally_lazy/either.rb', line 80

def map_left(fn=nil, &block)
  assert_funcs(fn, block_given?)
  left(block_given? ? block.call(@value) : fn.(@value))
end

#right_valueObject



76
77
78
# File 'lib/totally_lazy/either.rb', line 76

def right_value
  raise NoSuchElementException.new
end

#to_sObject



98
99
100
# File 'lib/totally_lazy/either.rb', line 98

def to_s
  "left(#{@value})"
end