Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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)
        })
    }


FWIW this is often called `zipWith`, or sometimes just `map` (some `map` implementations can take a variable number of sequences to map over).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: