Created
April 26, 2015 21:27
-
-
Save trygvea/f08c7361907ef9555287 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
// 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