Skip to content

Commit 67dfc10

Browse files
author
Dean Karn
authored
Retain & Filter refactor (#46)
`sliceext.Retain` & `sliceext.Filter` to not shuffle data in the underlying slice array but create new slice referencing the data instead. In practice, it can cause unexpected behaviour and users expectations not met when the same data is also referenced elsewhere. If anyone still requires a `shuffle` implementation for efficiency I'd be happy to add a separate function for that as well.
1 parent a3ce712 commit 67dfc10

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [5.27.0] - 2024-01-29
10+
### Changed
11+
- `sliceext.Retain` & `sliceext.Filter` to not shuffle data in the underlying slice array but create new slice referencing the data instead. In practice, it can cause unexpected behaviour and users expectations not met when the same data is also referenced elsewhere. If anyone still requires a `shuffle` implementation for efficiency I'd be happy to add a separate function for that as well.
12+
913
## [5.26.0] - 2024-01-28
1014
### Added
1115
- `stringsext.Join` a more ergonomic way to join strings with a separator when you don't have a slice of strings.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# pkg
22

3-
![Project status](https://img.shields.io/badge/version-5.26.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-5.27.0-green.svg)
44
[![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
66
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)

slice/slice.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@
44
package sliceext
55

66
import (
7-
optionext "github.com/go-playground/pkg/v5/values/option"
87
"sort"
8+
9+
optionext "github.com/go-playground/pkg/v5/values/option"
910
)
1011

1112
// Retain retains only the elements specified by the function.
1213
//
13-
// This shuffles and returns the retained values of the slice.
14+
// This returns a new slice with references to the underlying data instead of shuffling.
1415
func Retain[T any](slice []T, fn func(v T) bool) []T {
15-
var j int
16+
results := make([]T, 0, len(slice))
1617
for _, v := range slice {
18+
v := v
1719
if fn(v) {
18-
slice[j] = v
19-
j++
20+
results = append(results, v)
2021
}
2122
}
22-
return slice[:j]
23+
return results
2324
}
2425

2526
// Filter filters out the elements specified by the function.
2627
//
27-
// This shuffles and returns the retained values of the slice.
28+
// This returns a new slice with references to the underlying data instead of shuffling.
2829
func Filter[T any](slice []T, fn func(v T) bool) []T {
29-
var j int
30+
results := make([]T, 0, len(slice))
3031
for _, v := range slice {
32+
v := v
3133
if fn(v) {
3234
continue
3335
}
34-
slice[j] = v
35-
j++
36+
results = append(results, v)
3637
}
37-
return slice[:j]
38+
return results
3839
}
3940

4041
// Map maps a slice of []T -> []U using the map function.

0 commit comments

Comments
 (0)