Created
November 27, 2015 06:10
-
-
Save domgetter/083e8f591b21c4a90a1f 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
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) |
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 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