Method: Funkr::Types::Maybe#apply

Defined in:
lib/funkr/types/maybe.rb

#apply(to) ⇒ Object

see applicative apply

Maybe can be made an applicative functor, for example :

f = Maybe.curry_lift_proc{|x,y| x + y}
a = Maybe.just(3)
b = Maybe.just(4)
c = Maybe.nothing
f.apply(a).apply(b) => Just 7
f.apply(a).apply(c) => Nothing
[View source]

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/funkr/types/maybe.rb', line 54

def apply(to)
  # This implementation isn't safe but is a bit faster than the
  # safe one. A safe implementation would be as follow :
  #   self.match do |f_on|
  #     f_on.just do |f|
  #       to.match do |t_on|
  #         t_on.just {|t| self.class.unit(f.call(t)) }
  #         t_on.nothing { to }
  #       end
  #     end
  #     f_on.nothing { self }
  #   end
  if self.just? and to.just? then
    self.class.unit(self.unsafe_content.call(to.unsafe_content))
  else self.class.nothing end
end