underscore
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

ParallelMap

ParallelMap applies a function to each element concurrently using a worker pool, preserves order, and supports context cancellation.

package main

import (
 "context"
 "fmt"
 u "github.com/rjNemo/underscore"
)

func main() {
 out, err := u.ParallelMap(context.Background(),
    []int{1,2,3,4}, 4, func(ctx context.Context, n int) (int, error) {
  return n*n, nil
 })
 fmt.Println(out, err) // [1 4 9 16] <nil>
}