Skip to content

Instantly share code, notes, and snippets.

@frabbit
Last active December 31, 2015 11:29
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 frabbit/7979748 to your computer and use it in GitHub Desktop.
Save frabbit/7979748 to your computer and use it in GitHub Desktop.
interface Filterable<M, A> {
public function filter (f:A->Bool):Of<M,A>;
}
class MyArray<A> implements Filterable<MyArray<In>, A> {
var a:Array<A>;
public function new (?a:Array<A>) {
this.a = a == null ? [] else a;
}
public function filter(f:A->Bool):MyArray<A> {
return new MyArray(a.filter(f));
}
}
class MyList<A> implements Filterable<MyList<In>, A>
{
var a:List<A>;
public function new () {
this.a = a == null ? new List() else a;
}
public function filter(f:A->Bool):MyList<A> {
return new MyList(a.filter(f));
}
}
class Main {
// abstract over the type constructor
public static function withFilterable <M>(m:Filterable<M, Int>):Of<M,Int>
{
return m.filter(function (x) return x > 10);
}
public static function main () {
trace(withFilterable(new MyArray())); // the result is typed as MyArray<Int>
trace(withFilterable(new MyList())); // the result is typed as MyList<Int>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment