Add helper functions
This commit is contained in:
parent
cde63c03d6
commit
e202da86b3
11
Sources/App/Extensions/Array+Extensions.swift
Normal file
11
Sources/App/Extensions/Array+Extensions.swift
Normal 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])
|
||||
}
|
||||
}
|
20
Sources/App/Extensions/Power.swift
Normal file
20
Sources/App/Extensions/Power.swift
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user