Skip to content

Instantly share code, notes, and snippets.

@deltaluca
Created March 6, 2013 23:18
Show Gist options
  • Save deltaluca/5104163 to your computer and use it in GitHub Desktop.
Save deltaluca/5104163 to your computer and use it in GitHub Desktop.
pipe dream haxe
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