Created
December 22, 2013 21:41
-
-
Save davidbalbert/8088766 to your computer and use it in GitHub Desktop.
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
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