Search
Duplicate
🎖️

(Swift) map 직접 구현해보기!

간단소개
팔만코딩경 컨트리뷰터
ContributorNotionAccount
주제 / 분류
Swift
Scrap
태그
9 more properties
import UIKit let array: Int? = 2 let increaseResult = array.map({ $0 + 1 }) let doubledResult = array.map({ $0 * 2 }) func increaseArray(array: [Int]) -> [Int] { var result = [Int]() for x in array { result.append(x + 1) } return result } func doubledArray(array: [Int]) -> [Int] { var result = [Int]() for x in array { result.append(x * 2) } return result } func computeIntArray(array: [Int], f: (Int) -> Int) -> [Int] { var result = [Int]() for x in array { result.append(f(x)) } return result } func computeIntArray<U>(array: [Int], f: (Int) -> U) -> [U] { var result = [U]() for x in array { result.append(f(x)) } return result } extension Optional { func customMap<U>(f: (Wrapped) -> U) -> [U]? { var result = [U]() for x in Wrapped { result.append(f(x)) } return result } } print(array.map({ $0 + 2 })) print(array.customMap({ $0 + 2 }))
Swift
복사