Class: Rumonade::Either::LeftProjection

Inherits:
Object
  • Object
show all
Includes:
Monad
Defined in:
lib/rumonade/either.rb

Overview

Projects an Either into a Left.

Constant Summary

Constants included from Monad

Monad::METHODS_TO_REPLACE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Monad

#each, #flat_map_with_monad, #flatten_with_monad, included, #shallow_flatten

Constructor Details

#initialize(either_value) ⇒ LeftProjection

Returns a new instance of LeftProjection.

Parameters:

  • either_value (Object)

    the Either value to project



171
172
173
# File 'lib/rumonade/either.rb', line 171

def initialize(either_value)
  @either_value = either_value
end

Instance Attribute Details

#either_valueObject (readonly)

Returns the Either value

Returns:

  • Returns the Either value



176
177
178
# File 'lib/rumonade/either.rb', line 176

def either_value
  @either_value
end

Class Method Details

.emptyLeftProjection

Returns the empty LeftProjection

Returns:



165
166
167
# File 'lib/rumonade/either.rb', line 165

def empty
  self.new(Right(nil))
end

.unit(value) ⇒ LeftProjection

Returns a LeftProjection of the Left of the given value

Returns:

  • (LeftProjection)

    Returns a LeftProjection of the Left of the given value



160
161
162
# File 'lib/rumonade/either.rb', line 160

def unit(value)
  self.new(Left(value))
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if other is a LeftProjection with an equal Either value

Returns:

  • (Boolean)

    Returns true if other is a LeftProjection with an equal Either value



179
180
181
# File 'lib/rumonade/either.rb', line 179

def ==(other)
  other.is_a?(LeftProjection) && other.either_value == self.either_value
end

#all?(lam = nil, &blk) ⇒ Boolean

Returns true if Right or returns the result of the application of the given function to the Left value.

Returns:

  • (Boolean)

    Returns true if Right or returns the result of the application of the given function to the Left value.



201
202
203
# File 'lib/rumonade/either.rb', line 201

def all?(lam = nil, &blk)
  !either_value.left? || bind(lam || blk)
end

#any?(lam = nil, &blk) ⇒ Boolean

Returns false if Right or returns the result of the application of the given function to the Left value.

Returns:

  • (Boolean)

    Returns false if Right or returns the result of the application of the given function to the Left value.



191
192
193
# File 'lib/rumonade/either.rb', line 191

def any?(lam = nil, &blk)
  either_value.left? && bind(lam || blk)
end

#bind(lam = nil, &blk) ⇒ Object

Binds the given function across Left.



184
185
186
# File 'lib/rumonade/either.rb', line 184

def bind(lam = nil, &blk)
  if !either_value.left? then either_value else (lam || blk).call(either_value.left_value) end
end

#getObject

Returns the value from this Left or raises NoSuchElementException if this is a Right.



206
207
208
# File 'lib/rumonade/either.rb', line 206

def get
  if either_value.left? then either_value.left_value else raise NoSuchElementError end
end

#get_or_else(val_or_lam = nil, &blk) ⇒ Object

Returns the value from this Left or the given argument if this is a Right.



211
212
213
214
# File 'lib/rumonade/either.rb', line 211

def get_or_else(val_or_lam = nil, &blk)
  v_or_f = val_or_lam || blk
  if either_value.left? then either_value.left_value else (v_or_f.respond_to?(:call) ? v_or_f.call : v_or_f) end
end

#inspectString

Returns a String containing a human-readable representation of this object.

Returns:

  • (String)

    Returns a String containing a human-readable representation of this object.



232
233
234
# File 'lib/rumonade/either.rb', line 232

def inspect
  "LeftProjection(#{either_value.inspect})"
end

#map(lam = nil, &blk) ⇒ Either

Returns Maps the function argument through Left.

Returns:

  • (Either)

    Maps the function argument through Left.



222
223
224
# File 'lib/rumonade/either.rb', line 222

def map(lam = nil, &blk)
  bind { |v| Left((lam || blk).call(v)) }
end

#select(lam = nil, &blk) ⇒ Option

Returns None if this is a Right or if the given predicate does not hold for the left value, otherwise, returns a Some of Left.

Returns:

  • (Option)

    Returns None if this is a Right or if the given predicate does not hold for the left value, otherwise, returns a Some of Left.



196
197
198
# File 'lib/rumonade/either.rb', line 196

def select(lam = nil, &blk)
  Some(self).select { |lp| lp.any?(lam || blk) }.map { |lp| lp.either_value }
end

#to_optOption

Returns a Some containing the Left value if it exists or a None if this is a Right.

Returns:

  • (Option)

    Returns a Some containing the Left value if it exists or a None if this is a Right.



217
218
219
# File 'lib/rumonade/either.rb', line 217

def to_opt
  Option(get_or_else(nil))
end

#to_sString

Returns a String representation of this object.

Returns:

  • (String)

    Returns a String representation of this object.



227
228
229
# File 'lib/rumonade/either.rb', line 227

def to_s
  "LeftProjection(#{either_value})"
end