새벽의 기록

[ios\swift] 고차함수 (map, filter, reduce) 본문

[iOS]

[ios\swift] 고차함수 (map, filter, reduce)

OneTen 2024. 6. 11. 10:00
다른 함수를 전달인자로 받거나 함수실행의 결과를 함수로 반환하는 함수

map

 

https://developer.apple.com/documentation/swift/array/map(_:)-87c4d

 

map(_:) | Apple Developer Documentation

Returns an array containing the results of mapping the given closure over the sequence’s elements.

developer.apple.com


 

# case1 

 

let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
// 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"]
let letterCounts = cast.map { $0.count }
// 'letterCounts' == [6, 6, 3, 4]

 

 

# case2

 

let string = ["1", "2", "3", "4", "5"]
let numbers = string.map { Int($0)! }

print(numbers)
// [1, 2, 3, 4, 5]

 

 

# case3

 

// numbers의 각 요소에 9 곱하기

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let multiplyArray: [Int] = numbers.map { $0 * 9 }

print(multiplyArray)
// [9, 18, 27, 36, 45, 54, 63, 72, 81]

 


 

filter

 

https://developer.apple.com/documentation/swift/string/filter(_:)

 

filter(_:) | Apple Developer Documentation

Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate.

developer.apple.com


 

# case1

 

let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let shortNames = cast.filter { $0.count < 5 }
print(shortNames)
// Prints "["Kim", "Karl"]"

 

 

# case2

 

// numbers에서 짝수만 추출하기

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
let evenNumbers = numbers.filter { $0 % 2 == 0 }

print(evenNumbers)
// [2, 4, 6, 8]

 

 

# case3

 

// 2글자인 스포츠만 추출하기

let sports = ["클라이밍", "백패킹", "낚시", "러닝", "수영", "눈썰매"]
let twoLetterSports: [String] = sports.filter { $0.count == 2 }

print(twoLetterSports)
// ["낚시", "러닝", "수영"]

 


 

reduce

 

https://developer.apple.com/documentation/swift/array/reduce(_:_:)

 

reduce(_:_:) | Apple Developer Documentation

Returns the result of combining the elements of the sequence using the given closure.

developer.apple.com


 

# case1

 

let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
    x + y
})
// numberSum == 10

 

 

# case 2-1

 

// 각 요소의 합 구하기

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let sum = numbers.reduce(0, +)

print(sum)
// 55

 

 

# case 2-2

 

// 각 요소의 합 구하기

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let sum = numbers.reduce(0) { $0 + $1 }

print(sum)
// 55

 

 

# case 3-1

 

// 각 요소의 곱셈 결과 구하기

let numbers = [1, 2, 3, 4, 5]
let sum = numbers.reduce(1, *)

print(sum)
// 120

 

 

# case 3-2

 

// 각 요소의 곱셈 결과 구하기

let numbers = [1, 2, 3, 4, 5]
let sum = numbers.reduce(1) { $0 * $1 }

print(sum)
// 120

 


참고

https://jeonyeohun.tistory.com/265

https://yagom.github.io/swift_basic/contents/22_higher_order_function/

https://iosdevlime.tistory.com/entry/iOSSwift-유용한-대표-고차함수-map-filter-reduce-다루기

https://velog.io/@un1945/Swift-고차함수-Higher-order-Function

 

 

'[iOS]' 카테고리의 다른 글

[ios\swift] SwiftUI TextField 글자 수 제한  (0) 2024.08.06
[ios\swift] allSatisfy(_:)  (0) 2024.06.10
[ios\swift] 내장함수 zip(_:_:)  (0) 2024.05.28
Comments