ParallelMap function for arrays/slices

How to write a concurrent functional Map function in golang using generics and goroutines

ParallelMap applies a function to each element in the input slice in parallel and returns a new slice with the results. The function f takes an element of type A and returns an element of type B.

func ParallelMap[A, B any](input []A, f func(A) B) []B {
	result := make([]B, len(input))
	var wg sync.WaitGroup
	wg.Add(len(input))

	for i, v := range input {
		go func(i int, v A) {
			defer wg.Done()
			result[i] = f(v)
		}(i, v)
	}

	wg.Wait()
	return result
}

Technologies: