• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
atul khot
Ranch Hand
Posts: 67
  • 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
 
Raoul Gabriel
Author
Greenhorn
Posts: 20
6
  • 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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!