Skip to content

Commit

Permalink
Update 1-3.md
Browse files Browse the repository at this point in the history
  • Loading branch information
falOn-Dev committed Sep 25, 2024
1 parent 8a8b0b1 commit 3abe2e5
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions book/src/kotlin/unit-1/1-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 3abe2e5

Please sign in to comment.