Java 8 Lambda Syntax has been (mostly) Finalized

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 16868 Views - Last Post: 15 September 2011 - 07:20 AM Rate Topic: -----

#16 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 09 September 2011 - 10:13 PM

View PostRaynes, on 10 September 2011 - 01:32 AM, said:

View Postfarrell2k, on 09 September 2011 - 06:23 PM, said:

Lambdas are a big confusing mess. I look at that syntax and want to avoid it at all costs.

c# is superior to Java in many ways. I especially like the fact that I can implements two interfaces with the same method names. That being said, beyond doing development for Xbox or windows phone, I see no reason to use c#, ever.


Lambdas are a very high level and powerful programming tool that absolutely *no* modern programming language should be without. I highly suggest that you investigate lambdas and functional programming a bit more before making such harsh judgements. I'm certain you'll find that you can do things with them that you wouldn't have otherwise thought possible.


They seem to be nothing but syntactic sugar.

x => x + x;

is equivalent to:

x += x;

right?
Was This Post Helpful? 0
  • +
  • -

#17 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,455
  • Joined: 08-June 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 06:51 AM

Yes, they are syntactic sugar, but no, that's not the equivalent.

x => x + x;

is equivalent to

int anonFunc(x){
return x + x;
}

Assuming that the context it was used in was an integer (the type is inferred). It's not really useful on it's own, but here's an example of where it's useful:

var list = Enumerable.Range(1,100);
var res = list.Where(x => x % 5 == 0).ToList();


That's C# code, btw. res is now a list of integers divisible by five from 5 to 100. The "Where" method takes a delegate (similar to a function pointer) that matches a signature of bool function(int), and applies every value to it. The ones that return true are included in the resulting collection.

It is very useful for things like LINQ.
Was This Post Helpful? 2
  • +
  • -

#18 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 07:21 AM

I ♥ Linq. Seriously.
Was This Post Helpful? 0
  • +
  • -

#19 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 610
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 08:58 AM

View Postfarrell2k, on 10 September 2011 - 06:13 AM, said:

View PostRaynes, on 10 September 2011 - 01:32 AM, said:

View Postfarrell2k, on 09 September 2011 - 06:23 PM, said:

Lambdas are a big confusing mess. I look at that syntax and want to avoid it at all costs.

c# is superior to Java in many ways. I especially like the fact that I can implements two interfaces with the same method names. That being said, beyond doing development for Xbox or windows phone, I see no reason to use c#, ever.


Lambdas are a very high level and powerful programming tool that absolutely *no* modern programming language should be without. I highly suggest that you investigate lambdas and functional programming a bit more before making such harsh judgements. I'm certain you'll find that you can do things with them that you wouldn't have otherwise thought possible.


They seem to be nothing but syntactic sugar.

x => x + x;

is equivalent to:

x += x;

right?


Just no. http://en.wikipedia....nymous_function


View PostCurtis Rutland, on 10 September 2011 - 02:51 PM, said:

Yes, they are syntactic sugar, but no, that's not the equivalent.

x => x + x;

is equivalent to

int anonFunc(x){
return x + x;
}


Syntactic sugar for what? Usually, they're less syntactic sugar and more language feature. If you mean they're syntactic sugar for creating a function with no name, then I suppose the entire language is syntactic sugar for generating bytecodes. ;)

This post has been edited by Raynes: 10 September 2011 - 09:04 AM

Was This Post Helpful? 0
  • +
  • -

#20 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,455
  • Joined: 08-June 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 12:06 PM

In C#, they're "syntactic sugar" for creating anonymous functions. The older way to do it was like this:

delegate(int x){ return x + x; };


Lambda syntax:

x => x + x;

Was This Post Helpful? 0
  • +
  • -

#21 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,351
  • Joined: 29-May 08

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 12:57 PM

View PostSergio Tapia, on 10 September 2011 - 03:21 PM, said:

I ♥ Linq. Seriously.


Linq isn't Lambdas, it Language INtegrated Query which is a pattern. LINQ Style is
    Dim mynumbers = Enumerable.Range(0, 100)
    Dim multiples = From x In myNumbers
                Where (x Mod 3) = 0 OrElse (x Mod 5) = 0
    Dim total = Aggregate x In multiples Into Sum()
                             
 


Coding like its 1936
Was This Post Helpful? 0
  • +
  • -

#22 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1251
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 02:01 PM

I know, but I love Linq.
Was This Post Helpful? 0
  • +
  • -

#23 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 610
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 10 September 2011 - 04:34 PM

View PostCurtis Rutland, on 10 September 2011 - 08:06 PM, said:

In C#, they're "syntactic sugar" for creating anonymous functions. The older way to do it was like this:

delegate(int x){ return x + x; };


Lambda syntax:

x => x + x;


Oh, I see what you're saying. In most circles, lambdas are considered synonymous with anonymous functions because they both result in the same thing. It didn't click that you were using the literal definition of the term.

As you were.
Was This Post Helpful? 0
  • +
  • -

#24 Viper2KX  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 25-January 09

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 13 September 2011 - 11:12 AM

Java has just died for me... I love my 1.3 to 1.4.2. And I am slowly getting to 5... but now after I move on. I am only stopping at 7.
Was This Post Helpful? -1
  • +
  • -

#25 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,455
  • Joined: 08-June 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 13 September 2011 - 12:35 PM

If I understand you correctly, you're the reason the language is stagnating. The world is moving forward, and you have to accept the fact that you need to learn new things. Java has barely progressed in the last 8 years, and here you are ready to abandon it because they're talking about including something that most modern languages have had for a while. When Java stops geting updates, and everyone else is moving forward, remember that you asked for it.
Was This Post Helpful? 4
  • +
  • -

#26 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 14 September 2011 - 07:29 PM

As far as I can tell, from my C++0x experience lambdas are only particularly useful when you need a short and simple callback function (such as in the example of sorting a list). Don't get me wrong, it's a great use, but I'm wondering if there are any other uses for lambda expressions.
Was This Post Helpful? 0
  • +
  • -

#27 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 14 September 2011 - 07:37 PM

Lambda! Finally!
So, we wait for another 5 years for it to come out on Java 8?
Right.
Was This Post Helpful? 0
  • +
  • -

#28 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 14 September 2011 - 11:23 PM

I look at Curtis' example with the Enumerable range, and it looks nice. It's two nice lines of code for something that would take a few more lines to do in Java, but It's just a sort cut. I don't see how they progress the language at all. I suppose it is good to have options, however.
Was This Post Helpful? 0
  • +
  • -

#29 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4309
  • View blog
  • Posts: 7,455
  • Joined: 08-June 10

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 15 September 2011 - 06:49 AM

I'm not surprised it's not obviously useful to people who haven't had it to use. I thought it was pointless as well, until I started using features that relied on it, and they started making sense.

It's not just for simple callbacks. Another example is to map a particular function across a collection. Methods that take lambdas are what makes up C#'s LINQ, which enables querying collections. My first example was simple, but you can do complex things. Here's an example of parsing XML:

var Resp = messageSet.Descendants(xs).Attributes("name")
	.Where (resp => resp.Value.Contains("Response") || resp.Value.Contains("Reply"))
	.OrderBy (resp => resp.Value)
	.Select (resp => new {
		Key = resp.Value.Substring(0, resp.Value.IndexOf("_")),
		Value = resp.Value
	});


The point is, just the fact that they are available open doors for your language.
Was This Post Helpful? 2
  • +
  • -

#30 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1905
  • View blog
  • Posts: 3,948
  • Joined: 11-December 07

Re: Java 8 Lambda Syntax has been (mostly) Finalized

Posted 15 September 2011 - 07:20 AM

If you are struggling to see how lambdas could be useful in Java try experimenting with Ruby a little. You can quickly pick up enough to play with it. When you come back to Java, some of the hoops you have to jump through to so simple things like loop over a collection will drive you nuts!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2