Skip to content

Instantly share code, notes, and snippets.

@samwgoldman
Last active August 29, 2015 14:00
Show Gist options
  • Save samwgoldman/5c76574ff5f58a90afc7 to your computer and use it in GitHub Desktop.
Save samwgoldman/5c76574ff5f58a90afc7 to your computer and use it in GitHub Desktop.
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