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
복사