DevoxxUK Lambdas Lab

Brian Goetz brian.goetz at oracle.com
Wed Apr 3 08:14:42 PDT 2013


On 4/3/2013 10:08 AM, Richard Warburton wrote:
>>
>> 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 realize that the state of the documentation is currently woefully 
inadequate.  My question at this point is: is that our biggest problem 
at this point?  (If so, that would be a good thing.)

We've been struggling without how to present and document these 
complicated and tangled concepts in Javadoc, and its not easy.  (It 
would be much easier to document a "dumbed down" API.)  So, let me turn 
the question around: what else can we be doing to best help people make 
the conceptual leaps required?

> 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

Like many other things, this is only obvious in hindsight :(  Once you 
see the trick, its completely obvious and cool and you are unlikely to 
forget it.  (It's also extremely efficient and parallelizes well.)  So, 
how do we get people over that hump?  Or, is the problem worse, and you 
have concluded that the APIs are just "too advanced" for Java users?

> 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.

Yes.  This was a part of the motivation for including methods like sum() 
-- not only are they easy and obvious, but they provide a toehold in the 
docs to teach people that this is a special case of reduction.



More information about the lambda-dev mailing list