Last active
August 29, 2015 14:00
-
-
Save samwgoldman/5c76574ff5f58a90afc7 to your computer and use it in GitHub Desktop.
This file contains 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
module Result | |
class << self | |
def not_nil(value) | |
if value.nil? | |
None | |
else | |
Some.new(value) | |
end | |
end | |
def some(value) | |
Some.new(value) | |
end | |
def none | |
None | |
end | |
end | |
None = Object.new | |
class << None | |
def flat_map | |
self | |
end | |
def map | |
self | |
end | |
def or_else | |
yield | |
end | |
def get_or_else | |
yield | |
end | |
def for_each | |
end | |
end | |
class Some | |
def initialize(value) | |
@value = value | |
end | |
def flat_map | |
yield @value | |
end | |
def map | |
Some.new(yield @value) | |
end | |
def or_else | |
self | |
end | |
def get_or_else | |
@value | |
end | |
def for_each(&block) | |
flat_map(&block) | |
nil | |
end | |
end | |
private_constant :None, :Some | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment