Created
November 3, 2015 07:12
-
-
Save anissen/cdd57069afde771a6550 to your computer and use it in GitHub Desktop.
Test of pseudo-monads in 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
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