Created
March 6, 2013 23:18
-
-
Save deltaluca/5104163 to your computer and use it in GitHub Desktop.
pipe dream haxe
This file contains 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
typedef Monad<M> = { | |
static function bind<A,B>(a:M<A>, f:A->M<B>):M<B>; | |
static function ret<A>(a:A):M<A>; | |
} | |
enum MaybeImpl<A> { | |
mJust(a:A); | |
mNothing); | |
} | |
abstract Maybe(MaybeImpl) from MaybeImpl to MaybeImpl { | |
static function bind<A,B>(a:Maybe<A>, f:A->Maybe<B>):Maybe<B> { | |
return switch (a) { case mJust(a): f(a); default: mNothing; }; | |
} | |
static function ret<A>(a:A):Maybe<A> return mJust(a); | |
} | |
class Func { | |
static function liftM<M:Monad<M>,A,B>(f:A->B):M<A>->M<B> { | |
return function (m1:M<A>):M<B> { | |
return M.bind(m1, function (a:A):M<B> return M.ret(f(a))); | |
}; | |
} | |
} | |
class Main { | |
static function main() { | |
var add1 = function (a) return a+1; | |
trace(liftM(add1)(mJust(10)); // mJust(11) | |
trace(liftM(add1)(mNothing)); // mNothing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment