Skip to content

Instantly share code, notes, and snippets.

Created February 5, 2016 11:06
Show Gist options
  • Select an option

  • Save anonymous/673348c38a84ee06cc7f to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/673348c38a84ee06cc7f 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