Skip to content

Commit 5175c86

Browse files
committed
core/silces: map and nil filters
1 parent fd52e8f commit 5175c86

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

intra/core/slices.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func FilterLeft[T any](arr []T, test TestFn[T]) (out []T) {
4141
return out
4242
}
4343

44+
func Map[T, U any](arr []T, transform func(T) U) (out []U) {
45+
out = make([]U, 0)
46+
for _, x := range arr {
47+
out = append(out, transform(x))
48+
}
49+
return out
50+
}
51+
4452
// WithoutElem returns arr (may be a copy) removing all occurrences of elem.
4553
func WithoutElem[T comparable](arr []T, elem T) (out []T) {
4654
if !slices.Contains(arr, elem) {
@@ -67,3 +75,13 @@ func WithElem[T comparable](s []T, add T) []T {
6775
}
6876
return append(s, add)
6977
}
78+
79+
func WithoutNils[T any](arr []T) (out []T) {
80+
for _, x := range arr {
81+
if IsNil(x) {
82+
continue
83+
}
84+
out = append(out, x)
85+
}
86+
return out
87+
}

0 commit comments

Comments
 (0)