Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with a Lambda Expression  RSS feed

 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I acheive the same result in Java defined in the following C++ code snippet:


I've tried to do the following:


If I comment out everything after the expression it compiles fine as can be seen in the snapshot...

But when I try to call it, it fails complaining about not finding the identifier...

Can you fine experts help me solve this mystery of mine?
SuccessOnCommentedOutCode.png
[Thumbnail for SuccessOnCommentedOutCode.png]
Expression compiled fine with call commented out
FailureOnUncommentedCode.png
[Thumbnail for FailureOnUncommentedCode.png]
Code failed to compile with a call to the closure
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.function.Function is a functional interface whose functional method is apply(Object). You need to invoke that method, not use betweenOneAndTen as if it were a method itself.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That cleared up my unstructured thoughts....
What I was doing was trying to use the reference to the implementation as the method and not actually calling the implemented method...
Oh boy! This polyglot thing is really getting to me...
Trying to manage 14 and still got 2 more to add
 
Campbell Ritchie
Sheriff
Posts: 53766
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you write this?

apply(Random::nextInt)

I don't know. I suspect not.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Random::nextInt won't work, but random::nextInt would.

On a side note, I think an IntPredicate (with method test, not apply) is perhaps a better choice than Function<Integer, Boolean>. It does mostly the same, but does not use boxing.
 
Campbell Ritchie
Sheriff
Posts: 53766
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Rob
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob Thank you for pointing that out... IntPredicate is the perfect choice
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You too are welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!