Skip to content

Commit

Permalink
feat: type/slicesutil: add LengthCounts(), Sort()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Nov 27, 2023
1 parent 126f3c3 commit e11df2e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions type/slicesutil/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func Dedupe[S ~[]E, E comparable](s S) S {
return deduped
}

// LengthCounts returns a `map[uint]uint` where the keys are element lengths and the values
// are counts of slices with those lenghts.

Check failure on line 26 in type/slicesutil/slice.go

View workflow job for this annotation

GitHub Actions / lint (1.x, ubuntu-latest)

`lenghts` is a misspelling of `lengths` (misspell)
func LengthCounts[E any](s [][]E) map[uint]uint {
stats := map[uint]uint{}
for _, si := range s {
stats[uint(len(si))]++
}
return stats
}

func ElementCounts[E comparable](s []E) map[E]int {
m := map[E]int{}
for _, si := range s {
Expand Down Expand Up @@ -78,6 +88,12 @@ func Shift[S ~[]E, E any](s S) (E, S) {
return s[0], s[1:]
}

func Sort[E constraints.Ordered](s []E) {
sort.Slice(s, func(i, j int) bool {
return s[i] < s[j]
})
}

func SortSliceOfSlice[S ~[][]E, E constraints.Ordered | string](s S, indexes ...uint) {
for _, idx := range indexes {
sort.Slice(s, func(i, j int) bool {
Expand Down

0 comments on commit e11df2e

Please sign in to comment.