Skip to content

Instantly share code, notes, and snippets.

@anissen
Created November 3, 2015 07:12
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 anissen/cdd57069afde771a6550 to your computer and use it in GitHub Desktop.
Save anissen/cdd57069afde771a6550 to your computer and use it in GitHub Desktop.
Test of pseudo-monads in haxe
import haxe.ds.Option;
using Test.OptionTools;
class OptionTools {
static public function fmap<T>(m :Option<T>, f :T->T) :Option<T> {
switch m {
case Some(x): return Some(f(x));
case None: return None;
}
}
static public function afmap<T>(m :Array<Option<T>>, f :T->T) :Array<Option<T>> {
return m.map(function(o) { return fmap(o, f); });
}
}
class Test {
static function main() {
function add3(x) {
return x + 3;
}
var val = Some(4);
trace(val.fmap(add3));
trace('---------');
var vals = [Some(2), None, Some(3)];
var res = vals.afmap(add3);
for (v in res) trace(v);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment