Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Created October 28, 2017 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/316a0c55d3576ccea728ab73efdea799 to your computer and use it in GitHub Desktop.
Save dminuoso/316a0c55d3576ccea728ab73efdea799 to your computer and use it in GitHub Desktop.
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