Arithmosophi - Arithmosoϕ
Arithmosophi是一个缺失的协议集合,它简化了在泛型对象或函数上的算术和统计分析。正如Equatable定义了==操作符,Addable将定义+操作符。
protocol Addable {
func + (lhs: Self, rhs: Self) -> Self
}
[1, 2, 3, 4].sum // 1 + 2 + 3 + 4
[0, 1, 2, 3, 4].average // 2
[13, 2.4, 3, 4].varianceSample- 正如你可能猜到的,
Substractable定义了-操作符,Multiplicatable定义了*操作符等,所有这些都在Arithmosophi.swift中定义。
内容
泛型函数
看看sumOf函数
func sumOf<T where T:Addable, T:Initializable>(input : [T]) -> T {
return reduce(input, T()) {$0 + $1}
}Int、Double甚至String的数组都可以作为参数传递给此函数。任何Addable对象。
无需为Double、Float、Int等实现特定的函数。
sumOf和productOf函数在Arithmosophi.swift中可用。
CollectionType
本框架对CollectionType进行了一些有用的扩展。
[1, 2, 3, 4].sum // 1 + 2 + 3 + 4
[1, 2, 3, 4].product // 1 * 2 * 3 * 4
["a","b","c","d"].sum // "abcd" same as joinWithSeparator("")
[["a","b"],["c"],["d"]].sum // ["a","b","c","d"] same as flatMap{$0}平均
[1, 2, 3, 4].average // (1 + 2 + 3 + 4) / 4如果一个类型是可除以Int的,并且可以定义一个操作符来执行此操作,则称其为Averagable。
func /(lhs: Self, rhs: Int) -> Self所有算术类型都遵循此协议,并且可以为CollectionType获取平均值。
备注:您可以遵循此协议和Addable,以创建自定义平均值。
中位数
从数组中获取中位数。
- 如果
CollectionType中有偶数个元素,将返回两个中间值的平均值。
[1, 11, 19, 4, -7].median // 4- 如果
CollectionType中有偶数个元素,将返回两个中间值中的较小值。
[1.0, 11, 19.5, 4, 12, -7].medianLow // 4- 如果
CollectionType中有偶数个元素,将返回两个中间值中的较大值。
[1, 11, 19, 4, 12, -7].medianHigh // 11方差
计算方差。
- 样本方差:Σ( (元素 - 平均值)^2 ) / (计数 - 1)
[1.0, 11, 19.5, 4, 12, -7].varianceSample- 总体方差:Σ( (元素 - 平均值)^2 ) / 计数
[1.0, 11, 19.5, 4, 12, -7].variancePopulation标准差
计算标准差。
[1.0, 11, 19.5, 4, 12, -7].standardDeviationSample[[1.0, 11, 19.5, 4, 12, -7].standardDeviationPopulation偏度
计算偏度。
[1.0, 11, 19.5, 4, 12, -7].skewness // or .moment.skewness峰度
计算峰度。
[1.0, 11, 19.5, 4, 12, -7].kurtosis // or .moment.kurtosis协方差
计算与另一个CollectionType的协方差。
[1, 2, 3.5, 3.7, 8, 12].covarianceSample([0.5, 1, 2.1, 3.4, 3.4, 4])- 总体协方差
[1, 2, 3.5, 3.7, 8, 12].covariancePopulation([0.5, 1, 2.1, 3.4, 3.4, 4])[1, 2, 3.5, 3.7, 8, 12].pearson([0.5, 1, 2.1, 3.4, 3.4, 4])复数
与Complex.swift配套使用,Complex是一个包含两个ArithmeticType(实部和虚部)的结构体。
var complex = Complex(real: 12, imaginary: 9)
complex = 12 + 9.i您可以对它执行如下操作:(+, -, *, /, ++, --, -)。
result = complex + 8 // Complex(real: 20, imaginary: 9)
Complex(real: 12, imaginary: 9) + Complex(real: 8, imaginary: 1)
// Complex(real: 20, imaginary: 10)
对象属性
在使用操作符时,简单算术协议的强大功能得以释放。
如果我们实现一个包含泛型T值的盒子对象
class Box<T> {
var value: T
}我们可以在它上面定义一些以泛型方式实现的操作符,就像我们可以在Equatable或Comparable上做到的那样
func +=<T where T:Addable> (inout box: Box<T>, addend: T) {
box.value = box.value + addend
}
func -=<T where T:Substractable> (inout box: Box<T>, addend: T) {
box.value = box.value - addend
}如何使用这个操作符
var myInt: Box<Int>(5)
myInt += 37有关完整示例,请参阅Prephirence文件,位于Prephirences框架中,或查看示例《a href="Samples/Box.swift">Box.swift》
可设置技巧
对于可选属性,您可以使用 Initializable 或任何定义获取值方式的协议
class Box<T> {
var value: T?
}
func +=<T where T:Addable, T:Initializable> (inout box: Box<T>, addend: T) {
box.value = (box.value ?? T()) + addend
}逻辑运算
LogicalOperationsType 是一个针对 Bool 的缺失协议,灵感来自 BitwiseOperationsType(或 IntegerArithmeticType)
目的是一样的,实现函数而不需要知道基本类型
例如,您可以实现自己的 Boolean 枚举 并实现该协议
enum Boolean: LogicalOperationsType {case True, False}
func && (left: Boolean, @autoclosure right: () -> Boolean) -> Boolean {
switch left {
case .False: return .False
case .True: return right()
}
}
...然后只为 Box 上的 Bool、Boolean 和任何 LogicalOperationsType 实现一个运算符
func &&=<T:LogicalOperationsType> (inout box: Box<T>, @autoclosure right: () -> TT) {
box.value = box.value && right()
}查看更复杂的枚举 Optional,它也实现了 LogicalOperationsType
几何学
使用 Arithmos(数字) 和 Statheros(常数)
Arithmos 和 Statheros 分别为 Double、Float 和 ATEGORY 添加函数和数学常数,允许实现泛型函数而无需考虑类型
func distance<T: Arithmos>(#x: T, y: T) -> T {
return x.hypot(y)
}
func radiansFromDegrees<T where T: Multiplicable, Dividable, T: Arithmos, T: Statheros>(degrees: T) -> T {
return degrees * T.PI / T(180.0)
}查看 Geometry.swift 了解更多信息
配置
使用 cocoapods
pod 'Arithmosophi'如果您不感兴趣整个框架,可以使用以下命令安装子集:
pod 'Arithmosophi/Core' # Arithmosophi.swift
pod 'Arithmosophi/Logical' # LogicalOperationsType.swift
pod 'Arithmosophi/Complex' # Complex.swift
pod 'Arithmosophi/MesosOros' # MesosOros.swift
pod 'Arithmosophi/Arithmos' # Arithmos.swift
pod 'Arithmosophi/Sigma' # Sigma.swift
pod 'Arithmosophi/Statheros' # Statheros.swift
pod 'Arithmosophi/Samples' # Samples/*.swift (not installed by default)请将 use_frameworks! 添加到 Podfile 的末尾。
创建您自己的框架依赖
在.podspec文件中
s.dependency 'Arithmosophi'或仅定义所需的target
s.dependency 'Arithmosophi/Core'
s.dependency 'Arithmosophi/Logical'使用xcode
将文件拖到您的项目中