Why Lambdas Were Introduced?
Eliminating "Boilerplate" Code
Treating Functions as "First-Class Citizens"
Powering the Standard Library
Performance Optimizations
A lambda expression, or lambda, is a type of object that holds a block of code. You can assign a lambda to a variable, just as you can any other sort of object, or pass a lambda to a function which can then execute the code it holds. This means that you can use lambdas to pass specific behavior to a more generalized function.
What we’re going to do?
1. Define a lambda.
2. Create a higher-order function.
The lambda starts and ends with curly braces {}. All lambdas are defined within curly braces, so they can’t be omitted.
When you assign a lambda to a variable, you’re assigning a block of code to it, not the result of the code being run. To run the code in a lambda, you need to explicitly invoke it.
Lambda expressions have a type
Just like any other sort of object, a lambda has a type. The difference with a lambda’s type, however, is that it doesn’t specify a class name that the lambda implements. Instead, it specifies the type of the lambda’s parameters and return value.
You can pass a lambda to a function
As well as assigning a lambda to a variable, you can also use one or more as function parameters. Doing so allows you to pass specific behavior to a more generalized function.
A function that uses a lambda as a parameter or return value is known as a higher-order function.
A type alias lets you provide an alternative name for an existing type, which you can then use in your code.
Telegram Group: https://t.me/androidNinjaWarrior
Guide to watch:
00:00 What is the Requirement of Lambda in Kotlin?
01:49 Why use Lambda in Kotlin?
02:13 How to create a lambda in Kotlin?
06:40 Lambda VS function in Kotlin
10:08 How to explicitly define lambda type in Kotlin?
13:41 How to create lambda with no parameter in Kotlin?
19:12 How to create a lambda with no return type in Kotlin?
21:55 How to use lambda as a parameter in function in Kotlin?
30:17 How to use lambda as a return type in function in Kotlin?
34:54 How type alias works in Kotlin?
#lambda #lambdas #typealias #lambdaInKotlin #kotlin #kotlinLambda #higherOrderFunction #lambdaVSFunction
Disclaimer- Some content are used for educational purposes under fair use.
Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for "fair use" for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use. All credit for copyright material used in the video goes to the respected Owner.
Thank you guys.