Class: Right

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) ⇒ Right

Returns a new instance of Right.



104
105
106
# File 'lib/totally_lazy/either.rb', line 104

def initialize(value)
  @value = value
end

Instance Method Details

#<=>(other) ⇒ Object



143
144
145
# File 'lib/totally_lazy/either.rb', line 143

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

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

a function which returns an either



134
135
136
137
# File 'lib/totally_lazy/either.rb', line 134

def flat_map(fn=nil, &block) # a function which returns an either
  assert_funcs(fn, block_given?)
  block_given? ? block.call(@value) : fn.(@value)
end

#fold(seed, fn_left, fn_right) ⇒ Object



139
140
141
# File 'lib/totally_lazy/either.rb', line 139

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

#is_left?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/totally_lazy/either.rb', line 108

def is_left?
  false
end

#is_right?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/totally_lazy/either.rb', line 112

def is_right?
  true
end

#left_valueObject



116
117
118
# File 'lib/totally_lazy/either.rb', line 116

def left_value
  raise NoSuchElementException.new
end

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



124
125
126
127
# File 'lib/totally_lazy/either.rb', line 124

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

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



129
130
131
132
# File 'lib/totally_lazy/either.rb', line 129

def map_left(fn=nil, &block)
  assert_funcs(fn, block_given?)
  self
end

#right_valueObject



120
121
122
# File 'lib/totally_lazy/either.rb', line 120

def right_value
  @value
end

#to_sObject



147
148
149
# File 'lib/totally_lazy/either.rb', line 147

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