Skip to content

Commit c7b651b

Browse files
committed
exslices: add utility to cast and filter
1 parent 4cda3e4 commit c7b651b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

exslices/cast.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ func CastFunc[To, From any](source []From, conv func(From) To) []To {
1414
return result
1515
}
1616

17+
func CastFuncFilter[To, From any](source []From, conv func(From) (To, bool)) []To {
18+
result := make([]To, 0, len(source))
19+
for _, v := range source {
20+
res, ok := conv(v)
21+
if ok {
22+
result = append(result, res)
23+
}
24+
}
25+
return result
26+
}
27+
1728
func CastToString[To, From ~string](source []From) []To {
1829
result := make([]To, len(source))
1930
for i, v := range source {

0 commit comments

Comments
 (0)