Skip to main content
2 of 4
for prudes with limited vocabulary

Faster way to split an array into stride by 2?

i've written a basic function to split an array into stride by 2

[z] -> [x],[y]

    func splitArray(Z x:[Float64])->(Real:[Float64],Imag:[Float64])
    {
      guard (x.count % 2 == 0) else {"BOMB:: need even number of elements in array \(#file) \(#line) \(#function)"
      return ([0.0],[0.0])
      }
      var Real:[Float64] = zeroD(npts: Int(x.count/2) )
      var Imag:[Float64] = zeroD(npts: Int(x.count/2) )
      var ida:Int = 0, idb:Int = 0
  for elem in stride(from:0, to: x.count-1, by:2)
  {
      Real[ida] = x[elem]
      ida += 1
  }
  for elem in stride(from:1, to:x.count, by:2)
  {
    Imag[idb] = x[elem]
    idb += 1
  }
      return (Real,Imag)
}

anyone have a faster map? sometimes map is not that fast, it just hides the loop.