Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Created October 27, 2017 12:19
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/3d19c3bdbf864751ffa43e125e81f130 to your computer and use it in GitHub Desktop.
Save dminuoso/3d19c3bdbf864751ffa43e125e81f130 to your computer and use it in GitHub Desktop.
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
module Kernel
def map(func = nil, &blk)
f = func || blk
-> (e) { e.class.instance_method(:map).bind(e).call(&f) }
end
end
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