Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Lambda Expression in Scala

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.

Basic Lambda Syntax:

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

Examples:

  • A simple lambda with one parameter:
val addOne = (x: Int) => x + 1
println(addOne(5))  // Outputs: 6
  • A lambda with multiple parameters:
val sum = (x: Int, y: Int) => x + y
println(sum(5, 3))  // Outputs: 8
  • A lambda with no parameters:
val greet = () => println("Hello, World!")
greet()  // Outputs: Hello, World!
  • Using lambdas as arguments to higher-order functions: Higher-order functions are functions that take other functions as parameters and/or return functions. They are pervasive in Scala's collections library.
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)

Placeholder Syntax:

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

Type Inference:

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)

Conclusion:

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.

  1. Using lambda functions in Scala:

    • Description: Lambda functions, also known as anonymous functions, are concise function literals in Scala.
    • Code Example:
      val add: (Int, Int) => Int = (x, y) => x + y
      val result = add(3, 4)  // Result: 7
      
  2. Scala higher-order functions with lambdas:

    • Description: Scala supports higher-order functions that take functions as parameters or return functions.
    • Code Example:
      val numbers = List(1, 2, 3, 4, 5)
      
      // Higher-order function (map) with lambda
      val squaredNumbers = numbers.map(x => x * x)
      
  3. Scala lambda parameters and types:

    • Description: Lambda parameters and return types can be explicitly or implicitly defined.
    • Code Example:
      val multiply: (Int, Int) => Int = (x, y) => x * y
      
  4. Functional programming with Scala Lambdas:

    • Description: Lambdas play a crucial role in functional programming, enabling concise and expressive code.
    • Code Example:
      val numbers = List(1, 2, 3, 4, 5)
      
      // Functional programming with lambdas
      val sumOfSquares = numbers.map(x => x * x).reduce((x, y) => x + y)
      
  5. Currying and partial application in Scala:

    • Description: Currying involves transforming a function with multiple parameters into a sequence of functions, each taking a single parameter. Partial application fixes some parameters of a function, creating a new function with fewer parameters.
    • Code Example:
      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, _)
      
  6. 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)