Deep_Dev

๐Ÿ“š ๊ณ ์ฐจํ•จ์ˆ˜

Map

Filter

Reduce

 

 

 

๊ณ ์ฐจํ•จ์ˆ˜๋ž€ ?

๋‹ค๋ฅธ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜์‹คํ–‰์˜ ๊ฒฐ๊ณผ๋ฅผ ํ•จ์ˆ˜๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ 

( = ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ๊ฐ–๋Š” ํ•จ์ˆ˜ )

 

 

Map ( ๋ณ€ํ˜• )

๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ˜•ํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ, ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋Š” ๋ณ€ํ˜•๋˜์ง€ ์•Š๋Š”๋‹ค.

Map์€ ๊ธฐ์กด์˜ for-in ๊ตฌ๋ฌธ๊ณผ ํฐ ์ฐจ์ด๊ฐ€ ์—†์ง€๋งŒ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด์ ์ด ์žˆ๋‹ค.

 

  • ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐ์„ฑ
  • ์žฌ์‚ฌ์šฉ ์šฉ์ด
  • ์ปดํŒŒ์ผ๋Ÿฌ ์ตœ์ ํ™” ์„ฑ๋Šฅ ์ข‹์Œ

 

map์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ

array.map(transform:T throws -> T)

Tํƒ€์ž…์˜ transform์„ ๋ฐ›์•„ ์ƒˆ๋กœ์šด Txใ…์ž…์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค๋Š” ๋ง์ด๋‹ค.

 

 

* ๊ธฐ์กด for - in ๊ตฌ๋ฌธ

let numArray = [1,3,5,7,9]
var multiArray = [Int]()
for num in numArray {
     multiArray.append(num*2)
}
print(multiArray) // [2,6,10,14,18]

 

* map ์‚ฌ์šฉ์‹œ

let numArray = [1,3,5,7,9]
let multiArray = numArray.map { $0 * 2 }
print(multiArray) // [2,6,10,14,18]

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ํ›จ์”ฌ ์งง๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ ์  ๋” ๋งŽ์€ ์—ฐ์‚ฐ๊ณผ ๋‹ค์–‘ํ•œ ์กฐ๊ฑด์ด ๋“ค์–ด๊ฐ€๋ฉด for-in๋ณด๋‹ค map์ด ๊ฐ„๊ฒฐํ•˜๊ณ  ํŽธํ•˜๋‹ค๋Š”๊ฒƒ์„ ๋Š๋‚„ ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ map์˜ ์ฝ”๋“œ๋Š”

let numArray [ 1,3,5,7,9]
let multiArray = numArray.map({ (number:Int) -> Int in
     return number*2
})
print(multiArray) // [2,6,10,14,18]

์œ„ ์ฝ”๋“œ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฐ˜ํ™˜ํƒ€์ž…, ๋ฐ˜ํ™˜ ํ‚ค์›Œ๋“œ(return)์„ ์ƒ๋žต(์ถ•์•ฝ)ํ•œ ํ›„ํ–‰ํด๋กœ์ €์ธ ๊ฒƒ์ด๋‹ค.

 

 

 

Filter ( ์ถ”์ถœ )

filter๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋‚ด๋ถ€์˜ ๊ฐ’์„ ๊ฑธ๋Ÿฌ ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ์— ๊ฐ’์„ ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

map์ฒ˜๋Ÿผ ๊ธฐ์กด ์ฝ˜ํ…์ธ ๋ฅผ ๋ณ€ํ˜•ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ํŠน์ • ์กฐ๊ฑด์— ๋งž๊ฒŒ ๊ฑธ๋Ÿฌ๋‚ด๋Š” ์—ญํ• ์ด๋‹ค.

 

filterํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋˜๋Š” ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ํƒ€์ž…์€ Bool์ด๋‹ค. ํ•ด๋‹น ์ฝ˜ํ…์ธ ์˜ ๊ฐ’์„ ๊ฐ–๊ณ  ์ƒˆ๋กœ์šด ์ปจํ…Œ์ด๋„ˆ์— ํฌํ•จ๋  ํ•ญ๋ชฉ์ด๋ผ๊ณ  ํŒ๋‹จํ•˜๋ฉด true, ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

filter ๋˜ํ•œ map๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ for-in ๊ตฌ๋ฌธ๊ณผ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค.

 

filter์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ

array.filter(isInlcuded:T throws -> T)

Tํƒ€์ž…์˜ isIncluded๋ฅผ ๋ฐ›์•„ ์ƒˆ๋กœ์šด Tํ˜•ํƒœ์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

 

* ๊ธฐ์กด for - in ๊ตฌ๋ฌธ

let stringArray = ["๊ฐ€์ˆ˜", "๋Œ€ํ†ต๋ น", "๊ฐœ๋ฐœ์ž", "์„ ์ƒ๋‹˜", "์˜์‚ฌ", "๊ฒ€์‚ฌ", "๊ฑด๋ฌผ์ฃผ"]
var threeCountArray = [String]()
for st in stringArray {
     if st.count == 3 {
          threeCountArray.append(st)
     }
}
print(threeCountArray) // ["๋Œ€ํ†ต๋ น", "๊ฐœ๋ฐœ์ž", "์„ ์ƒ๋‹˜", "๊ฑด๋ฌผ์ฃผ"]

 

 

* filter ์‚ฌ์šฉ์‹œ 

let stringArray = ["๊ฐ€์ˆ˜", "๋Œ€ํ†ต๋ น", "๊ฐœ๋ฐœ์ž", "์„ ์ƒ๋‹˜", "์˜์‚ฌ", "๊ฒ€์‚ฌ", "๊ฑด๋ฌผ์ฃผ"]
let threeCountArray = stringArray.filter { $0.count == 3 }
print(threeCountArray) // ["๋Œ€ํ†ต๋ น", "๊ฐœ๋ฐœ์ž", "์„ ์ƒ๋‹˜", "๊ฑด๋ฌผ์ฃผ"]

์œ„ ์ฝ”๋“œ๋Š” map ์˜ˆ์ œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฐ˜ํ™˜ ํƒ€์ž…, ๋ฐ˜ํ™˜ ํ‚ค์›Œ๋“œ(return)์„ ์ƒ๋žตํ•œ ํ›„ํ–‰ํด๋กœ์ €์ด๋‹ค.

let stringAttay = ["๊ฐ€์ˆ˜", "๋Œ€ํ†ต๋ น", "๊ฐœ๋ฐœ์ž", "์„ ์ƒ๋‹˜", "์˜์‚ฌ", "๊ฒ€์‚ฌ", "๊ฑด๋ฌผ์ฃผ"]
let threeCountArray = stringAttay.filter({ (value: String) -> Bool in
    return value.count == 3
})
print(threeCountArray) // ["๋Œ€ํ†ต๋ น", "๊ฐœ๋ฐœ์ž", "์„ ์ƒ๋‹˜", "๊ฑด๋ฌผ์ฃผ"]

 

 

 

map๊ณผ filter์˜ ์—ฐ๊ณ„์‚ฌ์šฉ

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

let mappedNumbers: [Int] = numbers.map{ $0 + 3 }

let evenNumbers: [Int] = mappedNumbers.filter{ (number: Int) -> Bool in
     return number % 2 == 0
}
print(evenNumbers) // [4, 6, 8]

// mappedNumbers๋ฅผ ๊ตณ์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ๋ฉ”์„œ๋“œ๋ฅผ ์ฒด์ธ์ฒ˜๋Ÿผ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
let oddNumbers: [Int] = numbers.map{ $0 + 3 }.filter{ $0 % 2 == 1 }
print(oddNumbers) // [3, 5, 7]

 

 

Reduce ( ๊ฒฐํ•ฉ )

reduce๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•ฉ์ณ์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋‚ด๋ถ€์˜ ๊ฐ’๋“ค์„ ๊ฒฐํ•ฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋งŒ๋“ ๋‹ค.

reduce๋ฅผ ํ•œ๋ฒˆ์— ์ดํ•ดํ•˜๊ธฐ์—” ์ˆซ์ž ํ•ฉ ์˜ˆ์ œ๊ฐ€ ์ตœ๊ณ ๋ผ ์ € ๋˜ํ•œ ํ•ฉ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค.

 

* ๊ธฐ์กด for-in ๊ตฌ๋ฌธ

let numberArray = [1,2,3,4,5]
var sum = 0
for number in numberArray {
     sum += number
}
print(sum) // 15

 

 

* reduce ์‚ฌ์šฉ์‹œ

let numberArray = [1,2,3,4,5]
let sum = numberArray.reduce(0) { $0 + $1 }
print(sum) // 15

์ฝ”๋“œ ์„ค๋ช…

1. reduce ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ดˆ๊ธฐ๊ฐ’์€ 0์œผ๋กœ ์ง€์ •. ์ด ์ดˆ๊ธฐ๊ฐ’์€ ๋ˆ„์  ์ž‘์—…์„ ์‹œ์ž‘ํ•  ๋–„ ์‚ฌ์šฉ๋˜๋ฉฐ, ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ์˜ ์ดˆ๊ธฐ๊ฐ’์ด๊ธฐ๋„ ํ•˜๋‹ค.

2. ํด๋กœ์ € { $0 + $1 }๋Š” reduceํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰๋˜๋Š” ํด๋กœ์ €์ด๋‹ค. $0์€ ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๊ณ , $1์€ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

3. reduce ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ํด๋กœ์ €๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์ดˆ๊ธฐ๊ฐ’์ธ 0์„ ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

4. ํด๋กœ์ €๋Š” ๊ฐ ์š”์†Œ๋ฅผ ํ˜„์žฌ ๋ˆ„์ ๋œ ๊ฒฐ๊ณผ์— ๋”ํ•ด์ฃผ๋Š” ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ฒ˜์Œ์—”์€ 0 + 1์„ ๊ณ„์‚ฐํ•˜์—ฌ ๊ฒฐ๊ณผ์— 1์„ ๋ˆ„์ ํ•œ๋‹ค. ๊ทธ๋Ÿผ ๋‹ค์Œ 1+2, 3+3, 6+4, 10+5 ์ˆœ์„œ๋กœ ์ง„ํ–‰ํ•œ๋‹ค.

5. reduce ํ•จ์ˆ˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•œ ํ›„ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์œ„ ์ฝ”๋“œ๋Š” ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฐ˜ํ™˜ ํƒ€์ž…, ๋ฐ˜ํ™˜ ํ‚ค์›Œ๋“œ(return)์„ ์ƒ๋žตํ•œ ํ›„ํ–‰ ํด๋กœ์ €์ด๋‹ค.

let numberArray = [1,2,3,4,5]
let sum = numberArray.reduce(0, { (first: Int, second: Int) -> Int in
    return first + second
})
print(sum) // 15

 

 

 

 

 

 

๐Ÿ”— ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ณ ์ฐจํ•จ์ˆ˜ ํ™œ์šฉ ๋ฌธ์ œ

 

Level 0

https://school.programmers.co.kr/learn/courses/30/lessons/181841

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/181885

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr