Anonymous user
List comprehensions: Difference between revisions
Updated Java code to use IntStreams to do the list comprehension bit. Technically cleaner, but still clunky.
(Updated Java code to use IntStreams to do the list comprehension bit. Technically cleaner, but still clunky.) |
|||
Line 957:
Using list-of-arrays made the syntax easier than list-of-lists, but meant that you need the "output expression" part to get to something easily printable.
<lang Java>// Boilerplate
import java.util.Arrays;
import java.util.List;
import static java.util.
import static java.util.stream.Collectors.toList;
public class PythagComp{▼
import static java.util.stream.IntStream.range;
public static void main(String[] args){▼
System.out.println(run(20));
}
Line 968 ⟶ 969:
static List<List<Integer>> run(int n){
return
// Here comes the list comprehension bit
// input stream - bit clunky
.flatMap(identity())
.flatMap(identity())
// predicate
.filter(a -> a[0]*a[0] + a[1]*a[1] == a[2]*a[2])
// output expression
.map(
// the result is a list
.collect(
;
}
}
|