• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

"Java 8 in action" question: Are lambdas similar to Scala anonymous functions?

Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Authors,
Greetings of the day!

I just started looking at Java 8 - and as far as I understand - the lambda expression is functional in spirit - similar to Scala's anonymous functions?
Is this analogy roughly correct?

If so - As Scala is anyway a JVM language - why to provide such overlapping functionality?

Thanks again for your time!

--- regards atul
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Atul,

Lambda expressions and method references are similar to Scala's anonymous functions in the sense that they let you pass a piece of code around in a concise way.

Java *had* to provide a similar feature so it doesn't stay behind compared to other programming languages. Lambda expressions are great for Java because:

- they encourage a pattern called "behaviour parameterisation" which enables more flexible APIs
- they are very concise compared to alternative way of doing the same thing (i.e. using anonymous classes)

Note that in Scala's, functions are in general richer (we detail this in chapter 12 of our book):
- Scala provides function types, a syntactic sugar to represent the idea of function descriptors
- Scala provides anonymous functions that do not have the write restrictions on non-local variables that lambda expressions have (i.e anonymous functions capture variables and not values as with lambdas).
- Scala provides support for currying: breaking down a function that takes multiple arguments into a series of functions that take part of the arguments.

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic