const
a = [1, 2, 3]
b = [-1, 2, 5]
c = [1, -2, 4]
s = [a, b, c]
block original:
var x, y, z: seq[int]
for arr in s:
x.add(arr[0])
y.add(arr[1])
z.add(arr[2])
echo [min(x), min(y), min(z)] #(-1, -2, 3)
import std/options
func minByIdx[N: static int; T](seqOfArrays: openArray[array[N, T]]):
Option[array[N, T]] =
if seqOfArrays.len == 0: none(array[N, T])
elif seqOfArrays.len == 1: some(seqOfArrays[0])
else:
var res = seqOfArrays[0]
for i in 1..high(seqOfArrays):
for j in 0..<N:
res[j] = min(res[j], seqOfArrays[i][j])
some(res)
echo minByIdx(s).get()
Run
If you have factually supported reasons to worry about performance (the code
performs slowly with the real data), I'd look if the compiler really unrolls
the inner loop, it probably should. You can write a macro to unroll it or maybe
use SIMD if data fits.