Skip to content

bobg/go-generics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-generics - Generic slice, map, set, iterator, and goroutine utilities for Go

Go Reference Go Report Card Tests Coverage Status Mentioned in Awesome Go

This is go-generics, a collection of typesafe generic utilities for slices, sets, and goroutine patterns in Go.

Compatibility note

This is version 4 of this library, for the release of Go 1.23.

Earlier versions of this library included a package, iter, that defined an iterator type over several types of containers, and functions for operating with iterators. However, Go 1.23 defines its own, better iterator mechanism via the new “range over function” language feature, plus a new standard-library package also called iter. This version of the go-generics library therefore does away with its iter package. The handy functions that iter contained for working with iterators (Filter, Map, FirstN, and many more) can now be found in the github.com/bobg/seqs library, adapted for Go 1.23 iterators.

(This version of go-generics might have kept iter as a drop-in replacement for the standard-library package, but was unable because the standard library defines two types, iter.Seq[K] and iter.Seq2[K, V], that go-generics would have had to reference using type aliases; but Go type aliases do not yet permit type parameters.)

Earlier versions of this library included combinatorial operations in the slices package. Those have now been moved to their own library, github.com/bobg/combo.

Earlier versions of this library included a maps package, which was a drop-in replacement for the stdlib maps (added in Go 1.21) plus a few convenience functions. With the advent of Go 1.23 iterators, those few convenience functions are mostly redundant (and a couple of them − Keys and Values − conflict with new functions in the standard library), so maps has been removed.

Earlier versions of this library included a Find method on the set.Of[T] type, for finding some element in the set that satisfies a given predicate. This method has been removed in favor of composing operations with functions from github.com/bobg/seqs. For example, s.Find(pred) can now be written as seqs.First(seqs.Filter(s.All(), pred)).

Slices

The slices package is useful in three ways:

  • It encapsulates hard-to-remember Go idioms for inserting and removing elements to and from the middle of a slice;
  • It adds the ability to index from the right end of a slice using negative integers (for example, Get(s, -1) is the same as s[len(s)-1]); and
  • It includes Map, Filter, and a few other such functions for processing slice elements with callbacks.

The slices package is a drop-in replacement for the slices package added to the Go stdlib in Go 1.21. There is one difference: this version of slices allows the index value passed to Insert, Delete, and Replace to be negative for counting backward from the end of the slice.

Set

The set package implements the usual collection of functions for sets: Intersect, Union, Diff, etc., as well as member functions for adding and removing items, checking for the presence of items, and iterating over items.

Parallel

The parallel package contains functions for coordinating parallel workers:

  • Consumers manages a set of N workers consuming a stream of values produced by the caller.
  • Producers manages a set of N workers producing a stream of values consumed by the caller.
  • Values concurrently produces a set of N values.
  • Pool manages access to a pool of concurrent workers.
  • Protect manages concurrent access to a protected data value.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages