From 3abe2e5abb7960daaef55ef7d1ca34d1cc353faf Mon Sep 17 00:00:00 2001 From: Falon <47285315+falOn-Dev@users.noreply.github.com> Date: Wed, 25 Sep 2024 00:03:46 -0400 Subject: [PATCH] Update 1-3.md --- book/src/kotlin/unit-1/1-3.md | 60 +++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/book/src/kotlin/unit-1/1-3.md b/book/src/kotlin/unit-1/1-3.md index 7e5ecaf..fba6d12 100644 --- a/book/src/kotlin/unit-1/1-3.md +++ b/book/src/kotlin/unit-1/1-3.md @@ -128,48 +128,52 @@ fun main() { When you run a Kotlin program, the code inside the `main` function is executed sequentially. You can define other functions and call them from the `main` function to organize your code. -## Lambdas and First-Class Functions +## Lambdas -Before we dive into lambdas, let's first understand what first-class functions are. In Kotlin, functions are considered -first-class citizens, which means they can be treated like any other value. You can assign functions to variables, -pass them as arguments to other functions, and return them from other functions. +Lambda expressions (also known as anonymous functions) are a concise way to define functions that can be passed as +arguments to other functions. In Kotlin, functions are first-class citizens, which means they can be treated like any +other value. This allows you to pass functions as arguments, return functions from other functions, and store functions +in variables. -```kotlin -fun add(a: Int, b: Int) = a + b - -fun multiply(a: Int, b: Int) = a * b - -println(add(multiply(2, 3), 4)) // Output: 10 -``` - -In the example above, we define two functions `add` and `multiply`. We then call `add` with the result of `multiply(2, 3)` -and `4`. This demonstrates how functions can be used as values in Kotlin. +### Lambda Syntax -### Lambdas - -Lambdas are "anonymous functions", this means that they do not have a name. Lambdas are often used to define small -blocks of code that can be passed as arguments to other functions. The syntax for defining a lambda is as follows: +Lambda expressions are defined using curly braces `{}` and the `->` operator. The syntax for a lambda expression is as +follows: ```kotlin -{ parameter1: DataType, parameter2: DataType, ... -> code } +{ parameter1: DataType, parameter2: DataType, ... -> + // Code to be executed +} ``` -- `{}`: curly braces used to define a lambda -- `parameter1`, `parameter2`, ...: parameters passed to the lambda +- `{}`: curly braces used to define a lambda expression +- `parameter1`, `parameter2`, ...: parameters passed to the lambda expression - `DataType`: type of the parameters -- `->`: separates the parameters from the code block -- `code`: block of code to be executed +- `->`: operator used to separate parameters from the code block +- `// Code to be executed`: code block to be executed by the lambda expression +- The last expression in the lambda block is the return value -Lambdas can be assigned to variables and passed as arguments to functions. They are often used with higher-order -functions, which are functions that take other functions as arguments. +These lambda expressions can be assigned to variables and passed as arguments to other functions, which allows you to +write more concise and expressive code. ### Example ```kotlin val add: (Int, Int) -> Int = { a, b -> a + b } +val multiply: (Int, Int) -> Int = { a, b -> a * b } -println(add(5, 3)) // Output: 8 +fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int { + return operation(a, b) +} ``` -As you can see, the "type" of the lambda is `(Int, Int) -> Int`, which means it takes two `Int` parameters and returns -an `Int`. The lambda itself is `{ a, b -> a + b }`, which adds the two parameters `a` and `b`. +In the example above, we define two lambda expressions `add` and `multiply` that take two parameters `a` and `b` of type +`Int` and return their sum and product, respectively. We then define a function `calculate` that takes two parameters +`a` and `b` of type `Int` and an operation that is a lambda expression `(Int, Int) -> Int`. The `calculate` function +calls the operation with the given parameters `a` and `b` and returns the result. + +You can input any lambda expression that matches the `(Int, Int) -> Int` signature into the `calculate` function, which +allows you to perform different operations on the input values. + +A function like `calculate` that takes another function as an argument is known as a higher-order function. Higher-order +functions are a powerful feature of Kotlin that allows you to write more flexible and reusable code. \ No newline at end of file