Skip to content

Instantly share code, notes, and snippets.

@mainyaa
Created October 4, 2012 02:53
Show Gist options
  • Save mainyaa/3831200 to your computer and use it in GitHub Desktop.
Save mainyaa/3831200 to your computer and use it in GitHub Desktop.
This is Functional Programming on Java
List<Integer> listI = list(1, list(2, list(3, list(4, list(5, list(6, emptyList()))))));
Integer sum = listI.filter(new Function1<Integer, Boolean>() {
public Boolean apply(Integer i){ return i % 2 == 0; }
})
.map(new Function1<Integer, Integer>() {
public Boolean apply(Integer i) { return i * 2; }
})
.foldLeft(0, new Function2<Integer, Integer, Integer>(){
public Integer apply(Integer seed, Integer item){ return seed + item; }
});
assertEquals(new Integer(24, sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment