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) SParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | S | Input slice; may share its backing array. |
| eq | func(E, E) bool | Equality 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))}