Skip to content

Instantly share code, notes, and snippets.

@samwgoldman samwgoldman/result.rb
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.