43 lines
771 B
Go
43 lines
771 B
Go
package parallel
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"git.tuxpa.in/a/lambda"
|
|
)
|
|
|
|
var routineCount int = 1
|
|
|
|
func SetRoutineCount(n int) {
|
|
routineCount = n
|
|
}
|
|
func Foldl[T any](xs []T, fx func(T, T) T) T {
|
|
spl := lambda.Split(xs, routineCount)
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(spl))
|
|
tmp := make([]T, len(spl))
|
|
for i, v := range spl {
|
|
go func(ix int, vx []T) {
|
|
tmp[ix] = lambda.Foldl(vx, fx)
|
|
wg.Done()
|
|
}(i, v)
|
|
}
|
|
wg.Wait()
|
|
return lambda.Foldl(tmp, fx)
|
|
}
|
|
|
|
func Foldr[T any](xs []T, fx func(T, T) T) T {
|
|
spl := lambda.Split(xs, routineCount)
|
|
wg := new(sync.WaitGroup)
|
|
wg.Add(len(spl))
|
|
tmp := make([]T, len(spl))
|
|
for i, v := range spl {
|
|
go func(ix int, vx []T) {
|
|
tmp[ix] = lambda.Foldr(vx, fx)
|
|
wg.Done()
|
|
}(i, v)
|
|
}
|
|
wg.Wait()
|
|
return lambda.Foldr(tmp, fx)
|
|
}
|