Skip to content

Instantly share code, notes, and snippets.

@frabbit
Last active December 31, 2015 11:29
Show Gist options
  • Save frabbit/87f69012620499a747c9 to your computer and use it in GitHub Desktop.
Save frabbit/87f69012620499a747c9 to your computer and use it in GitHub Desktop.
interface Mappable<M, A> {
public function map<B>(f:A->B):Of<M,B>;
}
class MyArray<A> implements Mappable<MyArray<In>, A> {
var a:Array<A>;
public function new () {
this.a = [];
}
public function map<B>(f:A->B):MyArray<B> {
var r = new MyArray();
r.a = a.map(f);
return r;
}
}
class MyList<A> implements Mappable<MyList<In>, A>
{
var a:List<A>;
public function new () {
this.a = new List();
}
public function map<B>(f:A->B):MyList<B> {
var r = new MyList();
r.a = a.map(f);
return r;
}
}
class Main {
// abstract over the type constructor
public static function withMappable <M>(m:Mappable<M, Int>):Of<M,Int> {
return m.map(function (x) return x + 1);
}
public static function main () {
trace(withMappable(new MyArray()));
trace(withMappable(new MyList()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment