Skip to content

Instantly share code, notes, and snippets.

@trygvea
Created April 26, 2015 21:27
Show Gist options
  • Save trygvea/f08c7361907ef9555287 to your computer and use it in GitHub Desktop.
Save trygvea/f08c7361907ef9555287 to your computer and use it in GitHub Desktop.
// Tested in groovy 2.4.3
import groovy.transform.TypeChecked
@TypeChecked
interface F1<A, B> { // Function1
abstract B f(A a);
}
@TypeChecked
interface Functor<T, A> { // Egentlig Functor<T<A>> men det går ikke i java/groovy
abstract <A,B> T<B> map(F1<A, B> f)
}
@TypeChecked
class MyList extends ArrayList<Integer> implements Functor<List, Integer> {
MyList(List src) {
super(src)
}
List<String> map (F1 f) {
this.collect { f.f(it) }
}
}
def list = new MyList([1,2,3])
list.map {(it+2).toString()}
list.map {(it+2)} // So much for type safety ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment