Method: Funkr::Types::Maybe#map

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

#map(&block) ⇒ Object

see functor map

Maybe.nothing.map{|x| something(x)} # => nothing
Maybe.just(x).map{|x| something(x)} # => just something(x)


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

def map(&block)
  # This implementation isn't safe but is a bit faster than the
  # safe one. A safe implementation would be as follow :
  #   self.match do |on|
  #     on.just {|v| self.class.just(yield(v))}
  #     on.nothing { self }
  #   end
  if self.just? then self.class.just(yield(unsafe_content))
  else self end
end