-
-
Save PavelPenkov/a9656edfbb659da4d33b 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 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