Last active
September 10, 2016 17:48
-
-
Save arsane/6429bdfd247fc8dab91c890e0099b94d 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
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