Created
October 27, 2017 12:19
-
-
Save dminuoso/3d19c3bdbf864751ffa43e125e81f130 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
double = -> (e) { e * 2 } | |
doubleF = map double | |
doubleF.([1,2,3,4]) #=> [2,4,6,8] | |
doubleF.([]) #=> [] | |
doubleF.(Just(3)) #=> Just 6 | |
doubleF.(Nothing) #=> Nothing |
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
module Kernel | |
def map(func = nil, &blk) | |
f = func || blk | |
-> (e) { e.class.instance_method(:map).bind(e).call(&f) } | |
end | |
end |
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
module Kernel | |
def Just(o) | |
Maybe.of(o) | |
end | |
end | |
class Maybe | |
def self.of(value) | |
new(value) | |
end | |
def map(callable) | |
Maybe.of(callable.call(@value)) | |
end | |
def to_s | |
"Just #{@value}" | |
end | |
implements Functor | |
implements Applicative | |
private | |
def initialize(value) | |
@value = value | |
end | |
public | |
Nothing = new(nil) | |
class << Nothing | |
def map(callable) | |
self | |
end | |
def to_s | |
"Nothing" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment