Skip to content

Instantly share code, notes, and snippets.

@eric
Last active December 27, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric/7328974 to your computer and use it in GitHub Desktop.
Save eric/7328974 to your computer and use it in GitHub Desktop.
class Option
end
class None < Option
def map(&block)
return None
end
end
class Some < Option
def initialize(value)
@value = value
end
def map(&block)
return Some(block.call(@value))
end
end
None = None.new
def Some(value)
Some.new(value)
end
def method(input)
result = input.map { |v| "#{v} day" }.map { |v| "very #{v}" }
end
method(Some("happy")).map { |r| puts "Our result: #{r.inspect}" }
# Prints: very happy day
method(None).map { |r| puts "Our result: #{r.inspect}" }
# Prints nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment