Skip to content

Instantly share code, notes, and snippets.

@dtchepak
Created December 17, 2012 12:31
Show Gist options
  • Select an option

  • Save dtchepak/4317956 to your computer and use it in GitHub Desktop.

Select an option

Save dtchepak/4317956 to your computer and use it in GitHub Desktop.
Attempt at Maybe in Ruby
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