Skip to content

Instantly share code, notes, and snippets.

@he9lin
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save he9lin/2ccb74bd0c8bc7724538 to your computer and use it in GitHub Desktop.
Save he9lin/2ccb74bd0c8bc7724538 to your computer and use it in GitHub Desktop.
functional-ruby Option extension
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