From 6c0bbc4632b54d11ff23c9c9b91ad573a448bd11 Mon Sep 17 00:00:00 2001 From: a Date: Sat, 26 Mar 2022 17:02:08 -0500 Subject: [PATCH] mapv --- parallel/parallel.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/parallel/parallel.go b/parallel/parallel.go index 0aaea52..bec772f 100644 --- a/parallel/parallel.go +++ b/parallel/parallel.go @@ -25,6 +25,22 @@ func Map[T any](xs []T, fx func(T) T) []T { wg.Wait() return lambda.Flatten(spl) } + +func MapV[T, V any](xs []T, fx func(T) T) []V { + spl := lambda.Split(xs, routineCount) + wg := new(sync.WaitGroup) + wg.Add(len(spl)) + tmp := make([][]V, len(spl)) + for i, v := range spl { + go func(ix int, vx []T) { + spl[ix] = lambda.Map(vx, fx) + wg.Done() + }(i, v) + } + wg.Wait() + return lambda.Flatten(tmp) +} + func Foldl[T any](xs []T, fx func(T, T) T) T { spl := lambda.Split(xs, routineCount) wg := new(sync.WaitGroup)