This commit is contained in:
a 2022-03-26 17:01:10 -05:00
parent 4476140fd8
commit 7a2fb30f61
5 changed files with 186 additions and 0 deletions

View File

@ -4,6 +4,9 @@ func Foldl[T any](xs []T, fx func(T, T) T) T {
if len(xs) < 1 {
return *new(T)
}
if len(xs) == 1 {
return xs[0]
}
for i := 1; i < len(xs); i++ {
xs[0] = fx(xs[0], xs[i])
}
@ -14,6 +17,9 @@ func Foldr[T any](xs []T, fx func(T, T) T) T {
if len(xs) < 1 {
return *new(T)
}
if len(xs) == 1 {
return xs[0]
}
li := len(xs) - 1
for i := li - 1; i >= 0; i-- {
xs[li] = fx(xs[li], xs[i])

5
parallel.go Normal file
View File

@ -0,0 +1,5 @@
package lambda

56
parallel/parallel.go Normal file
View File

@ -0,0 +1,56 @@
package parallel
import (
"sync"
"git.tuxpa.in/a/lambda"
)
var routineCount int = 1
func SetRoutineCount(n int) {
routineCount = n
}
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 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)
}

49
transform.go Normal file
View File

@ -0,0 +1,49 @@
package lambda
func Entries[K comparable, V any](m map[K]V) ([]K, []V) {
keys := make([]K, 0, len(m))
vals := make([]V, 0, len(m))
for k, v := range m {
keys = append(keys, k)
vals = append(vals, v)
}
return keys, vals
}
func Flatten[T any](xss [][]T) []T {
return Foldl(xss, Merge[T])
}
func Merge[T any](a, b []T) []T {
return append(a, b...)
}
//split xs into n groups
//extra elements will be put in the last array to keep order
//if len(xs) < n, result will be [][]T{xs}
func Split[T any](xs []T, n int) [][]T {
if n == 0 {
return nil
}
groupSize := len(xs) / n
if groupSize == 0 {
return [][]T{xs}
}
idx := 0
out := make([][]T, n)
for i := 0; i < len(xs); i = i + groupSize {
from := i
to := i + groupSize
if idx == (n - 1) {
to = len(xs)
}
if to >= len(xs) {
to = len(xs)
out[idx] = append(out[idx], xs[from:to]...)
return out
}
out[idx] = append(out[idx], xs[from:to]...)
idx = idx + 1
}
return out
}

70
transform_test.go Normal file
View File

@ -0,0 +1,70 @@
package lambda_test
import (
"reflect"
"testing"
"git.tuxpa.in/a/lambda"
)
func TestFlattenIntSix(t *testing.T) {
var arr [][]int
arr = [][]int{{1}, {2}, {3}, {4}, {6}, {5}}
ans := lambda.Flatten(arr)
exp := []int{1, 2, 3, 4, 6, 5}
if !reflect.DeepEqual(ans, exp) {
t.Fatalf("expect %v to be %v", ans, exp)
}
}
func TestSplitIntSix0(t *testing.T) {
arr := []int{1, 2, 3, 4, 6, 5}
ans := lambda.Split(arr, 0)
if ans != nil {
t.Fatalf("expect %v to be %v", ans, nil)
}
}
func TestSplitIntSix1(t *testing.T) {
arr := []int{1, 2, 3, 4, 6, 5}
ans := lambda.Split(arr, 1)
exp := [][]int{{1, 2, 3, 4, 6, 5}}
if !reflect.DeepEqual(ans, exp) {
t.Fatalf("expect %v to be %v", ans, exp)
}
}
func TestSplitIntSix2(t *testing.T) {
arr := []int{1, 2, 3, 4, 6, 5}
ans := lambda.Split(arr, 2)
exp := [][]int{{1, 2, 3}, {4, 6, 5}}
if !reflect.DeepEqual(ans, exp) {
t.Fatalf("expect %v to be %v", ans, exp)
}
}
func TestSplitIntSix6(t *testing.T) {
arr := []int{1, 2, 3, 4, 6, 5}
ans := lambda.Split(arr, 6)
exp := [][]int{{1}, {2}, {3}, {4}, {6}, {5}}
if !reflect.DeepEqual(ans, exp) {
t.Fatalf("expect %v to be %v", ans, exp)
}
}
func TestSplitIntSix5(t *testing.T) {
arr := []int{1, 2, 3, 4, 6, 5}
ans := lambda.Split(arr, 5)
exp := [][]int{{1}, {2}, {3}, {4}, {6, 5}}
if !reflect.DeepEqual(ans, exp) {
t.Fatalf("expect %v to be %v", ans, exp)
}
}
func TestSplitIntSix10(t *testing.T) {
arr := []int{1, 2, 3, 4, 6, 5}
ans := lambda.Split(arr, 10)
exp := [][]int{{1, 2, 3, 4, 6, 5}}
if !reflect.DeepEqual(ans, exp) {
t.Fatalf("expect %v to be %v", ans, exp)
}
}