โœ–๏ธa^^n

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
        }
    }
}

Last updated