# Lambda Expression "Arrow Token" ->

posted 1 year ago

Forgive me if this has been covered and I can't find it. I'm reading the Lambda Expressions lesson on Oracle's tutorials and don't see an explanation of the "Arrow Token" ->. It appears to be introduced here:

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax

Most of the way down the page at the section "Syntax of Lambda Expressions". I maybe looking right at it and not seeing it. I do see it called out: "The arrow token, ->", but not defined that I can tell. Please help me understand this.

Thanks!

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax

Most of the way down the page at the section "Syntax of Lambda Expressions". I maybe looking right at it and not seeing it. I do see it called out: "The arrow token, ->", but not defined that I can tell. Please help me understand this.

Thanks!

Campbell Ritchie

Sheriff

Posts: 50614

82

posted 1 year ago

I shall copy this discussion in our Java8 forum.

The arrow is part of the syntax of a λ. You know you write λ

The part before the • goes to the left of the -> in () but you can omit the () if there is only one argument. Then the arrow token replaces the •. Then the remainder goes afterwards. So my λ

By the way: how do people pronounce -> when reading out code? I have never seen it anywhere.

Edit: additional link to maths tutorial as PDF.

The arrow is part of the syntax of a λ. You know you write λ

*x*•*x*+ 2 and that means that (λ*x*•*x*+ 2)*y*will evaluate to*y*+ 2? Well, you cannot write • because there isn't a • key on most keyboards, not even with AltGr. So they came up with a new operator using two keystrokes which are usually found on keyboards − and >.The part before the • goes to the left of the -> in () but you can omit the () if there is only one argument. Then the arrow token replaces the •. Then the remainder goes afterwards. So my λ

*x*•*x*+ 2 comes out as x -> x + 2By the way: how do people pronounce -> when reading out code? I have never seen it anywhere.

Edit: additional link to maths tutorial as PDF.

posted 1 year ago

Thanks for the reply and moving it to the Java 8 forum where it belongs (I'm guessing). I took a look at your PDF, it appears this is related to calculus. I have no knowledge of calculus, so I'm guessing I may not be able to understand the meaning of "->". If possible, please dumb this down as far as you can just in case. I literally have no knowledge of any mathematics beyond arithmetic.

Thanks.

Thanks.

Mike. J. Thompson

Bartender

Posts: 689

17

posted 1 year ago

No, Lambda expressions are not related to calculus, they are related to mathematical functions. In java they are just syntactic sugar to make it easier to create an object that meets a particular type of interface, and wherever you see a lambda expression you could replace it with an anonymous class declaration. Lambda expressions are much more readable though, so make functional programming styles much more accessible in java.

To answer your question Campbell, I normally read the arrow operator as 'goes to', or 'evaluates to'.

So for

I would read 'a goes to 2a'

Dustin Wright wrote: I took a look at your PDF, it appears this is related to calculus. I have no knowledge of calculus, so I'm guessing I may not be able to understand the meaning of "->". If possible, please dumb this down as far as you can just in case. I literally have no knowledge of any mathematics beyond arithmetic.

No, Lambda expressions are not related to calculus, they are related to mathematical functions. In java they are just syntactic sugar to make it easier to create an object that meets a particular type of interface, and wherever you see a lambda expression you could replace it with an anonymous class declaration. Lambda expressions are much more readable though, so make functional programming styles much more accessible in java.

To answer your question Campbell, I normally read the arrow operator as 'goes to', or 'evaluates to'.

So for

I would read 'a goes to 2a'

posted 1 year ago

A real oversimplification to lambdas would be...

Assuming I have

"Give me the

Does that make sense?

Assuming I have

`p`, then create a "method" where`System.out.println(p)`is done. Method is in quotes because it isn't really a method, but at the beginning you can think of it that way."Give me the

`n`and I'll square it."Does that make sense?

All things are lawful, but not all things are profitable.

Campbell Ritchie

Sheriff

Posts: 50614

82

Piet Souris

Rancher

Posts: 1398

29

posted 1 year ago

Hey!

That was Leibniz!

But I guess you also say the English invented football...

Anyway: in the first class of high school I learned to write functions like this:

f: x -> 3x + 1

later to be replaced by f(x) = 3x + 1.

So the lambda notation reminds me of the goold old days when I was young...

Greetz,

Piet

Campbell Ritchie wrote:(...)but it is completely different from differential and integral calculus which Newton invented.

Hey!

That was Leibniz!

But I guess you also say the English invented football...

Anyway: in the first class of high school I learned to write functions like this:

f: x -> 3x + 1

later to be replaced by f(x) = 3x + 1.

So the lambda notation reminds me of the goold old days when I was young...

Greetz,

Piet

Campbell Ritchie

Sheriff

Posts: 50614

82

posted 1 year ago

If you look up Leibniz on Wikipedia, you find two interesting things:-1: His signature appears to read Leibniz not, as one would expect from old German spellings, Leibnitz. 2: It suggests Leibniz and Newton developed calculus independently of each other. We also invented Rugby by means of cheating at football.