Updated State of the Collections

Brian Goetz brian.goetz at oracle.com
Fri Nov 16 06:44:55 PST 2012


Mostly.  Think of the Sink argument as the workaround for not having 
"yield" in the language.

On 11/16/2012 6:26 AM, Jose wrote:
> Thanks for the explanations!
>
> I think I'm starting to get the idea:
>
> - I don't have to see flatMap as function of two arguments, because I'm only
> allowed to provide one.
> - The first argument is just a workaround to access (but not to set) an
> internal variable of the flapMap method.
> - The way this argument is used in the flatMap body defines a concrete
> flavor of the "old" flatMap version.
>
> I'm correct?
>
>
>
>
>    _____
>
> De: Michael Hixson [mailto:michael.hixson at gmail.com]
> Enviado el: viernes, 16 de noviembre de 2012 11:14
> Para: Jose
> CC: lambda-dev at openjdk.java.net
> Asunto: Re: Updated State of the Collections
>
>
> The sink is provided to you by the stream - you don't create it yourself.
> So in this example:
>
>    List<Book> books = authors.stream()
>        .<Book>flatMap((sink, author) -> author.getBooks().forEach(sink))
>        .into(new ArrayList<Book>());
>
> the object behind the "sink" variable was created by the stream upon calling
> .flatMap.  The flatMap method is basically saying, "I'm giving you a sink.
> It's what I'm using to build the result.  For each author, use the sink to
> mark which books will appear in the result."  This lambda:
>
>    (sink, author) -> author.getBooks().forEach(sink)
>
> instructs the sink to include every book for every author in the result.
>
> I agree that it's confusing.  Without looking at the method signature, I
> expected flatMap to do that dumping into the sink for me.  Something like
> this:
>
>    // Note: this doesn't work
>    List<Book> books = authors.stream()
>        .flatMap(author -> author.getBooks())
>        .into(new ArrayList<Book>());
>
> But I see why they didn't write flatMap that way.  In my example, I have
> author.getBooks() which returns a collection.  If I didn't have a method
> like that, or I didn't want to create a throwaway collection just to hold
> each author's books (which will all then get merged into one collection),
> then if they only gave me a flatMap(A -> Collection<B>) method I'd be hosed.
> The fact that they give me the sink lets me pick and choose the elements in
> the result individually from wherever I want.
>
> -Michael
>
>
> On Fri, Nov 16, 2012 at 1:40 AM, Jose <jgetino at telefonica.net> wrote:
>
>
>
>
> So flatMap function has two arguments.  For the second one you provide a
> List of authors that you defined previously.
>
> But what about the first argument?,  sink is just a variable, it has not
> being initialized at any place
>
> I find this like defining the square function as:
>
> sq=(x,y)->y*y
>
> and then using it in this way
>
> sq(x,3)==9
>
>
>
>
>
> -----Mensaje original-----
> De: Michael Hixson [mailto:michael.hixson at gmail.com]
> Enviado el: viernes, 16 de noviembre de 2012 8:30
> Para: Jose
> CC: lambda-dev at openjdk.java.net
> Asunto: Re: Updated State of the Collections
>
>
> flatMap caused me a lot of trouble as well.  Eventually I figured out you
> can use it without casting, declaring anonymous classes, or using a helper
> method like this:
>
> List<Author> authors = ...
> // Assume author.getBooks() returns List<Book> List<Book> books =
> authors.stream()
>      .<Book>flatMap((sink, author) -> author.getBooks().forEach(sink)
>      .into(new ArrayList<Book>());
> // Assume author.getDeceasedDate() returns Optional<Date> List<Date>
> deceasedDates = authors.stream()
>      .<Date>flatMap(sink, author) ->
> author.getDeceasedDate().ifPresent(sink))
>      .into(new ArrayList<Date>());
>
> The generic for the flatMap invocations ("<Book>.flatMap" and
> "<Date>flatMap" above) seem to be necessary.  I don't know if that was
> because of the IDE I was using (IntelliJ 12 EAP) or because that's just how
> it has to be.
>
>
>
> On Thu, Nov 15, 2012 at 10:58 PM, Jose <jgetino at telefonica.net> wrote:
>
>
>
>          Would you mind to provide an example of the use of flatMap?.
>
>          I need to adapt old b56 code like this:
>
>          Set<Polyline> lines(mapName){};
>          namesColl.flatMap(name -> lines(name)).into(...);
>
>          to the new sintax but I have no idea how to start with (do I have to
> provide
>          a Block.instance?)
>
>          I made some web search on flatMap and jdk8 but everyone seems to be
> talking
>          about Optional.
>          And in the repos I couln't find a single example, only discussions
> about the
>          sintax.
>
>
>
>
>          -----Mensaje original-----
>          De: lambda-dev-bounces at openjdk.java.net
>          [mailto:lambda-dev-bounces at openjdk.java.net] En nombre de Brian
> Goetz
>          Enviado el: jueves, 15 de noviembre de 2012 19:32
>          Para: lambda-dev at openjdk.java.net
>          Asunto: Updated State of the Collections
>
>          is here: http://cr.openjdk.java.net/~briangoetz/lambda/sotc3.html
>
>
>
>
>
>
>
>
>
>
>


More information about the lambda-dev mailing list