Last active
December 27, 2015 12:49
-
-
Save eric/7328974 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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