Skip to content

Instantly share code, notes, and snippets.

@arsane
Last active September 10, 2016 17:48
Show Gist options
  • Save arsane/6429bdfd247fc8dab91c890e0099b94d to your computer and use it in GitHub Desktop.
Save arsane/6429bdfd247fc8dab91c890e0099b94d to your computer and use it in GitHub Desktop.
Optional = Struct.new(:value) do
def and_then(&block)
if value.nil?
Optional.new(nil)
else
block.call(value)
end
end
def method_missing(*args, &block)
and_then do |value|
Optional.new(value.public_send(*args, &block))
end
end
end
Many = Struct.new(:values) do
def and_then(&block)
Many.new(values.map(&block).flat_map(&:values))
end
def method_missing(*args, &block)
and_then do |value|
Many.new(value.public_send(*args, &block))
end
end
end
Eventually = Struct.new(:block) do
def initialize(&block)
super(block)
end
def run(&success)
block.call(success)
end
def and_then(&next_block)
Eventually.new do |success|
run do |value|
next_block.call(value).run(&success)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment