Documentation
¶
Overview ¶
Package `compact` provides utilities for compacting pre-sorted slices and sequences.
Index ¶
- func Compact[E any, S ~[]E](src S, eq_fn func(E, E) bool, to_fn func(S) E) iter.Seq[E]
- func CompactSeq[E any](src iter.Seq[E], eq_fn func(E, E) bool, to_fn func([]E) E) iter.Seq[E]
- func Compacted[E any, S ~[]E](src S, eq_fn func(E, E) bool, to_fn func(S) E) S
- func CompactedSeq[E any](src iter.Seq[E], eq_fn func(E, E) bool, to_fn func([]E) E) []E
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compact ¶
Returns an iterator that yields elements of src, with runs of duplicates compacted into a single element.
If to_fn is provided it receives runs of duplicates and is responsible for collapsing them into a single element, otherwise the first element in the run is used.
Example ¶
package main
import (
"fmt"
"gitlab.com/cptpackrat/go-seq/compact"
)
type Pkg struct {
Name, Vers string
}
var pkgs = []Pkg{
{"foo", "1.2.5"},
{"foo", "1.2.4"},
{"foo", "1.2.3"},
{"foo", "1.2.0"},
{"bar", "1.2.4"},
{"boo", "1.1.5"},
{"boo", "1.1.2"},
{"boo", "1.1.0"},
{"baz", "1.0.0"},
}
func main() {
// collapse multiple versions of the same package
for p := range compact.Compact(pkgs, func(a, b Pkg) bool {
return a.Name == b.Name
}, func(ps []Pkg) Pkg {
return Pkg{
Name: ps[0].Name,
Vers: ps[0].Vers + fmt.Sprintf(" (+%d)", len(ps)-1),
}
}) {
fmt.Println(p.Name, p.Vers)
}
}
Output: foo 1.2.5 (+3) bar 1.2.4 boo 1.1.5 (+2) baz 1.0.0
func CompactSeq ¶
Returns an iterator that yields elements of src, with runs of duplicates compacted into a single element.
If to_fn is provided it receives runs of duplicates and is responsible for collapsing them into a single element, otherwise the first element in the run is used.
func Compacted ¶
Returns a new slice containing elements of src, with runs of duplicates compacted into a single element.
If to_fn is provided it receives runs of duplicates and is responsible for collapsing them into a single element, otherwise the first element in the run is used.
func CompactedSeq ¶
Returns a new slice containing elements of src, with runs of duplicates compacted into a single element.
If to_fn is provided it receives runs of duplicates and is responsible for collapsing them into a single element, otherwise the first element in the run is used.
Types ¶
This section is empty.