Skip to content

Commit

Permalink
Update slices.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzotinfena committed Apr 17, 2024
1 parent 554eaba commit 1d224c8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions utils/slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ func Map[Dom any, Codom any](v []Dom, f func(Dom) Codom) []Codom {
}
return mapped
}

func Shrink[T any](v []T) []T {
if 4*len(v) <= cap(v) {
v1 := make([]T, len(v))
copy(v1, v)
return v1
}
return v
}

func Reverse[T any](v []T) {
for i, j := 0, len(v)-1; i < j; i, j = i+1, j-1 {
v[i], v[j] = v[j], v[i]
}
}

0 comments on commit 1d224c8

Please sign in to comment.