37 lines
660 B
Go
37 lines
660 B
Go
package parallel
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.tuxpa.in/a/lambda"
|
|
)
|
|
|
|
func Map[T any](xs []T, fx func(T) T) []T {
|
|
spl := lambda.Split(xs, routineCount)
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(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(spl)
|
|
}
|
|
|
|
func MapV[T, V any](xs []T, fx func(T) V) []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) {
|
|
tmp[ix] = lambda.MapV(vx, fx)
|
|
wg.Done()
|
|
}(i, v)
|
|
}
|
|
wg.Wait()
|
|
return lambda.Flatten(tmp)
|
|
}
|