Class: Maybe

Inherits:
Object
  • Object
show all
Extended by:
ADT
Defined in:
lib/data/maybe.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ADT

cases, operation

Class Method Details

.from_nil(v) ⇒ Object



34
35
36
# File 'lib/data/maybe.rb', line 34

def self.from_nil(v)
  v.nil? ? nothing : just(v)
end

Instance Method Details

#bindObject



14
15
16
# File 'lib/data/maybe.rb', line 14

def bind
  fold(proc { self }, proc { |v| yield v })
end

#filterObject



18
19
20
# File 'lib/data/maybe.rb', line 18

def filter
  fold(proc { self }, proc { |v| yield(v) ? self : Maybe.nothing })
end

#mapObject



10
11
12
# File 'lib/data/maybe.rb', line 10

def map
  fold(proc { self }, proc { |v| self.class.just(yield v) })
end

#or_else(other = nil) ⇒ Object



22
23
24
# File 'lib/data/maybe.rb', line 22

def or_else(other = nil)
  fold(proc { other || yield }, proc { |v| self }) 
end

#or_nilObject



30
31
32
# File 'lib/data/maybe.rb', line 30

def or_nil
  fold(proc { nil }, proc { |v| v })
end

#or_value(v = nil) ⇒ Object



26
27
28
# File 'lib/data/maybe.rb', line 26

def or_value(v = nil)
  fold(proc { v || yield }, proc { |v| v })
end