Lambda Syntax क्या है
In this page:
val lambda = { param: Type -> expression }
val noParams = { expression }
val multi = { a: Type, b: Type -> expression }
lambda(argument)
Writing a Basic Lambda
एक lambda curly braces के अंदर लिखा जाता है, parameters (अगर कोई हों) को एक arrow -> से पहले list करते हुए, उसके बाद body। body में आखिरी expression lambda का result बन जाता है।
उदाहरण: Writing a Basic Lambda
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `square`, set to `{ x: Int -> x * x }`
val square = { x: Int -> x * x }
// Print "Square of 5: ${square(5)}" to the console, with a trailing newline
println("Square of 5: ${square(5)}")
}
Login to try C/C++/Java/PHP code in the editor
Lambdas with No Parameters
जब कोई lambda कोई parameters नहीं लेता, arrow और parameter list को बस छोड़ दिया जाता है, braces के अंदर सिर्फ body छोड़ते हुए।
उदाहरण: Lambdas with No Parameters
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `sayHello`, set to `{ println("Hello from a lambda!") }`
val sayHello = { println("Hello from a lambda!") }
sayHello()
}
Login to try C/C++/Java/PHP code in the editor
Lambdas with Multiple Parameters
एक lambda कई parameters ले सकता है, arrow से पहले commas से अलग करते हुए, बिल्कुल किसी regular function की parameter list की तरह।
उदाहरण: Lambdas with Multiple Parameters
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `add`, set to `{ a: Int, b: Int -> a + b }`
val add = { a: Int, b: Int -> a + b }
// Print "Sum: ${add(3, 4)}" to the console, with a trailing newline
println("Sum: ${add(3, 4)}")
}
Login to try C/C++/Java/PHP code in the editor
Passing a Lambda to a Function
Lambdas अक्सर पहले एक variable में store होने के बजाय उस function के call site पर सीधे लिखे जाते हैं जो एक की उम्मीद करता है, जैसे collection operations के साथ।
उदाहरण: Passing a Lambda to a Function
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
val numbers = listOf(1, 2, 3, 4)
// Declare a read-only variable `doubled`, set to `numbers.map { n -> n * 2 }`
val doubled = numbers.map { n -> n * 2 }
// Print `doubled` to the console, with a trailing newline
println(doubled)
}
Login to try C/C++/Java/PHP code in the editor
- जब parameters explicitly नामित हों तब किसी lambda के parameters को उसकी body से अलग करने वाला arrow
->भूल जाना। - किसी lambda की parameter list के चारों ओर अनावश्यक parentheses जोड़ना; lambda parameters किसी regular function की तरह
()में wrap नहीं होते। - किसी variable को assign किए lambda के अंदर
returnलिखने की कोशिश करना और उम्मीद करना कि यह सिर्फ lambda से बाहर निकलेगा, जबकि अगर lambda inlined है तो यह असल में एक non-local return करता है।
- एक lambda expression
{ parameters -> body }के रूप में लिखा जाता है, और इसे किसी variable को assign किया जा सकता है या सीधे किसी function को पास किया जा सकता है। - अगर किसी lambda में कोई parameters नहीं हैं,
->और उससे पहले की हर चीज़ पूरी तरह छोड़ दी जाती है। - किसी lambda की body में आखिरी expression अपने आप इसकी return value है।
- Lambdas को एक function type के साथ variables में store किया जा सकता है, जैसे
val add: (Int, Int) -> Int।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: