Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39039
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Cecil Westerhof <Cecil@decebal.nl> |
| Newsgroups | comp.lang.java.programmer |
| Subject | getFibonacciArray() cannot call getFibonacciArray(int from) |
| Date | Wed, 31 Jul 2019 11:52:43 +0200 |
| Organization | Decebal Computing |
| Lines | 118 |
| Message-ID | <87o91amsyc.fsf@munus.decebal.nl> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | reader02.eternal-september.org; posting-host="43d00c68d6ba140619e0c8523007a8b7"; logging-data="12527"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Oi5kbAof2z8mgMJ4NOzF9id/RU5P73qc=" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux) |
| Cancel-Lock | sha1:M65QqcQwo8GKu1j3HRoBAHgXgMs= sha1:AEYriKBL7on8d47YkY0SkQeHcwA= |
| Xref | csiph.com comp.lang.java.programmer:39039 |
Show key headers only | View raw
I have the following code I use in jshell:
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 I call this with:
jshell --startup DEFAULT ~/Java/Jshell/test.jsh
I get:
| Error:
| method getFibonacciArray in class cannot be applied to given types;
| required: no arguments
| found: int
| reason: actual and formal argument lists differ in length
| return getFibonacciArray(0);
| ^---------------^
| Error:
| cannot find symbol
| symbol: class
| LongStream getFibonacciStreamFrom(int from) {
| ^
| Error:
| static import only from classes and interfaces
| LongStream getFibonacciStreamFrom(int from) {
| ^
| Welcome to JShell -- Version 9-Debian
| For an introduction type: /help intro
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
So it has no problem with the method:
long[] getFibonacciArray()
instead it has a problem with:
LongStream getFibonacciStream()
What is happening here?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll 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