Swift โฉ operator โฉ a^^n
ๅ๏ผSwift ็พๅจๆฏไธๆฏๆ **
้็ฎโ
// custom precedence
precedencegroup PowerPrecedence {
higherThan : MultiplicationPrecedence
associativity: right
}
// custom operator
infix operator ^^ : PowerPrecedence
extension Int {
/// `a^^n`: a to the n-th power
public static func ^^(a:Int, n:Int) -> Int {
precondition(n >= 0, "โ ็ฎๅ็กๆณ่จ็ฎ่ฒ ็ๆฌกๆน")
guard n > 0 else { return 1 }
return (1...n).reduce(1) { (product, i) in
product * a
}
}
}