The following java code:
public static void main (String[] args) {
List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
List twoEvenSquares =
numbers.stream()
.filter(n -> {
System.out.println("filtering " + n);
return n % 2 == 0;
})
.map(n -> {
System.out.println("mapping " + n);
return n * n;
})
.limit(2)
.collect(() -> new ArrayList<>(),
(c, e) -> c.add(e),
(c1, c2) -> c1.addAll(c2));
System.out.println("Output: " + twoEvenSquares);
}
yields:
filtering 1
filtering 2
mapping 2
filtering 3
filtering 4
mapping 4
Output: [4, 16]
The ostensibly equivalent JRuby code:
numbers = java.util.Arrays.asList(1,2,3,4,5,6,7,8)
filter_lamb = ->(n) {
puts “filtering #{n}”
n % 2 == 0
}
map_lamb = ->(n) {
puts “mapping #{n}”
n*n
}
l1 = -> { java.util.ArrayList.new }
l2 = -> (c,e) { c.add(e) }
l3 = -> (c1,c2) { c1.addAll(c2)}
two_even_squares =
numbers.stream().filter(filter_lamb).map(map_lamb).limit(2).collect(l1,l2,l3)
bombs out with:
ArgumentError: wrong number of arguments (1 for 2)
in the collect call.
What am I doing wrong?
Thanks,
Cris