Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Created December 22, 2013 21:41
Show Gist options
  • Save davidbalbert/8088766 to your computer and use it in GitHub Desktop.
Save davidbalbert/8088766 to your computer and use it in GitHub Desktop.
require 'singleton'
class NothingClass
include Singleton
def bind
self
end
def map
self
end
def inspect
"Nothing"
end
end
Nothing = NothingClass.instance
class Just
attr_reader :value
def initialize(val)
@value = val
end
def bind
ret = yield value
unless ret.is_a?(Just) || ret.is_a?(NothingClass)
raise "The block passed to bind must return a Just or Nothing"
end
ret
end
def map
bind { |v| Just(yield(v)) }
end
def inspect
"Just(#{value})"
end
end
def Just(value)
Just.new(value)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment