This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Lambda Expression "Arrow Token" ->  RSS feed

 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall copy this discussion in our Java8 forum.

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 + 2

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.
 
Dustin Wright
Ranch Hand
Posts: 30
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A real oversimplification to lambdas would be...


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?
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oddly enough, I read it as “goes to”, too.

It is called “Church's Lambda Calculus” after Church who invented it in the 1920s, but it is completely different from differential and integral calculus which Newton invented.
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!