|
| 1 | +//go:build go1.18 |
| 2 | +// +build go1.18 |
| 3 | + |
| 4 | +package sliceext |
| 5 | + |
| 6 | +import ( |
| 7 | + optionext "github.com/go-playground/pkg/v5/values/option" |
| 8 | + "sort" |
| 9 | +) |
| 10 | + |
| 11 | +// Retain retains only the elements specified by the function. |
| 12 | +// |
| 13 | +// This shuffles and returns the retained values of the slice. |
| 14 | +func Retain[T any](slice []T, fn func(v T) bool) []T { |
| 15 | + var j int |
| 16 | + for _, v := range slice { |
| 17 | + if fn(v) { |
| 18 | + slice[j] = v |
| 19 | + j++ |
| 20 | + } |
| 21 | + } |
| 22 | + return slice[:j] |
| 23 | +} |
| 24 | + |
| 25 | +// Filter filters out the elements specified by the function. |
| 26 | +// |
| 27 | +// This shuffles and returns the retained values of the slice. |
| 28 | +func Filter[T any](slice []T, fn func(v T) bool) []T { |
| 29 | + var j int |
| 30 | + for _, v := range slice { |
| 31 | + if fn(v) { |
| 32 | + continue |
| 33 | + } |
| 34 | + slice[j] = v |
| 35 | + j++ |
| 36 | + } |
| 37 | + return slice[:j] |
| 38 | +} |
| 39 | + |
| 40 | +// Map maps a slice of []T -> []U using the map function. |
| 41 | +func Map[T, U any](slice []T, init U, fn func(accum U, v T) U) U { |
| 42 | + if len(slice) == 0 { |
| 43 | + return init |
| 44 | + } |
| 45 | + accum := init |
| 46 | + for _, v := range slice { |
| 47 | + accum = fn(accum, v) |
| 48 | + } |
| 49 | + return accum |
| 50 | +} |
| 51 | + |
| 52 | +// Sort sorts the sliceWrapper x given the provided less function. |
| 53 | +// |
| 54 | +// The sort is not guaranteed to be stable: equal elements |
| 55 | +// may be reversed from their original order. |
| 56 | +// |
| 57 | +// For a stable sort, use SortStable. |
| 58 | +func Sort[T any](slice []T, less func(i T, j T) bool) { |
| 59 | + sort.Slice(slice, func(j, k int) bool { |
| 60 | + return less(slice[j], slice[k]) |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +// SortStable sorts the sliceWrapper x using the provided less |
| 65 | +// function, keeping equal elements in their original order. |
| 66 | +func SortStable[T any](slice []T, less func(i T, j T) bool) { |
| 67 | + sort.SliceStable(slice, func(j, k int) bool { |
| 68 | + return less(slice[j], slice[k]) |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +// Reduce reduces the elements to a single one, by repeatedly applying a reducing function. |
| 73 | +func Reduce[T any](slice []T, fn func(accum T, current T) T) optionext.Option[T] { |
| 74 | + if len(slice) == 0 { |
| 75 | + return optionext.None[T]() |
| 76 | + } |
| 77 | + accum := slice[0] |
| 78 | + for _, v := range slice { |
| 79 | + accum = fn(accum, v) |
| 80 | + } |
| 81 | + return optionext.Some(accum) |
| 82 | +} |
0 commit comments