News for hackers. Stuff that matters.
Re:Streams in Java 8 by barrkel on Sunday February 09, 2014 @07:45PM (#7208350)

    sorted(Comparator<? super T> comparator)
Eurgh. Why isn't this a projection to a comparable type (or better, a projection to a tuple of comparable types, for use with lexicographical ordering)?

Say you have class Country { int population; int name; }. You want the names of the top 3 countries by population:

    countries.orderByDesc(x -> x.population).limit(3).map(x -> x.name)
But instead, you must do something like:

    countries.sorted((x, y) -> Integer.compare(y.population, x.population).limit(3).map(x -> x.name)
It gets much uglier when you want a lexicographical ordering by several attributes.
Re:Streams in Java 8 by jargonjustin on Sunday February 09, 2014 @08:48PM (#7208600)
Java's Comparator type is more general (you don't necessarily need to project the type for comparison) and an existing abstraction. It's not hard to introduce a combinator to transform the representation as a library, as in:

    countries.sorted(by(x -> x.population)).limit(3).map(x -> x.name)
where `by` looks something like:

    <S, T> Comparator<T> by(Projection<S, T> projection);

    interface Projection<S, T> {
       Comparable<S> project(T value);
    }
Re:Streams in Java 8 by barrkel on Sunday February 09, 2014 @11:53PM (#7209219)
Sure; and you'd have to import the thing that contains 'by'.

It's still ugly, and would prevent e.g. a radix sort being used when projecting an integer as the sort key, or American flag sort for strings.

Re:Streams in Java 8 by javafu2 on Monday February 10, 2014 @02:50AM (#7209683)
It already exists in the API. Your type Projection is a Function, and by is called comparing in interface Comparator.

so one can write: ``` contries.sorted(comparingInt(Country::getPopulation)). ... ```