Created
December 17, 2012 12:31
-
-
Save dtchepak/4317956 to your computer and use it in GitHub Desktop.
Attempt at Maybe in Ruby
This file contains hidden or 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
| class Maybe | |
| def self.empty | |
| Maybe.new nil | |
| end | |
| def initialize(value) | |
| @value = value | |
| end | |
| def bind(f) | |
| @value.nil? ? self : f.call(@value) | |
| end | |
| def fmap(f) | |
| @value.nil? ? self : Maybe.new(f.call(@value)) | |
| end | |
| def to_s | |
| @value.nil? ? "Nothing" : "Just #{@value}" | |
| end | |
| def value_or(x) | |
| @value.nil? ? x : @value | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment