SliceFilter takes an input collection and returns elements where `predicate` returns true Known as 'filter' in most other languages or 'Select' in C# Linq.
(slice []T, predicate func(item T) bool)
| 56 | // SliceFilter takes an input collection and returns elements where `predicate` returns true |
| 57 | // Known as 'filter' in most other languages or 'Select' in C# Linq. |
| 58 | func SliceFilter[T any](slice []T, predicate func(item T) bool) []T { |
| 59 | var results []T = nil |
| 60 | for _, item := range slice { |
| 61 | if predicate(item) { |
| 62 | results = append(results, item) |
| 63 | } |
| 64 | } |
| 65 | return results |
| 66 | } |
| 67 | |
| 68 | func SliceExcept[T any](slice []T, predicate func(item T) bool) []T { |
| 69 | return SliceFilter(slice, func(item T) bool { return !predicate(item) }) |
no outgoing calls
no test coverage detected