๋ฐ์ํ
๐ ๋ชฉ๋ก
- ๊ณ ์ฐจ ํจ์๋?
- map
- filter
- reduce
๐ ๊ณ ์ฐจ ํจ์๋?
๋ค๋ฅธ ํจ์๋ฅผ ์ ๋ฌ ์ธ์๋ก ๋ฐ๊ฑฐ๋ ํจ์ ์ฑํ์ ๊ฒฐ๊ณผ๋ฅผ ํจ์๋ก ๋ฐํํ๋ ํจ์
swift์์ ์ ๊ณตํ๋ ๊ธฐ๋ณธ์ ์ธ ๊ณ ์ฐจํจ์: map, filter, reduce
๐ map
let numbers: [Int] = [0, 1, 2, 3]
// ํ์ด์ ์ด ํจ์
//let mapArray = numbers.map { (number) -> Int in
// return number * 2
//}
// ๊ฐ์ํ
let mapArray = numbers.map { $0 * 2 }
mapArray // [0, 2, 4, 6]
๐ filter
let intArray = [10, 4, 5, 20, 13]
let filterArray = intArray.filter { $0 > 5 }
filterArray // [10, 20 ,13]
๐ reduce
let intArray2 = [1, 2, 3, 4, 5]
// ์ด๊ธฐ๊ฐ ์ค์ 0
let sum = intArray2.reduce(0) {
(result: Int, element: Int) -> Int in
return result + element
}
sum // 15
๋ฐ์ํ
'Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Swift - Navigator Push Present (0) | 2021.12.07 |
---|---|
Swift - Content Hugging, compression Resistance (0) | 2021.12.06 |
Swift closure (0) | 2021.11.28 |
Swift try-catch (0) | 2021.11.28 |
Swift ์ต์ ๋ ์ฒด์ด๋ (0) | 2021.11.28 |