Last active
August 29, 2015 14:20
-
-
Save he9lin/2ccb74bd0c8bc7724538 to your computer and use it in GitHub Desktop.
functional-ruby Option extension
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 Functional | |
class Option | |
def map(fn) | |
if some? | |
self.class.some(fn.call(value)) | |
else | |
self.class.none(reason) | |
end | |
end | |
def self.flatten(result) | |
if result.some? | |
result.value # which is an Option obj | |
else | |
none result.reason | |
end | |
end | |
def >=(fn) | |
self.class.flatten(map(fn)) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment