• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Lambda Expression "Arrow Token" ->

 
Ranch Hand
Posts: 51
1
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 51
1
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic