← Back to Swift Course | Chapter 6: Collections | Lesson 6 of 7

map, filter और reduce

map किसी list के हर item को बदलता है, filter सिर्फ वे पिक करता है जो आप चाहते हैं, और reduce इन सभी को एक single result में combine करता है।
Syntax
swift
let mapped = array.map { $0 transformation }
let filtered = array.filter { condition using $0 }
let total = array.reduce(initialValue) { partial, element in partial + element }

Transforming with map

.map हर element पर एक closure apply करता है और results को एक नए array में collect करता है, original बदले बिना।

उदाहरण: Transforming with map

markup
// Declare a constant `numbers`, set to `[1, 2, 3, 4]`
let numbers = [1, 2, 3, 4]
// Declare a constant `squared`, set to `numbers.map { $0 * $0 }`
let squared = numbers.map { $0 * $0 }
// Print `squared` to the console
print(squared)

Filtering with filter

.filter सिर्फ उन elements को रखता है जिनके लिए दिया closure true return करता है, बाकी discard करते हुए।

उदाहरण: Filtering with filter

markup
// Declare a constant `numbers`, set to `[1, 2, 3, 4, 5, 6]`
let numbers = [1, 2, 3, 4, 5, 6]
// Declare a constant `evens`, set to `numbers.filter { $0 % 2 == 0 }`
let evens = numbers.filter { $0 % 2 == 0 }
// Print `evens` to the console
print(evens)

Combining with reduce

.reduce सभी elements को एक single value में fold करता है, एक initial value से शुरू करते हुए और इसे हर element के साथ बारी-बारी combine करते हुए।

उदाहरण: Combining with reduce

markup
// Declare a constant `prices`, set to `[9.99, 19.99, 5.00]`
let prices = [9.99, 19.99, 5.00]
// Declare a constant `total`, set to `prices.reduce(0) { partial, price in partial + price }`
let total = prices.reduce(0) { partial, price in partial + price }
// Print "Total: \(total)" to the console
print("Total: \(total)")

Chaining map, filter, and reduce

इन operations को एक single readable pipeline में एक multi-step transformation express करने के लिए एक साथ chain किया जा सकता है।

उदाहरण: Chaining map, filter, and reduce

markup
// Declare a constant `numbers`, set to `[1, 2, 3, 4, 5, 6, 7, 8]`
let numbers = [1, 2, 3, 4, 5, 6, 7, 8]
// Declare a constant `sumOfSquaredEvens`, set to `numbers.filter { $0 % 2 == 0 }.map { $0 * $0 }.reduce(0, +)`
let sumOfSquaredEvens = numbers.filter { $0 % 2 == 0 }.map { $0 * $0 }.reduce(0, +)
// Print `sumOfSquaredEvens` to the console
print(sumOfSquaredEvens)
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. हर element transform करने के लिए एक manual for loop उपयोग करना जब .map() उसी intent को ज़्यादा clearly और concisely express करता है।
  2. यह भूल जाना कि .filter() के closure को हर element रखना है या नहीं यह decide करते हुए एक Bool return करना चाहिए।
  3. .reduce() के पहले argument को final result के बजाय starting accumulator value के रूप में galat समझना।
चैप्टर सारांश
  • .map { } हर element transform करता है और results का एक नया array return करता है।
  • .filter { } सिर्फ उन elements को रखता है जिनके लिए closure true return करता है।
  • .reduce(initial) { } सभी elements को एक single accumulated value में combine करता है।
  • इन तीनों को powerful, readable data transformations के लिए एक साथ chain किया जा सकता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.