Hack Forums
[Java] Unfamiliar Java syntax - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Unfamiliar Java syntax (/showthread.php?tid=4965026)



Unfamiliar Java syntax - Avocado - Aug 26, 2015

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.


* - MrOrang - Aug 27, 2015

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.


* - Avocado - Aug 27, 2015

(Aug 27, 2015 01:25 AM)Looka 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' ;)


* - WildeThing - Aug 27, 2015

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.


* - clrlslwi - Aug 27, 2015

(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.


* - WildeThing - Aug 27, 2015

(Aug 27, 2015 09:16 AM)Demmonic 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.


* - csrk - Aug 27, 2015

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.


* - Jire - Sep 3, 2015

Java's lambdas are syntactic sugar to produce anonymous classes that require one method to be implemented.