Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Unfamiliar Java syntax

Hey,

Can anyone tell me what this syntax is in Java?

invoke(() -> new Main());

What is "() ->" has something to do with a Runnable I'm assuming.

I can't find it in the docs though.

Lambdas guys.. new Java 8 operator. TIL.
Yeah it's the new lambdas.

Instead of writing something like new Runnable() {public void run() {}}

You just write () - >{your normal code}

Have a look at lambdas.
(Aug 27, 2015 01:25 AM)MrOrang Wrote:  Yeah it's the new lambdas.

Instead of writing something like new Runnable() {public void run() {}}

You just write () - >{your normal code}

Have a look at lambdas.

Yea I'll definitely take a 'Looka' ;)
inb4 Dan K doesn't like lambda expressions.

The usefulness of lambda expressions becomes apparent when you use lots of events and lists (and hasmaps - any collection of data really).
PHP Code

<?php 
hfMembers
.forEach(member -> member.kill()); 
It's useful for collections of data for filtering and looping through and comparing. In my opinion, it syntactically looks cleaner and is easy to follow. I tried lambda in C++ - never really picked up on it that much but Java's lambda expressions are incredibly straight forward. It's useful to learn.
(Aug 27, 2015 08:25 AM)WildeThing Wrote:  inb4 Dan K doesn't like lambda expressions.

He insults me in one way or another whenever he sees me using a lambda. I keep trying to talk him into using them though.

Also, Java 8 Streams can easily turn your code into spaghetti when trying to filter, convert data types, etc. I've had to avoid using them a handful of times because my code was simply unreadable.
(Aug 27, 2015 09:16 AM)clrlslwi Wrote:  He insults me in one way or another whenever he sees me using a lambda. I keep trying to talk him into using them though.

Also, Java 8 Streams can easily turn your code into spaghetti when trying to filter, convert data types, etc. I've had to avoid using them a handful of times because my code was simply unreadable.

I tried to get him into JavaFX one time and he now refers to it as a "badly designed mess" - not actual words but something similar on other thread.

Meh, I find that formatting it so that each method call to the new line (like the convention shows) makes it readable. It's not perfect but I can make out what's happening pretty easily and have never had to avoid using streams at all due to messy code.
As others have said this is a lambda expression. I believe this is the newest attempt to bring more functional concepts into Java.
You should take a look at Lambda functions in Haskell :-) It's, in my opinion, the best functional language.
Java's lambdas are syntactic sugar to produce anonymous classes that require one method to be implemented.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by BaldingSep 5, 2015 04:43 PM 1 233 Sep 5, 2015 04:43 PM
Last Post: Hinch
[Java] by President HolyFeb 8, 2014 11:48 AM 3 192 Feb 8, 2014 11:48 AM
Last Post: Knautschtabak
[.NET] by Drug balladSep 10, 2013 12:50 AM 9 167 Sep 10, 2013 12:50 AM
Last Post: TheBarret