Class: Maybe

Inherits:
Struct
  • Object
show all
Extended by:
MonadPlus
Includes:
Monad
Defined in:
lib/do_notation/monads/maybe.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MonadPlus

guard

Methods included from Monad

#>>, #bind_const, included, #joinM, #reflect_that_doesnt_work

Instance Attribute Details

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



3
4
5
# File 'lib/do_notation/monads/maybe.rb', line 3

def value
  @value
end

Class Method Details

.mzeroObject



20
21
22
# File 'lib/do_notation/monads/maybe.rb', line 20

def self.mzero
  unit(nil)
end

.unit(value) ⇒ Object



6
7
8
# File 'lib/do_notation/monads/maybe.rb', line 6

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

Instance Method Details

#bind(&f) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/do_notation/monads/maybe.rb', line 10

def bind &f
  if value.nil?
    self
  else
    f.call(value)
  end
end

#mplus(m) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/do_notation/monads/maybe.rb', line 24

def mplus m
  if value.nil?
    m
  else
    self
  end
end