Filter returns a new slice containing only elements that match the predicate. This is a pure function that does not modify the input slice.
(slice []T, predicate func(T) bool)
| 14 | // Filter returns a new slice containing only elements that match the predicate. |
| 15 | // This is a pure function that does not modify the input slice. |
| 16 | func Filter[T any](slice []T, predicate func(T) bool) []T { |
| 17 | result := make([]T, 0, len(slice)) |
| 18 | for _, item := range slice { |
| 19 | if predicate(item) { |
| 20 | result = append(result, item) |
| 21 | } |
| 22 | } |
| 23 | return result |
| 24 | } |
| 25 | |
| 26 | // Map transforms each element in a slice using the provided function. |
| 27 | // This is a pure function that does not modify the input slice. |
no outgoing calls