复制 Array.reduce() 方法
Replicating Array.reduce() method
我试图在我的自定义 class 中复制 Array.reduce()
方法,并意识到它使用 Result 作为类型。只是无法理解是将结果类型创建为枚举还是其他类型。
import Foundation
public class MyArray {
private var arr: [Int] = []
internal static var instance: MyArray?
private init() {}
public static func getInstance() -> MyArray {
if self.instance == nil {
self.instance = MyArray()
}
return self.instance!
}
public func insert(value val: Int) {
arr.append(val)
}
/*************** Custom reduce like function ***************/
public func perform(_ initialResult: Int, _ nextPartialResult: (Int, Int) -> Int) -> Int {
var result = initialResult
for element in arr {
result = nextPartialResult(result, element) // calling the closure
}
return result
}
}
现在从外部访问 MyArray class
var arr1 = MyArray.getInstance()
arr1.insert(value: 1)
arr1.insert(value: 2)
arr1.insert(value: 4)
arr1.insert(value: 3)
arr1.insert(value: 2)
arr1.insert(value: 5)
arr1.insert(value: 2)
arr1.insert(value: 2)
// :Complex calculations left for user to implement
var result = arr1.perform(0) {
return [=12=] + ( * )
}
print("Complex calculation in elements of MEMBER array of arr1: \(result)")
// :Just another way of writing the above closure
result = arr1.perform(0) { (result, num1) -> Int in
return result + ( num1 * num1)
}
print("Complex calculation in elements of MEMBER array of hello arr1: \(result)")
// :Simple calculations
print("Factorial of elements in MEMBER array of arr1: \(arr1.perform(1, *))")
print("Sum of elements in MEMBER array of arr1: \(arr1.perform(0, +))")
问题是我必须一次用一种特定类型(Int 或 String 或 Double 等)定义我的 perform() 函数。我正在尝试创建我的函数来处理任何类型,就像 reduce() 函数一样。
我无法理解如何在我的 class 中定义结果类型,然后在我的函数中使用它!!
我了解结果类型不是 swift 中标准库的一部分。
标准 reduce
函数使用泛型。请参阅 Swift 书中的 Generics 章节。
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
它有两个通用类型:Result
和Element
。 Element
来自集合中值的类型,Result
来自减少值的结果类型。
因此您的第一步是在您自己的 perform
函数中使用相同的签名。
但是在这样做的过程中你会发现你现在需要让你的 MyArray
class 也基于一个泛型而不是被硬编码为只与 Int
一起工作。
在尝试这样做的过程中,您会发现无法将 MyArray
定义为泛型,同时支持单例模式。所以你需要删除 instance
和 getIntance()
.
最终结果变为:
public class MyArray<Element> {
private var arr: [Element] = []
public init() {}
public func insert(value val: Element) {
arr.append(val)
}
/*************** Custom reduce like function ***************/
public func perform<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result {
var result = initialResult
for element in arr {
result = nextPartialResult(result, element)
}
return result
}
}
有了这个,你的第一个例子就变成了:
var arr1 = MyArray<Int>()
arr1.insert(value: 1)
arr1.insert(value: 2)
arr1.insert(value: 4)
arr1.insert(value: 3)
arr1.insert(value: 2)
arr1.insert(value: 5)
arr1.insert(value: 2)
arr1.insert(value: 2)
// :Complex calculations left for user to implement
var result = arr1.perform(0) {
return [=12=] + ( * )
}
print(result)
这会输出 67
的预期结果。
最后,它起作用了,但如果您注意到的话,这个 MyArray
class 没有任何意义(除了学习练习)。只需使用 Array
.
我试图在我的自定义 class 中复制 Array.reduce()
方法,并意识到它使用 Result 作为类型。只是无法理解是将结果类型创建为枚举还是其他类型。
import Foundation
public class MyArray {
private var arr: [Int] = []
internal static var instance: MyArray?
private init() {}
public static func getInstance() -> MyArray {
if self.instance == nil {
self.instance = MyArray()
}
return self.instance!
}
public func insert(value val: Int) {
arr.append(val)
}
/*************** Custom reduce like function ***************/
public func perform(_ initialResult: Int, _ nextPartialResult: (Int, Int) -> Int) -> Int {
var result = initialResult
for element in arr {
result = nextPartialResult(result, element) // calling the closure
}
return result
}
}
现在从外部访问 MyArray class
var arr1 = MyArray.getInstance()
arr1.insert(value: 1)
arr1.insert(value: 2)
arr1.insert(value: 4)
arr1.insert(value: 3)
arr1.insert(value: 2)
arr1.insert(value: 5)
arr1.insert(value: 2)
arr1.insert(value: 2)
// :Complex calculations left for user to implement
var result = arr1.perform(0) {
return [=12=] + ( * )
}
print("Complex calculation in elements of MEMBER array of arr1: \(result)")
// :Just another way of writing the above closure
result = arr1.perform(0) { (result, num1) -> Int in
return result + ( num1 * num1)
}
print("Complex calculation in elements of MEMBER array of hello arr1: \(result)")
// :Simple calculations
print("Factorial of elements in MEMBER array of arr1: \(arr1.perform(1, *))")
print("Sum of elements in MEMBER array of arr1: \(arr1.perform(0, +))")
问题是我必须一次用一种特定类型(Int 或 String 或 Double 等)定义我的 perform() 函数。我正在尝试创建我的函数来处理任何类型,就像 reduce() 函数一样。
我无法理解如何在我的 class 中定义结果类型,然后在我的函数中使用它!!
我了解结果类型不是 swift 中标准库的一部分。
标准 reduce
函数使用泛型。请参阅 Swift 书中的 Generics 章节。
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
它有两个通用类型:Result
和Element
。 Element
来自集合中值的类型,Result
来自减少值的结果类型。
因此您的第一步是在您自己的 perform
函数中使用相同的签名。
但是在这样做的过程中你会发现你现在需要让你的 MyArray
class 也基于一个泛型而不是被硬编码为只与 Int
一起工作。
在尝试这样做的过程中,您会发现无法将 MyArray
定义为泛型,同时支持单例模式。所以你需要删除 instance
和 getIntance()
.
最终结果变为:
public class MyArray<Element> {
private var arr: [Element] = []
public init() {}
public func insert(value val: Element) {
arr.append(val)
}
/*************** Custom reduce like function ***************/
public func perform<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) -> Result) -> Result {
var result = initialResult
for element in arr {
result = nextPartialResult(result, element)
}
return result
}
}
有了这个,你的第一个例子就变成了:
var arr1 = MyArray<Int>()
arr1.insert(value: 1)
arr1.insert(value: 2)
arr1.insert(value: 4)
arr1.insert(value: 3)
arr1.insert(value: 2)
arr1.insert(value: 5)
arr1.insert(value: 2)
arr1.insert(value: 2)
// :Complex calculations left for user to implement
var result = arr1.perform(0) {
return [=12=] + ( * )
}
print(result)
这会输出 67
的预期结果。
最后,它起作用了,但如果您注意到的话,这个 MyArray
class 没有任何意义(除了学习练习)。只需使用 Array
.