← Back to Kotlin Course | Chapter 9: Lambdas & Higher-Order Functions | Lesson 3 of 6

map, filter, reduce का उपयोग

map, filter, और reduce एक पूरी list को एक साथ transform करने के लिए तीन power tools हैं: हर item बदलना, सिर्फ कुछ रखना, या सब कुछ एक result में निचोड़ना।
Syntax
kotlin
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

markup
// 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)
}

Selecting with filter

.filter { } सिर्फ उन elements वाली एक नई list return करता है जिनके लिए दिया गया predicate true return करता है, बाकी को discard करते हुए।

उदाहरण: Selecting with filter

markup
// 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)
}

Combining into One Value with reduce

.reduce { acc, item -> ... } बार-बार elements को एक single accumulated result में combine करता है, पहले element को initial accumulator value के रूप में शुरू करते हुए।

उदाहरण: Combining into One Value with reduce

markup
// 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")
}

Combining with fold and an Initial Value

.fold(initial) { acc, item -> ... } reduce जैसा व्यवहार करता है लेकिन एक explicit initial value से शुरू होता है, जो इसे किसी खाली collection पर उपयोग करना भी सुरक्षित बनाता है।

उदाहरण: Combining with fold and an Initial Value

markup
// 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)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. .reduce { } को .fold(initial) { } के साथ confuse करना; reduce पहले element को starting accumulator के रूप में उपयोग करता है और किसी खाली collection पर throw करता है, जबकि fold एक explicit initial value लेता है।
  2. .filter { }.map { } को गलत order में chain करना और इरादे से अलग (भले ही कभी-कभी बराबर रूप से valid) results पाना।
  3. यह भूल जाना कि ये 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.