Map, flatMap, compactMap
map
- ๋ฐฐ์ด ๋ด๋ถ์ ๊ฐ์ ํ๋์ฉ mapping
- ๊ฐ ์์์ ๋ํ ๊ฐ์ ๋ณ๊ฒฝํ๊ณ ์ ํ ๋ ์ฌ์ฉํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ค์ ๋ฐฐ์ด์ ์ํ๋ก ๋ฐํ
func map<T>(_ transform: (String) throws -> T) rethrows -> [T]
import Foundation
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
print(lowercaseNames) // ["vivien", "marlon", "kim", "karl"]
let letterCounts = cast.map{ $0.count }
print(letterCounts) // [6, 6, 3, 4]
flatMap
- ๋ฐฐ์ด์ flattenํ๊ฒ ๋ง๋ค์ด์ค
nil์ ์ ๊ฑฐํจ์ต์ ๋ ๋ฐ์ธ๋ฉ
func flatMap<SegementOfResult>(_ transform: ([Int]) throws -> SegementOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult: Sequence
import Foundation
let numbers = [1,2,3,4]
let mapped = numbers.map { Array(repeating: $0, count: $0)}
print(mapped) // [[1], [2,2], [3,3,3], [4,4,4,4]]
// # flatten #
let flatMapped = numbers.flatMap { Array(repeating: $0, count: $0)}
print(flatMapped) // [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
---------------------------------------------------------------------------
// # nil ์ ๊ฑฐ #
let numbers = [1,2,nil,3,4]
let flatMapped = numbers.flatMap { $0 }
print(flatMapped) // [1, 2, 3, 4]
flatMap์ ์ด์ฉํด์ optional binding์ ํ ์ ์์ง๋ง,
compactMap์ ์ฌ์ฉํ๋ ๊ฒ์ ์ถ์ฒํ๋ค๊ณ ํ๋ค.
https://sudonull.com/post/14814-Swift-41-why-Apple-renamed-flatMap-to-compactMap
์ ๋งํฌ์์๋ flatmap ์ฌ์ฉ ์ map๊ณผ ๋์ผํ๊ฒ ์ฌ์ฉ๋๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด์๋ผ๊ณ ํ๋ค.
let possibleNumbers = ["1", "2", "three", "///4///", "5"]
let mapped: [Int?] = possibleNumbers.map { str in Int(str) }
print(mapped) // [Optional(1), Optional(2), nil, nil, Optional(5)]
// # Optional Binding #
let flatMapped: [Int] = possibleNumbers.flatMap { str in Int(str) }
print(flatMapped) // [1, 2, 5]
-------> ํด๋น ์ผ์ด์ค(์ต์
๋ ๋ฐ์ธ๋ฉ)์์๋ compactMap ์ฌ์ฉ ๊ถ์ฅ
compactMap
- nil์ ์ ๊ฑฐํจ
- ์ต์ ๋ ๋ฐ์ธ๋ฉ
func compactMap<ElementOfResult>(_ transform: (Int?) throws -> ElementOfResult?) rethrows -> [ElementOfResult]
import Foundation
let numbers = [1,2,nil,3,4]
let compactMapped = numbers.compactMap { $0 }
print(compactMapped) // [1, 2, 3, 4]
---------------------------------------------------------------------------
// # Optional Binding #
let possibleNumbers = ["1", "2", "three", "///4///", "5"]
let mapped: [Int?] = possibleNumbers.map { str in Int(str) }
print(mapped) // [Optional(1), Optional(2), nil, nil, Optional(5)]
let compactMapped: [Int] = possibleNumbers.compactMap { str in Int(str) }
print(compactMapped) // [1, 2, 5]
---------------------------------------------------------------------------
flatMap vs compactMap
let arr: [[Int?]] = [[1, 2, 3], [nil, 5], [6, nil], [nil, nil]]
let flatMapped = arr.flatMap { $0 }
// [Optional(1), Optional(2), Optional(3), nil, Optional(5), Optional(6), nil, nil, nil]
let flatMappedAndflatMapped = arr.flatMap { $0 }.flatMap { $0 }
// [1, 2, 3, 5, 6]
let flatMappedAndcompactMapped = arr.flatMap { $0 }.compactMap { $0 } // flat ํ optional binding
// [1, 2, 3, 5, 6]
---------------------------------------------------------------------------
let compactMapped = arr.compactMap { $0 }
// [[Optional(1), Optional(2), Optional(3)], [nil, Optional(5)], [Optional(6), nil], [nil, nil]]
let compactMappedAndflatMapped = arr.compactMap { $0 }.flatMap { $0 }
// [Optional(1), Optional(2), Optional(3), nil, Optional(5), Optional(6), nil, nil, nil]
let compactMappedAndcompactMapped = arr.compactMap { $0 }.compactMap { $0 }
// [[Optional(1), Optional(2), Optional(3)], [nil, Optional(5)], [Optional(6), nil], [nil, nil]]
'๐ iOS > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] Guard๋ฌธ๊ณผ If๋ฌธ (0) | 2023.05.06 |
---|---|
[Swift] reduce ์ฌ์ฉํ๊ธฐ | sum (0) | 2022.07.28 |
[Swift] inout? | ํจ์ ๋ด๋ถ์์ ํ๋ผ๋ฏธํฐ์ ๊ฐ์ ๋ณ๊ฒฝํ๊ณ ์ง์ํ๊ธฐ (0) | 2022.06.23 |
[Swift] Protocols | Delegate๋ฅผ ์ด์ง ๊ณ๋ค์ธ (0) | 2022.02.25 |
[Swift] Guard๋ฌธ | guard let (0) | 2021.08.03 |