From adb239300fa1afa4a7d5450b35454a7e0fcd210b Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 6 Dec 2022 18:32:06 -0800 Subject: [PATCH] fn: add new fn (func utils) package for generic helper funcs We start with a simple Map function that can be useful for transforming objects on the fly. We may want to eventually make this Taro pakage into a module so we can just have everything in one place: https://github.com/lightninglabs/taro/tree/main/chanutils. --- fn/stream.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 fn/stream.go diff --git a/fn/stream.go b/fn/stream.go new file mode 100644 index 000000000..9974eb467 --- /dev/null +++ b/fn/stream.go @@ -0,0 +1,13 @@ +package fn + +// Map takes an input slice, and applies the function f to each element, +// yielding a new slice. +func Map[T1, T2 any](s []T1, f func(T1) T2) []T2 { + r := make([]T2, len(s)) + + for i, v := range s { + r[i] = f(v) + } + + return r +}