map, filter, reduce का उपयोग
In this page:
val mapped = list.map { it transformation }
val filtered = list.filter { condition }
val total = list.reduce { acc, item -> acc + item }
Transforming with map
.map { } किसी collection के हर element पर एक transformation लागू करता है और results के साथ एक बिल्कुल नई list return करता है, original collection को बिना छुए छोड़ते हुए।
उदाहरण: Transforming with map
// 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 `squares`, set to `numbers.map { it * it }`
val squares = numbers.map { it * it }
// Print `squares` to the console, with a trailing newline
println(squares)
}
Login to try C/C++/Java/PHP code in the editor
Selecting with filter
.filter { } सिर्फ उन elements वाली एक नई list return करता है जिनके लिए दिया गया predicate true return करता है, बाकी को discard करते हुए।
उदाहरण: Selecting with filter
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5, 6)`
val numbers = listOf(1, 2, 3, 4, 5, 6)
// Declare a read-only variable `evens`, set to `numbers.filter { it % 2 == 0 }`
val evens = numbers.filter { it % 2 == 0 }
// Print `evens` to the console, with a trailing newline
println(evens)
}
Login to try C/C++/Java/PHP code in the editor
Combining into One Value with reduce
.reduce { acc, item -> ... } बार-बार elements को एक single accumulated result में combine करता है, पहले element को initial accumulator value के रूप में शुरू करते हुए।
उदाहरण: Combining into One Value with reduce
// 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 `sum`, set to `numbers.reduce { acc, n -> acc + n }`
val sum = numbers.reduce { acc, n -> acc + n }
// Print "Sum via reduce: $sum" to the console, with a trailing newline
println("Sum via reduce: $sum")
}
Login to try C/C++/Java/PHP code in the editor
Combining with fold and an Initial Value
.fold(initial) { acc, item -> ... } reduce जैसा व्यवहार करता है लेकिन एक explicit initial value से शुरू होता है, जो इसे किसी खाली collection पर उपयोग करना भी सुरक्षित बनाता है।
उदाहरण: Combining with fold and an Initial Value
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf<Int>()`
val numbers = listOf<Int>()
// Declare a read-only variable `sum`, set to `numbers.fold(0) { acc, n -> acc + n }`
val sum = numbers.fold(0) { acc, n -> acc + n }
// Print "Sum via fold on empty list: $sum" to the console, with a trailing newline
println("Sum via fold on empty list: $sum")
// Declare a read-only variable `words`, set to `listOf("Kotlin", "is", "fun")`
val words = listOf("Kotlin", "is", "fun")
// Declare a read-only variable `sentence`, set to `words.fold("Result:") { acc, word -> "$acc $word" }`
val sentence = words.fold("Result:") { acc, word -> "$acc $word" }
// Print `sentence` to the console, with a trailing newline
println(sentence)
}
Login to try C/C++/Java/PHP code in the editor
.reduce { }को.fold(initial) { }के साथ confuse करना;reduceपहले element को starting accumulator के रूप में उपयोग करता है और किसी खाली collection पर throw करता है, जबकिfoldएक explicit initial value लेता है।.filter { }.map { }को गलत order में chain करना और इरादे से अलग (भले ही कभी-कभी बराबर रूप से valid) results पाना।- यह भूल जाना कि ये functions एक नई collection या value return करते हैं और original collection को कभी mutate नहीं करते।
.map { transform }दी गई function के अनुसार transform किए हर element वाली एक नई list return करता है।.filter { predicate }सिर्फ दी गई condition satisfy करने वाले elements वाली एक नई list return करता है।.reduce { acc, item -> ... }सभी elements को एक single result में combine करता है, पहले element को initial accumulator के रूप में उपयोग करते हुए।.fold(initial) { acc, item -> ... }reduceजैसा है लेकिन एक explicit starting value लेता है, इसलिए यह खाली collections पर भी काम करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: