compact

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2025 License: ISC Imports: 2 Imported by: 0

Documentation

Overview

Package `compact` provides utilities for compacting pre-sorted slices and sequences.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compact

func Compact[E any, S ~[]E](src S, eq_fn func(E, E) bool, to_fn func(S) E) iter.Seq[E]

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

func CompactSeq[E any](src iter.Seq[E], eq_fn func(E, E) bool, to_fn func([]E) E) iter.Seq[E]

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

func Compacted[E any, S ~[]E](src S, eq_fn func(E, E) bool, to_fn func(S) E) S

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

func CompactedSeq[E any](src iter.Seq[E], eq_fn func(E, E) bool, to_fn func([]E) E) []E

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL