diff --git a/fold.go b/fold.go index 717033f..d481be9 100644 --- a/fold.go +++ b/fold.go @@ -1,6 +1,6 @@ package lambda -func Foldl[T any](xs []T, fx func(T, T) T) T { +func Foldr[T any](xs []T, fx func(T, T) T) T { if len(xs) < 1 { return *new(T) } @@ -13,7 +13,7 @@ func Foldl[T any](xs []T, fx func(T, T) T) T { return xs[0] } -func Foldr[T any](xs []T, fx func(T, T) T) T { +func Foldl[T any](xs []T, fx func(T, T) T) T { if len(xs) < 1 { return *new(T) } diff --git a/go.mod b/go.mod index 2dba43e..300c325 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module git.tuxpa.in/a/lambda go 1.18 + +require golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fe45005 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 h1:Xt4/LzbTwfocTk9ZLEu4onjeFucl88iW+v4j4PWbQuE= +golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= diff --git a/map_test.go b/map_test.go new file mode 100644 index 0000000..37ecb53 --- /dev/null +++ b/map_test.go @@ -0,0 +1,17 @@ +package lambda_test + +import ( + "reflect" + "testing" + + "git.tuxpa.in/a/lambda" +) + +func TestMapAdd(t *testing.T) { + arr := []int{1, 2, 3, 4, 6, 5} + ans := lambda.Map(arr, func(x int) int { return x + 4 }) + exp := []int{5, 6, 7, 8, 10, 9} + if !reflect.DeepEqual(ans, exp) { + t.Fatalf("expect %v to be %v", ans, exp) + } +} diff --git a/math.go b/math.go new file mode 100644 index 0000000..3eda2cb --- /dev/null +++ b/math.go @@ -0,0 +1,23 @@ +package lambda + +import "golang.org/x/exp/constraints" + +type Numeric interface { + constraints.Complex | constraints.Integer | constraints.Float +} + +func Add[T Numeric](a, b T) T { + return a + b +} + +func Sub[T Numeric](a, b T) T { + return a - b +} + +func Mul[T Numeric](a, b T) T { + return a * b +} + +func Div[T Numeric](a, b T) T { + return a / b +} diff --git a/parallel.go b/parallel.go deleted file mode 100644 index 39af049..0000000 --- a/parallel.go +++ /dev/null @@ -1,5 +0,0 @@ -package lambda - - - - diff --git a/parallel/map.go b/parallel/map.go new file mode 100644 index 0000000..f174048 --- /dev/null +++ b/parallel/map.go @@ -0,0 +1,36 @@ +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) 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) +} diff --git a/parallel/parallel.go b/parallel/parallel.go index bec772f..e7f5385 100644 --- a/parallel/parallel.go +++ b/parallel/parallel.go @@ -11,36 +11,6 @@ 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 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) diff --git a/transform.go b/transform.go index 9a5ad10..527c926 100644 --- a/transform.go +++ b/transform.go @@ -11,7 +11,7 @@ func Entries[K comparable, V any](m map[K]V) ([]K, []V) { } func Flatten[T any](xss [][]T) []T { - return Foldl(xss, Merge[T]) + return Foldr(xss, Merge[T]) } func Merge[T any](a, b []T) []T {