Skip to content

Instantly share code, notes, and snippets.

@PavelPenkov
Forked from anonymous/maybe.rb
Created February 5, 2016 11:08
Show Gist options
  • Save PavelPenkov/a9656edfbb659da4d33b to your computer and use it in GitHub Desktop.
Save PavelPenkov/a9656edfbb659da4d33b to your computer and use it in GitHub Desktop.
class None
def map(&block)
self
end
def flat_map(&block)
self
end
def value
raise "Value not present"
end
end
class Some
def initialize(value)
@value = value
end
def map(&block)
val = yield @value
Some.new(val)
end
def flat_map(&block)
yield @value
end
def value
@value
end
end
Some.new(2).flat_map{|x| Some.new(1).map {|y| x + y}} # => Some(3)
Some.new(2).flat_map{|x| None.map {|y| x + y}} # => None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment