diff --git a/fold.go b/fold.go index 035796b..717033f 100644 --- a/fold.go +++ b/fold.go @@ -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]) diff --git a/parallel.go b/parallel.go new file mode 100644 index 0000000..39af049 --- /dev/null +++ b/parallel.go @@ -0,0 +1,5 @@ +package lambda + + + + diff --git a/parallel/parallel.go b/parallel/parallel.go new file mode 100644 index 0000000..0aaea52 --- /dev/null +++ b/parallel/parallel.go @@ -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) +} diff --git a/transform.go b/transform.go new file mode 100644 index 0000000..9a5ad10 --- /dev/null +++ b/transform.go @@ -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 +} diff --git a/transform_test.go b/transform_test.go new file mode 100644 index 0000000..2969898 --- /dev/null +++ b/transform_test.go @@ -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) + } +}