Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.27.0] - 2024-01-29
### Changed
- `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.

## [5.26.0] - 2024-01-28
### Added
- `stringsext.Join` a more ergonomic way to join strings with a separator when you don't have a slice of strings.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pkg

![Project status](https://img.shields.io/badge/version-5.26.0-green.svg)
![Project status](https://img.shields.io/badge/version-5.27.0-green.svg)
[![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)
Expand Down
23 changes: 12 additions & 11 deletions slice/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,38 @@
package sliceext

import (
optionext "github.com/go-playground/pkg/v5/values/option"
"sort"

optionext "github.com/go-playground/pkg/v5/values/option"
)

// Retain retains only the elements specified by the function.
//
// This shuffles and returns the retained values of the slice.
// This returns a new slice with references to the underlying data instead of shuffling.
func Retain[T any](slice []T, fn func(v T) bool) []T {
var j int
results := make([]T, 0, len(slice))
for _, v := range slice {
v := v
if fn(v) {
slice[j] = v
j++
results = append(results, v)
}
}
return slice[:j]
return results
}

// Filter filters out the elements specified by the function.
//
// This shuffles and returns the retained values of the slice.
// This returns a new slice with references to the underlying data instead of shuffling.
func Filter[T any](slice []T, fn func(v T) bool) []T {
var j int
results := make([]T, 0, len(slice))
for _, v := range slice {
v := v
if fn(v) {
continue
}
slice[j] = v
j++
results = append(results, v)
}
return slice[:j]
return results
}

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