4 ms·
This is pretty ugly and add the overhead of a function callback, but just for fun: func multiLoop[X, Y any](x []X, y []Y, cb func(i int, x X, y Y)) {
by maxmcd 4y ago
This is pretty ugly and add the overhead of a function callback, but just for fun:
func multiLoop[X, Y any](x []X, y []Y, cb func(i int, x X, y Y)) {
if len(x) != len(y) {
panic("invalid slice lengths")
}
for i := 0; i < len(x); i++ {
cb(i, x[i], y[i])
}
}
func foo() {
multiLoop([]int{1, 2, 3}, []string{"a", "b", "c"}, func(i int, x int, y string) {
fmt.Println(i, x, y)
})
}
- masklinn 4y agoFWIW this is often called `zipWith`, or sometimes just `map` (some `map` implementations can take a variable number of sequences to map over).