Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created November 27, 2015 06:10
Show Gist options
  • Save domgetter/083e8f591b21c4a90a1f to your computer and use it in GitHub Desktop.
Save domgetter/083e8f591b21c4a90a1f to your computer and use it in GitHub Desktop.
class Identity
def initialize(obj)
@obj = obj
end
def self.wrap(obj)
Identity.new(obj)
end
def bind(func)
func.call(@obj)
end
def value
@obj
end
def ==(o)
@obj == o.value
end
end
val = 3
iden_val = Identity.wrap(val)
# inc is of type \n -> Identity
inc = -> n { Identity.wrap(n + 1) }
iden = -> n { Identity.wrap(n) }
doub_inc = -> n {inc[n].bind(inc)}
iden_val.bind(inc) == inc[val]
iden_val.bind(iden) == iden_val
iden_val.bind(doub_inc) == iden_val.bind(inc).bind(inc)
class Maybe
def initialize(obj)
@obj = obj
end
def self.wrap(obj)
Maybe.new(obj)
end
def bind(func)
if @obj.nil?
nil
else
func.call(@obj)
end
end
def value
@obj
end
def ==(o)
@obj == o.value
end
end
val = 3
i = Maybe.wrap(val)
inc = -> n { Maybe.wrap(Maybe.wrap(n).bind(-> n {n + 1})) }
iden = -> n { Maybe.wrap(n) }
doub_inc = -> n {inc[n].bind(inc)}
i.bind(inc) == inc[val]
i.bind(iden) == i
i.bind(doub_inc) == i.bind(inc).bind(inc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment