This commit is contained in:
a 2022-03-26 14:16:31 -05:00
commit a1fd7794e7
4 changed files with 57 additions and 0 deletions

12
filter.go Normal file
View File

@ -0,0 +1,12 @@
package lambda
func Filter[T any](xs []T, fx func(T) bool) []T {
n := 0
for _, x := range xs {
if fx(x) {
xs[n] = x
n++
}
}
return xs[:n]
}

9
fold.go Normal file
View File

@ -0,0 +1,9 @@
package lambda
func Foldl[T any](xs []T, fx func(T, T) T) T {
return *new(T)
}
func Foldr[T any](xs []T, fx func(T, T) T) T {
return *new(T)
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.tuxpa.in/a/lambda
go 1.18

33
map.go Normal file
View File

@ -0,0 +1,33 @@
package lambda
func Map[T any](xs []T, fx func(T) T) []T {
for i, v := range xs {
xs[i] = fx(v)
}
return xs
}
func MapV[T, V any](xs []T, fx func(T) V) (ov []V) {
ov = make([]V, len(xs))
for i, v := range xs {
ov[i] = fx(v)
}
return
}
func MapError[T any](xs []T, fx func(T) (T, error)) (ov []T, oe []error) {
oe = make([]error, len(xs))
for i, v := range xs {
xs[i], oe[i] = fx(v)
}
return
}
func MapErrorV[T, V any](xs []T, fx func(T) (V, error)) (ov []V, oe []error) {
ov = make([]V, len(xs))
oe = make([]error, len(xs))
for i, v := range xs {
ov[i], oe[i] = fx(v)
}
return
}