aboutsummaryrefslogtreecommitdiff
path: root/pkg/u/list.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/u/list.go')
-rw-r--r--pkg/u/list.go22
1 files changed, 21 insertions, 1 deletions
diff --git a/pkg/u/list.go b/pkg/u/list.go
index 39d7b11..1cffbd5 100644
--- a/pkg/u/list.go
+++ b/pkg/u/list.go
@@ -1,5 +1,25 @@
package u
+func Filter[T any](v []T, f func(T) bool) []T {
+ var result []T
+
+ for _, s := range v {
+ if f(s) {
+ result = append(result, s)
+ }
+ }
+
+ return result
+}
+
+func Map[T any, V any](a []T, f func(T) V) []V {
+ result := make([]V, len(a))
+ for i, v := range a {
+ result[i] = f(v)
+ }
+ return result
+}
+
func First[T any](v []T) (T, bool) {
if len(v) == 0 {
var zero T
@@ -25,7 +45,7 @@ func LastOrZero[T any](v []T) T {
}
func ChunkBy[T any](items []T, chunkSize int) [][]T {
- var chunks = make([][]T, 0, (len(items)/chunkSize)+1)
+ chunks := make([][]T, 0, (len(items)/chunkSize)+1)
for chunkSize < len(items) {
items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize])
}