Add helper functions

This commit is contained in:
Christoph Hagen 2021-12-01 22:48:10 +01:00
parent cde63c03d6
commit e202da86b3
2 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,11 @@
import Foundation
extension Array {
func rotated(toStartAt index: Int) -> [Element] {
guard index != 0 else {
return self
}
return Array(self[index..<count] + self[0..<index])
}
}

View File

@ -0,0 +1,20 @@
import Foundation
precedencegroup PowerPrecedence { higherThan: MultiplicationPrecedence }
infix operator ^^ : PowerPrecedence
func ^^ (radix: Int, power: Int) -> Int {
switch power {
case Int.min..<0:
return 0
case 0:
return 1
case 1:
return radix
default:
var result = radix
for _ in 1..<power {
result *= radix
}
return result
}
}