Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
Lambda expressions, often just called "lambdas", are a way to define small, anonymous functions. In Scala, lambdas are a fundamental part of the language, given its deep integration with functional programming concepts. Lambdas are a concise way to define a function without giving it a name.
The syntax of lambda expressions in Scala typically involves parameters, followed by the =>
symbol, followed by an expression. Here's the basic syntax:
(argument1, argument2, ... ) => expression
val addOne = (x: Int) => x + 1 println(addOne(5)) // Outputs: 6
val sum = (x: Int, y: Int) => x + y println(sum(5, 3)) // Outputs: 8
val greet = () => println("Hello, World!") greet() // Outputs: Hello, World!
val numbers = List(1, 2, 3, 4, 5) val squared = numbers.map(x => x * x) // Using a lambda to square each number println(squared) // Outputs: List(1, 4, 9, 16, 25)
Scala provides a more concise way to write lambdas using underscores as placeholders for parameters.
// These two are equivalent val sum1 = (x: Int, y: Int) => x + y val sum2 = (_: Int) + (_: Int) println(sum1(5, 3)) // Outputs: 8 println(sum2(5, 3)) // Outputs: 8
Scala's type inference can often determine the types of lambda parameters for you, leading to even more concise code. For example, in the context of a List[Int]
, Scala knows that the lambda parameter must be an Int
.
val numbers = List(1, 2, 3, 4, 5) val doubled = numbers.map(_ * 2) println(doubled) // Outputs: List(2, 4, 6, 8, 10)
Lambda expressions in Scala provide a concise way to create anonymous functions on-the-fly. They are especially handy when working with higher-order functions and allow for expressive and readable code, especially when manipulating collections or dealing with functional constructs.
Using lambda functions in Scala:
val add: (Int, Int) => Int = (x, y) => x + y val result = add(3, 4) // Result: 7
Scala higher-order functions with lambdas:
val numbers = List(1, 2, 3, 4, 5) // Higher-order function (map) with lambda val squaredNumbers = numbers.map(x => x * x)
Scala lambda parameters and types:
val multiply: (Int, Int) => Int = (x, y) => x * y
Functional programming with Scala Lambdas:
val numbers = List(1, 2, 3, 4, 5) // Functional programming with lambdas val sumOfSquares = numbers.map(x => x * x).reduce((x, y) => x + y)
Currying and partial application in Scala:
val add: (Int, Int) => Int = (x, y) => x + y // Currying val curriedAdd: Int => (Int => Int) = x => y => x + y // Partial application val addFive: Int => Int = add(5, _)
Practical examples of Lambda Expressions in Scala:
Example 1 - Filtering with Lambda:
val numbers = List(1, 2, 3, 4, 5) // Filtering with lambda val evenNumbers = numbers.filter(x => x % 2 == 0)
Example 2 - Sorting with Lambda:
val words = List("apple", "banana", "grape", "orange") // Sorting with lambda val sortedWords = words.sortWith((a, b) => a.length < b.length)
Example 3 - Using Lambdas in Higher-Order Functions:
val numbers = List(1, 2, 3, 4, 5) // Using lambda in higher-order function (map) val squaredNumbers = numbers.map(x => x * x)