Reputation: 668
Is this possible to do a generic conversion of floating points and return arbitrary number of arrays?
Perhaps I don't understand how to do arbitrary recursion with arrays?
example input vector
Vector:[CGFloat] = [-0.23,-5.2,0.24,-1.4,4.0,10.2]
output vector if stride by 2
VectorOut=[[-0.23,0.24,4.0],[-5.2,-1.4,10.2]]
output vector if stride by 3
VectorOut=[[-0.23,-1.4],[-5.2,4.0],[0.24.10.2]]
func splitArray<T:FloatingPoint>(Vector x:[T], byStride N: Int)->([T],[T],...[byStride])
{
guard (x.count % byStride == 0) else {"BOMB \(#file) \(#line) \(#function)"
return [NaN]
}
var index:[Int] = [Int](repeating: 0.0, count: byStride)
var bigVector:[[T]] = [T](repeating: 0.0, count: byStride)
for idx in index {
for elem in stride(from:idx, to: x.count - 1 - idx, by:byStride){
bigVector[idx]=x[idx]
}
}
return bigVector
}
Upvotes: 0
Views: 438
Reputation: 539815
You cannot return a variable-sized tuple. What you can do is to return a nested array.
Example (hoping that I interpreted the problem correctly):
func splitArray<T>(vector x:[T], byStride N: Int) -> [[T]] {
precondition(x.count % N == 0, "stride must evenly divide the array count")
return (0..<N).map { stride(from: $0, to: x.count, by: N).map { x[$0] } }
}
let a = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
print(splitArray(vector: a, byStride: 2))
// [[1.0, 3.0, 5.0], [2.0, 4.0, 6.0]]
print(splitArray(vector: a, byStride: 3))
// [[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]]
Explanation: (0..<N).map { ... }
creates an array of size N
.
For each value $0
,
stride(from: $0, to: x.count, by: N).map { x[$0] }
creates the "stride" of x
starting at index $0
with increment N
:
[ x[$0], x[$0+N], x[$0+2*N], ..., x[x.count-N+$0] ]
Upvotes: 1