-
-
Save frabbit/87f69012620499a747c9 to your computer and use it in GitHub Desktop.
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
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