This commit is contained in:
a 2022-03-26 14:21:17 -05:00
parent a1fd7794e7
commit 4476140fd8
1 changed files with 15 additions and 2 deletions

13
fold.go
View File

@ -1,9 +1,22 @@
package lambda
func Foldl[T any](xs []T, fx func(T, T) T) T {
if len(xs) < 1 {
return *new(T)
}
for i := 1; i < len(xs); i++ {
xs[0] = fx(xs[0], xs[i])
}
return xs[0]
}
func Foldr[T any](xs []T, fx func(T, T) T) T {
if len(xs) < 1 {
return *new(T)
}
li := len(xs) - 1
for i := li - 1; i >= 0; i-- {
xs[li] = fx(xs[li], xs[i])
}
return xs[li]
}