IntPipelineError in a nested loop (original) (raw)
Paul Sandoz paul.sandoz at oracle.com
Tue Apr 9 04:28:32 PDT 2013
- Previous message: IntPipelineError in a nested loop
- Next message: hg: lambda/lambda/jdk: - Sync up spliterator tests into the spliterator test helper.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Apr 9, 2013, at 12:58 PM, Jose <jgetino at telefonica.net> wrote:
Thanks Paul, I forgot again that streams get conssumed!! :-( However there seems to be some more problems with the code, as I still get an error message like this: "Exception in thread "main" java.lang.IncompatibleClassChangeError: Class java.util.stream.IntPipeline does not implement the requested interface util.logger.functions.IntBiConsumer"
There is a bug related to static methods in interfaces that declare lambdas. Try:
public class Test {
public interface IntBiConsumer {
void accept(int n, int m);
}
static void fold(IntStream sx, Supplier<IntStream> ssy, IntBiConsumer bc) {
sx.forEach(nx -> ssy.get().forEach(ny -> bc.accept(nx, ny)));
}
public static void main(String[] args) throws Exception {
IntStream sx = Streams.intRange(0, 10);
Supplier<IntStream> ssy = () -> Streams.intRange(0, 5);
IntBiConsumer bc = (x, y) -> System.out.println("x: " + x + " y: " + y);
fold(sx, ssy, bc);
}
}
On the other hand I tried your suggestion of using:
flatMap() instead of fold() but then I get a linear stream, and I can't plug a biconsumer to the output.
Yes, i should have been clearer you are essentially creating your own "flat map" like thing from which you report elements to a bi-consumer. Its not really a fold since you are not providing an identity and an operator to combining pairs of elements into a result.
If you were to use flatMap you need to create an instance of a pair holding x & y. Unfortunately in Java this is painful since it lacks tuples :-(
Paul.
The biconsumer needs to be aware of both coordinates x, and y because in practical cases they have different meanings.
The new code, with flatMap included : import java.util.function.Supplier; import java.util.stream.IntStream; import java.util.stream.Streams;
@FunctionalInterface public interface IntBiConsumer { void accept(int n, int m); static void fold(IntStream sx, Supplier ssy, IntBiConsumer bc) { sx.forEach(nx -> ssy.get().forEach(ny -> bc.accept(nx, ny))); } class Demo { public static void main(String[] args) { Supplier ssx = () -> Streams.intRange(0, 10); Supplier ssy = () -> Streams.intRange(0, 5); IntBiConsumer bc = (x, y) -> System.out.println("x: " + x + ", y: " + y); /----------- FLAT ---------/ IntStream sx=ssx.get(); IntStream flatted= sx.flatMap(n -> ssy.get()); flatted.forEach(n -> System.out.println("n: " + n)); /----------- FOLD ---------/ sx=ssx.get(); fold(sx, ssy, bc); } } }
- Previous message: IntPipelineError in a nested loop
- Next message: hg: lambda/lambda/jdk: - Sync up spliterator tests into the spliterator test helper.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]