Processing-Mode Equality

Brian Goetz brian.goetz at oracle.com
Sun Feb 9 13:05:09 PST 2014


> Hi Doug, what do you mean by "updates" here?
>
> Is the following example a sanctioned usage of Stream?
>
>      widgetStream
>          .filter( w->w.getColor()==red )
>          .forEach( w->w.setColor(pink) );

If you think about it, it has to be.  Since forEach can *only* operate 
by side-effects, a forEachXxx op has to perform some mutation in order 
to have any meaning at all.  As long as those operations are 
*non-interfering* -- in other words, do not affect the behavior of the 
rest of the stream pipeline, this is fine.  And here, since the setColor 
is the last thing you are going to do with any element, mutating it in 
this way (assuming a reasonable implementation of setColor) is safe.

That said, this is about the extent of mutation that is sanctioned.  For 
example, this is not fine:

       widgetStream
           .filter( w->w.getColor()==red )
           .forEach( w-> { widgetStream.remove(w); });

since that interferes with the source.  Similarly, this also is not fine:

       widgetStream
           .map( w-> {
               if (seen.contains(w)) {
                   return f(w);
               }
               else {
                   seen.add(w);
                   return g(w);
               }
           })
           ....

since the stateful lambda here will interfere with other computations in 
the same pipeline.



More information about the lambda-dev mailing list