Class: Funkr::Types::Failable

Inherits:
ADT show all
Extended by:
Monad::ClassMethods, Monoid::ClassMethods
Includes:
Alternative, Applicative, Functor, Categories, Monad, Monoid
Defined in:
lib/funkr/types/failable.rb

Constant Summary

Constants inherited from ADT

ADT::MATCHER

Instance Method Summary collapse

Methods inherited from ADT

adt, #initialize, #match, matcher, #to_s, #unsafe_const, #unsafe_data

Constructor Details

This class inherits a constructor from Funkr::ADT

Instance Method Details

#apply(to) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/funkr/types/failable.rb', line 29

def apply(to)
  self.match do |f_on|
    f_on.ok do |f|
      to.match do |t_on|
        t_on.ok {|t| self.class.ok(f.call(t)) }
        t_on.failed { to }
      end
    end
    f_on.failed { self }
  end
end

#bind(&block) ⇒ Object



71
72
73
74
75
76
# File 'lib/funkr/types/failable.rb', line 71

def bind(&block)
  case self.const
  when :ok then yield(*self.data)
  else self
  end
end

#map(&block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/funkr/types/failable.rb', line 19

def map(&block)
  case self.const
  when :ok then
    self.class.ok(yield(*self.data))
  else self
  end
end

#mplus(m_y) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/funkr/types/failable.rb', line 55

def mplus(m_y)
  self.match do |x_on|
    x_on.failed { m_y }
    x_on.ok do |x|
      m_y.match do |y_on|
        y_on.failed { self }
        y_on.ok {|y| self.class.ok(x.mplus(y))}
      end
    end
  end
end

#or_else(&block) ⇒ Object



44
45
46
47
48
49
# File 'lib/funkr/types/failable.rb', line 44

def or_else(&block)
  case self.const
  when :ok then self
  else yield
  end
end