DevoxxUK Lambdas Lab

Richard Warburton richard.warburton at gmail.com
Wed Apr 3 07:08:17 PDT 2013


>
> If the problem was computing word frequency, the reason it is horrible
> is not the API :)
>
>    Map<String, Integer> m
>       = reader.lines()
>               .flatMap(s -> Arrays.stream(s.split(" ")))
>               .groupingBy(Functions.identity(),
>                           reducing(s -> 1, Integer::sum)));
>

This is pretty much Identical to the code that Stuart produced, and its
perfectly fine as far as addressing the question "Can you write an elegant
solution to the problem using the streams api?"  Unfortunately the
important question is "Will blue collar Java developers be able to write a
usable solution to the problem using the streams api?"

My conclusion from running this lab is no.  None of the people who attended
realised that the use of the downstream collector for the values of the
group was important in writing the code.  I think this issue could be
solved with better documentation pointing them to look at that method.

I tested the hypothetical that this documentation was in place by pointing
two attendees of the lab, who had successfully solved the easier exercises,
in its direction.  They still didn't produce "reducing(s -> 1,
Integer::sum)".  I'm honestly quite sceptical that people would make that
leap.  I don't think many developers when asked "produce something that
collects the count of a list" Will intuitively go in the direction of:

[ "dad", "dad", "dad" ] -> [1, 1, 1] -> 3

Elsewhere in this thread there is a suggestion that a counting method
should be provided.  That's not only a good suggestion because counting
things is a common method of collecting them, but its also a good
suggestion to use this as an example, in the javadoc, in order to help
developers build the intuition of this kind of Collector.

I'm not quite as negative as Stephen on the issue, because when getting
people to try out the streams API I think broadly people have picked things
up easily and been able to comprehend the problems quickly.  This is
definitely one of the areas where they struggled a lot.

regards,

  Richard Warburton

  http://insightfullogic.com
  @RichardWarburto <http://twitter.com/richardwarburto>


More information about the lambda-dev mailing list