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


Groups > comp.lang.java.programmer > #39039 > unrolled thread

getFibonacciArray() cannot call getFibonacciArray(int from)

Started byCecil Westerhof <Cecil@decebal.nl>
First post2019-07-31 11:52 +0200
Last post2019-08-03 11:11 +0200
Articles 5 — 3 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#39039 — getFibonacciArray() cannot call getFibonacciArray(int from)

FromCecil Westerhof <Cecil@decebal.nl>
Date2019-07-31 11:52 +0200
SubjectgetFibonacciArray() cannot call getFibonacciArray(int from)
Message-ID<87o91amsyc.fsf@munus.decebal.nl>
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

[toc] | [next] | [standalone]


#39055

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2019-08-01 19:15 +0200
Message-ID<qhv6p1$p7p$1@dont-email.me>
In reply to#39039
On 2019-07-31 11:52, Cecil Westerhof wrote:
> 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);
<snip />
> 
> What is happening here?
> 

If I had to hazard a guess, I would say that in jshell, you can't
reference a method before you've declared it (lexically speaking).

-- 
DF.

[toc] | [prev] | [next] | [standalone]


#39060

FromAndreas Leitgeb <avl@logic.at>
Date2019-08-02 06:51 +0000
Message-ID<slrnqk7nb4.36g.avl@logic.at>
In reply to#39055
Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> wrote:
> If I had to hazard a guess, I would say that in jshell, you can't
> reference a method before you've declared it (lexically speaking).

That was also my guess after reading Cecil's post.

Then I tried it, and found out I know nothing about jshell.

Whatever order I typed into it, it just gave continuation
prompts or standard prompts.  Sometimes it even went back
to standard prompt before I typed the last close brace, and
whatever I typed, I never got anything back.  Even typing 
   System.out.println(42);
gave me nothing but an empty line and a new prompt...

Most likely I've been doing something wrong...

[toc] | [prev] | [next] | [standalone]


#39069

FromCecil Westerhof <Cecil@decebal.nl>
Date2019-08-03 11:26 +0200
Message-ID<87a7cqmwg5.fsf@munus.decebal.nl>
In reply to#39055
Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> writes:

> On 2019-07-31 11:52, Cecil Westerhof wrote:
> If I had to hazard a guess, I would say that in jshell, you can't
> reference a method before you've declared it (lexically speaking).

No, that is not the problem. A method cannot call an overridden
version of itself in jshell, while it can in a class. :'-(

I have the following in test.jsh:
    void greeter() {
        printGreeting("Hello world.");
    }

    void printGreeting(String msg) {
        println(msg);
    }

    int getInt(int value) {
        return value;
    }

    int getInt() {
        return getInt(12);
    }

When I open this with '/open', I get:
    |  Error:
    |  method getInt in class  cannot be applied to given types;
    |    required: no arguments
    |    found: int
    |    reason: actual and formal argument lists differ in length
    |      return getInt(12);
    |             ^----^

So greeter does not have any problem with printGreeting not being
defined.

Another strange thing: getInt() is defined. But when I call it, I get
(as expected):
    |  Error:
    |  method getInt in class  cannot be applied to given types;
    |    required: no arguments
    |    found: int
    |    reason: actual and formal argument lists differ in length
    |      return getInt(12);
    |             ^----^

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

[toc] | [prev] | [next] | [standalone]


#39068

FromCecil Westerhof <Cecil@decebal.nl>
Date2019-08-03 11:11 +0200
Message-ID<87ef22mx4r.fsf@munus.decebal.nl>
In reply to#39039
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web