wip
This commit is contained in:
commit
a1fd7794e7
|
@ -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]
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue