[Swift๐Ÿฆฉ] #29 ๋งต, ํ•„ํ„ฐ, ๋ฆฌ๋“€์Šค

๋˜์ƒยท2022๋…„ 6์›” 4์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
41/42
post-thumbnail
  • ๊ณ ์ฐจํ•จ์ˆ˜ : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ํ•จ์ˆ˜
  • Map, Filter, Reduce ๋Š” ๋ชจ๋‘ ๊ณ ์ฐจํ•จ์ˆ˜๋‹ค!

1. Map

  • ์ปจํ…Œ์ด๋„ˆ ์•ˆ์˜ ์š”์†Œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์ปจํ…Œ์ด๋„ˆ๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • Sequence, Collection ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” ํƒ€์ž…, Optional ์€ ๋ชจ๋‘ map ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • for - in ๊ตฌ๋ฌธ์œผ๋กœ๋„ map ์œผ๋กœ ์ž‘์„ฑํ•œ ๊ตฌ๋ฌธ์„ ์ถฉ๋ถ„ํžˆ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
container.map(f(x))   ->   return f(container ์˜ ๊ฐ ์š”์†Œ)
  • ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋จ.
  • ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๋™์‹œ์— ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์—ฌ๋Ÿฌ๋ฒˆ ๋ณ€๊ฒฝ๋œ๋‹ค๊ฑฐ๋‚˜ ํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฒŒ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค.
let arr = ["ํƒœ์—ฐ", "์จ๋‹ˆ", "ํ‹ฐํŒŒ๋‹ˆ", "ํšจ์—ฐ", "์œ ๋ฆฌ", "์ˆ˜์˜", "์œค์•„", "์„œํ˜„"]

let heartArr = arr.map { $0 + "โค" }
// ["ํƒœ์—ฐโค", "์จ๋‹ˆโค", "ํ‹ฐํŒŒ๋‹ˆโค", "ํšจ์—ฐโค", "์œ ๋ฆฌโค", "์ˆ˜์˜โค", "์œค์•„โค", "์„œํ˜„โค"]



2. Filter

  • ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๊ฑธ๋Ÿฌ์„œ ์ถ”์ถœ
  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ํƒ€์ž… Bool
let three = arr.filter { $0.length == 3 }  // ["ํ‹ฐํŒŒ๋‹ˆ"]



3. Reduce

  • ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ์š”์†Œ๋“ค์„ ์ „๋‹ฌ๋ฐ›์€ ํด๋กœ์ € ์—ฐ์‚ฐ์œผ๋กœ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋Š” ๊ธฐ๋Šฅ์„ ์‹คํ–‰

Swift ์—์„œ

  1. ํด๋กœ์ €๊ฐ€ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐ›์•„์„œ ์—ฐ์‚ฐ์„ ํ•˜๊ณ , ๊ทธ ๊ฐ’์„ ๋‹ค์Œ ํด๋กœ์ € ์‹คํ–‰์„ ์œ„ํ•ด ๋ฐ˜ํ™˜
    -> ๋ญ”์†Œ๋ฆฌ์•ผ? ์ดˆ๊นƒ๊ฐ’์„ ํ•˜๋‚˜ ๋ฐ›์•„์„œ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜ ๋”ํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋‹ค์‹œ ๋„˜๊น€
public func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result

nextPartialResult: (Result // ์ดˆ๊นƒ๊ฐ’ or ์ด์ „ ๊ฒฐ๊ณผ, Element // ํ˜„์žฌ ๋”ํ•  ์š”์†Œ)

let numbers: [Int] = [1, 2, 3, 4]

var sum: Int = numbers.reduce(0, { (result: Int, next: Int) -> Int in
    return result + next
})

var sum: Int = numbers.reduce(0) { $0 + $1 }
  1. 1๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ˆœํ™˜ํ•˜๋ฉด์„œ ์‹คํ–‰๋˜์ง€๋งŒ ๊ฒฐ๊ณผ๊ฐ’ ๋ฐ˜ํ™˜์€ X
    inout ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ดˆ๊นƒ๊ฐ’์— ์ง์ ‘ ๋”ํ•จ.
    -> ๊ฐ’์— ์ง์ ‘ ๋ณ€ํ™”๋ฅผ ์ผ์œผํ‚ค๊ธฐ ๋•Œ๋ฌธ์— map ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
public func reduce<Result>(into initialResult: Result, _ updateAccumulatingResult: (inout Result, Element) throws -> ()) rethrows -> Result
var sum: Int = numbers.reduce(into: 0) { $0 + $1 } // ํด๋กœ์ €๊ฐ€ ๋ฐ˜ํ™˜ํ•ด์„œ ๊ฐ’์„ ์ „๋‹ฌํ•˜์ง€ ์•Š๊ณ  ํ•ด๋‹น ๊ฐ’์„ ๊ณ„์† ๋ฐ”๊พธ์–ด๊ฐ„๋‹ค.


// map + filter ์ฒ˜๋Ÿผ ์‚ฌ์šฉ
// [] ์— ์ง์ˆ˜๋งŒ ๊ฑธ๋Ÿฌ๋‚ด์„œ 2๋ฐฐํ•ด์„œ append
var doubledNumbers: [Int] = numbers.reduce(into: []) { (result: inout [Int], next: Int) in
    guard next % 2 == 0 else { return }
    
    result.append(next * 2)
}



์ถœ์ฒ˜, ์ฐธ๊ณ 

์•ผ๊ณฐ์˜ ์Šค์œ„ํ”„ํŠธ5 ํ”„๋กœ๊ทธ๋ž˜๋ฐ

profile
0๋…„์ฐจ iOS ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€