Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39068

Re: getFibonacciArray() cannot call getFibonacciArray(int from)

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.java.programmer
Subject Re: getFibonacciArray() cannot call getFibonacciArray(int from)
Date 2019-08-03 11:11 +0200
Organization Decebal Computing
Message-ID <87ef22mx4r.fsf@munus.decebal.nl> (permalink)
References <87o91amsyc.fsf@munus.decebal.nl>

Show all headers | View raw


Cecil Westerhof <Cecil@decebal.nl> writes:

> When I turn it into a class it becomes really strange. I changed the
> code to:
>     import java.util.stream.LongStream;
>     import java.util.stream.Stream;
>
>
>     class Fibonacci {
>         long[] getFibonacciArray() {
>             // return getFibonacciArray(0);
>             return getFibonacciStreamFrom(0).toArray();
>         }
>
>         long[] getFibonacciArray(int from) {
>             return getFibonacciStreamFrom(from).toArray();
>         }
>
>         long[] getFibonacciArray(int from, int to) {
>             // check parameters
>             int count = to - from;
>             return getFibonacciStreamFrom(from).limit(count).toArray();
>         }
>
>         // Needs at least Java 9 because we use a predicate in the iterator
>         // Index 0 of stream i element always contains fibonacci(i) and
>         // index 1 always contains fibonacci(i + 1)
>         // So the map makes sure you have the fibonacci number
>         LongStream getFibonacciStream() {
>             return Stream.
>                 iterate(new long[]{0, 1}, t -> t[0] >= 0, t -> new long[]{t[1], t[0] + t[1]})
>                 .mapToLong(t -> t[0]);
>         }
>
>         LongStream getFibonacciStreamFrom(int from) {
>             // Check parameter
>             return getFibonacciStream().skip(from).map(l -> l);
>         }
>     }
>
> When compiling this, I get:
>     Fibonacci.java:26: error: method iterate in interface Stream<T#2> cannot be applied to given types;
>             return Stream.
>                          ^
>       required: T#1,UnaryOperator<T#1>
>       found: long[],(t)->t[0] >= 0,(t)->new l[...]t[1]}
>       reason: cannot infer type-variable(s) T#1
>         (actual and formal argument lists differ in length)
>       where T#1,T#2 are type-variables:
>         T#1 extends Object declared in method <T#1>iterate(T#1,UnaryOperator<T#1>)
>         T#2 extends Object declared in interface Stream
>     1 error

This was because jshell was using Java 9, and the compiler Java 8.
This is Java 9 functionality, but the error message was not clear. I
have both updated to 11.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

getFibonacciArray() cannot call getFibonacciArray(int from) Cecil Westerhof <Cecil@decebal.nl> - 2019-07-31 11:52 +0200
  Re: getFibonacciArray() cannot call getFibonacciArray(int from) Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2019-08-01 19:15 +0200
    Re: getFibonacciArray() cannot call getFibonacciArray(int from) Andreas Leitgeb <avl@logic.at> - 2019-08-02 06:51 +0000
    Re: getFibonacciArray() cannot call getFibonacciArray(int from) Cecil Westerhof <Cecil@decebal.nl> - 2019-08-03 11:26 +0200
  Re: getFibonacciArray() cannot call getFibonacciArray(int from) Cecil Westerhof <Cecil@decebal.nl> - 2019-08-03 11:11 +0200

csiph-web