Standard library / slices

slices.CompactFunc()

Compacts adjacent elements using a caller-defined equality relation. Mutates storage and clears the discarded tail. Expected stdout: '[A B]\n'.

Formal Syntax & Structure

go syntaxgo
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S

Parameters & Attributes

NameTypeDescription
sSInput slice; may share its backing array.
eqfunc(E, E) boolEquality callback, true for equivalent elements.

Return Value

S

Compacts adjacent elements using a caller-defined equality relation. Mutates storage and clears the discarded tail.

Working Example

Example Snippetgo
package main
import (  "fmt"  "slices"  "strings")
func main() {  a := []string{"A", "a", "B"}  fmt.Println(slices.CompactFunc(a, strings.EqualFold))}
Go Reference