0
0
Kotlinprogramming~15 mins

Lambda syntax and declaration in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Lambda syntax and declaration
📖 Scenario: You are working on a simple Kotlin program that processes a list of numbers. You want to use a lambda function to double each number in the list.
🎯 Goal: Build a Kotlin program that uses a lambda expression to double each number in a list and then prints the doubled numbers.
📋 What You'll Learn
Create a list of integers with exact values
Declare a lambda expression that doubles a number
Use the lambda to transform the list
Print the resulting list
💡 Why This Matters
🌍 Real World
Lambdas are used in Kotlin to write short, reusable pieces of code for tasks like transforming lists, handling events, or processing data streams.
💼 Career
Understanding lambda syntax is essential for Kotlin developers working on Android apps or backend services, as it helps write clean and efficient code.
Progress0 / 4 steps
1
Create the initial list of numbers
Create a list of integers called numbers with these exact values: 1, 2, 3, 4, 5
Kotlin
Need a hint?

Use val numbers = listOf(1, 2, 3, 4, 5) to create the list.

2
Declare a lambda expression to double a number
Declare a lambda expression called double that takes an integer n and returns n * 2
Kotlin
Need a hint?

Use val double: (Int) -> Int = { n -> n * 2 } to declare the lambda.

3
Use the lambda to transform the list
Create a new list called doubledNumbers by applying the double lambda to each element in numbers using map
Kotlin
Need a hint?

Use val doubledNumbers = numbers.map(double) to apply the lambda.

4
Print the doubled numbers
Print the doubledNumbers list using println
Kotlin
Need a hint?

Use println(doubledNumbers) to display the doubled list.