| 1 | package collect |
| 2 | |
| 3 | // Reverse returns a new slice with the elements of inp in reverse order. |
| 4 | // The input slice is not modified. |
| 5 | func Reverse[E any](inp []E) []E { |
| 6 | if inp == nil { |
| 7 | return nil |
| 8 | } |
| 9 | rv := make([]E, len(inp)) |
| 10 | for i, v := range inp { |
| 11 | rv[len(inp)-1-i] = v |
| 12 | } |
| 13 | return rv |
| 14 | } |
| 15 | |
| 16 | // UniqueComparableOrdered deduplicates elements of inp, preserving the |
| 17 | // insertion order of the first occurrence of each value. |
| 18 | func UniqueComparableOrdered[V comparable](inp []V) []V { |
| 19 | seen := make(map[V]struct{}, len(inp)) |
| 20 | rv := make([]V, 0, len(inp)) |
| 21 | for _, v := range inp { |
| 22 | if _, ok := seen[v]; ok { |
| 23 | continue |
| 24 | } |
| 25 | seen[v] = struct{}{} |
| 26 | rv = append(rv, v) |
| 27 | } |
| 28 | return rv[:len(rv):len(rv)] |
| 29 | } |
| 30 | |