2

I'm training with Java 8 streams, trying to implement the same logic as in this Scala code I also wrote :

def solution = {
        var s   = "input" 
        var n   = Math.sqrt(s.length / 3).toInt
        val arr = s.split("\\s+").map(_.toInt)

        def prod(index : Int, count : Int = 4)(inc : Int) : Int = 
            if (count > 0) arr(index)*prod(index + inc,count - 1)(inc) else 1

        def prodInRange(rows : Seq[Int], cols : Seq[Int])(inc : Int) = 
            for (i <- rows ; j <- cols) yield prod(n*i + j)(inc)

        val seq = prodInRange(0 to 19,0 to 16)(  1  ) ++ prodInRange(0 to 16,0 to 19)(  n  ) ++ 
                  prodInRange(0 to 16,0 to 16)(n + 1) ++ prodInRange(3 to 19,0 to 16)(1 - n)
        seq.max
    }

I implemented prod in Java, nothing special about that, and the method solution is also quite the same. However, I'm having a hard time with prodInRange. I change it to maxProdInRange, which is supposed to compute not only all the products but also their maximum. Here is the code :

public static int maxProdInRange(IntStream rows, IntStream cols, int inc, List<Integer> input) {
        int n = (int) Math.sqrt(input.size());
        return rows.flatMap(i -> cols.map(j -> prod(i*n + j,inc,input))).max().getAsInt();
}

Nevertheless, I'm having an exception (see title) when calling maxProdInRange. According to the tests I ran to try to debug it, it occurrences only when I call max(). First, I would like to know why I get this error (I think it comes from a misconception about Java streams) and also a way to fix it.

Note that in all the calls to maxProdInRange have the form maxProdInRange(IntStream.range(a,b),IntStream.range(a,b),input) so rows and cols are neither operated nore closed when passed as parameters.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Dici
  • 25,226
  • 7
  • 41
  • 82
  • I **did** solve it, in Scala. And I could also solve it in Java in an imperative style. I worked on a solution but I made a mistake probably related with something I don't know about Java 8 streams. In this context, isn't my question acceptable ? – Dici Aug 12 '14 at 18:25
  • It is "kinda" acceptable, but the closing votes are mounting. Maybe you should simplify the problem and ask for help on the stream (which was already closed) instead of mixing the solution with the Euler problem. – Alexandre Santos Aug 12 '14 at 18:28
  • Fair point, I'm going to shorten it. – Dici Aug 12 '14 at 18:30
  • That's a pity that the question was closed because I cannot post the answer that I found. I thought that there was nothing wrong about `rows.flatMap(i -> cols.map(j -> prod(i*n + j,inc,input)))` because when I ran it commenting the call to max() on it, there was no error. However, I think that this line actually does nothing because flatMap is not a `terminal operation`. The Stream is "computed" only when a terminal operation is called on it. The problem was the IntStream `cols`, which was called several times. I corrected it by passing 4 integers to the function instead of 2 IntStream objects – Dici Aug 12 '14 at 19:24
  • ... and recreate the IntStream **in** the lambda expression so that it's a new Stream for each evaluation of the expression in `flatMap`. – Dici Aug 12 '14 at 19:25

0 Answers0