Class: Kind::Either::Monad

Inherits:
Object
  • Object
show all
Defined in:
lib/kind/either/monad.rb

Direct Known Subclasses

Left, Right

Defined Under Namespace

Classes: Wrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Monad

Returns a new instance of Monad.



11
12
13
# File 'lib/kind/either/monad.rb', line 11

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/kind/either/monad.rb', line 7

def value
  @value
end

Instance Method Details

#===(monad) ⇒ Object



61
62
63
# File 'lib/kind/either/monad.rb', line 61

def ===(monad)
  self.class === monad && self.value === monad.value
end

#either?(matcher) ⇒ Boolean

Returns:



57
58
59
# File 'lib/kind/either/monad.rb', line 57

def either?(matcher)
  UNDEFINED == matcher || matcher === value
end

#left?Boolean

Returns:



15
16
17
# File 'lib/kind/either/monad.rb', line 15

def left?
  false
end

#map(&_) ⇒ Object Also known as: map!, then, then!, and_then, and_then!

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/kind/either/monad.rb', line 27

def map(&_)
  raise NotImplementedError
end

#on {|monad| ... } ⇒ Object

Yields:

  • (monad)


37
38
39
40
41
42
43
# File 'lib/kind/either/monad.rb', line 37

def on
  monad = Wrapper.new(self)

  yield(monad)

  monad.output
end

#on_left(matcher = UNDEFINED) {|value| ... } ⇒ Object

Yields:



51
52
53
54
55
# File 'lib/kind/either/monad.rb', line 51

def on_left(matcher = UNDEFINED)
  yield(value) if left? && either?(matcher)

  self
end

#on_right(matcher = UNDEFINED) {|value| ... } ⇒ Object

Yields:



45
46
47
48
49
# File 'lib/kind/either/monad.rb', line 45

def on_right(matcher = UNDEFINED)
  yield(value) if right? && either?(matcher)

  self
end

#right?Boolean

Returns:



19
20
21
# File 'lib/kind/either/monad.rb', line 19

def right?
  false
end

#value_or(_method_name = UNDEFINED, &block) ⇒ Object

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/kind/either/monad.rb', line 23

def value_or(_method_name = UNDEFINED, &block)
  raise NotImplementedError
end