From 4476140fd891c0546e59fba792260085fcf3c67e Mon Sep 17 00:00:00 2001 From: a Date: Sat, 26 Mar 2022 14:21:17 -0500 Subject: [PATCH] fold --- fold.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fold.go b/fold.go index 3981d5c..035796b 100644 --- a/fold.go +++ b/fold.go @@ -1,9 +1,22 @@ package lambda func Foldl[T any](xs []T, fx func(T, T) T) T { - return *new(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 { - return *new(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] }