From a1fd7794e783c5f315af8654cf9f744af0663c0a Mon Sep 17 00:00:00 2001 From: a Date: Sat, 26 Mar 2022 14:16:31 -0500 Subject: [PATCH] wip --- filter.go | 12 ++++++++++++ fold.go | 9 +++++++++ go.mod | 3 +++ map.go | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 filter.go create mode 100644 fold.go create mode 100644 go.mod create mode 100644 map.go diff --git a/filter.go b/filter.go new file mode 100644 index 0000000..1be680f --- /dev/null +++ b/filter.go @@ -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] +} diff --git a/fold.go b/fold.go new file mode 100644 index 0000000..3981d5c --- /dev/null +++ b/fold.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2dba43e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.tuxpa.in/a/lambda + +go 1.18 diff --git a/map.go b/map.go new file mode 100644 index 0000000..562fae4 --- /dev/null +++ b/map.go @@ -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 +}