Created
October 28, 2017 22:25
-
-
Save dminuoso/316a0c55d3576ccea728ab73efdea799 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
module Kernel | |
def Just(o) | |
Maybe.of(o) | |
end | |
end | |
class Maybe | |
def self.of(value) | |
new(value) | |
end | |
def to_s | |
"Just #{@value}" | |
end | |
def inspect | |
to_s | |
end | |
private | |
def initialize(value) | |
@value = value | |
end | |
public | |
Nothing = new(nil) | |
def map(func = nil, &blk) | |
f = blk || func | |
if self == Nothing | |
return self | |
end | |
Maybe.of(f.call(@value)) | |
end | |
class << Nothing | |
def map(func = nil, &blk) | |
self | |
end | |
def to_s | |
"Nothing" | |
end | |
end | |
end | |
Nothing = Maybe::Nothing | |
module Kernel | |
def map(func = nil, &blk) | |
f = func || blk | |
-> (e) { e.class.instance_method(:map).bind(e).call(&f) } | |
end | |
end | |
double = -> (e) { e * 2 } | |
double‛ = map double | |
p double‛.([1,2,3,4]) | |
p double‛.([]) | |
p double‛.(Just(3)) | |
p double‛.(Nothing) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment