This commit is contained in:
a 2022-03-26 17:22:54 -05:00
parent 6c0bbc4632
commit b85c13a047
9 changed files with 83 additions and 38 deletions

View File

@ -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)
}

2
go.mod
View File

@ -1,3 +1,5 @@
module git.tuxpa.in/a/lambda
go 1.18
require golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 // indirect

2
go.sum Normal file
View File

@ -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=

17
map_test.go Normal file
View File

@ -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)
}
}

23
math.go Normal file
View File

@ -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
}

View File

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

36
parallel/map.go Normal file
View File

@ -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)
}

View File

@ -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)

View File

@ -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 {