slices

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 2 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilReceiver occurs when a method is called on a receiver that was not expected to be
	// nil. This error can be checked with the == operator.
	//
	// Format:
	//		"receiver must not be nil"
	ErrNilReceiver error

	// ErrNoPredicate occurs when a predicate is not provided.
	//
	// Format:
	// 	"no predicate was provided"
	ErrNoPredicate error
)

Functions

func Filter added in v0.1.2

func Filter[S ~[]E, E any](s *S, predicate func(e E) bool) uint

Filter filters a slice of E's according to a predicate.

Parameters:

  • s: The slice of E's to filter.
  • predicate: The predicate to use for filtering.

Returns:

  • uint: The number of elements filtered-out.

Panics:

  • ErrNoPredicate: If the predicate is nil.

func Reject added in v0.1.2

func Reject[S ~[]E, E any](s *S, predicate func(e E) bool) uint

Reject rejects a slice of E's according to a predicate.

Parameters:

  • s: The slice of E's to reject.
  • predicate: The predicate to use for rejecting.

Returns:

  • uint: The number of elements rejected.

Panics:

  • ErrNoPredicate: If the predicate is nil.

func RejectNils

func RejectNils[S ~[]*E, E any](s *S) uint

RejectNils rejects in-place all nil elements from a slice of E's and returns the number of elements rejected.

Parameters:

  • s: The slice of E's to filter.

Returns:

  • uint: The number of elements rejected.

func RejectZero added in v0.1.7

func RejectZero[S ~[]E, E comparable](s *S) uint

RejectZero rejects in-place all zero-valued elements from a slice of E's and returns the number of elements rejected.

Parameters:

  • s: The slice of E's to filter.

Returns:

  • uint: The number of elements rejected.

Types

type Builder added in v0.1.1

type Builder[E any] struct {
	// contains filtered or unexported fields
}

Builder is a builder for slices. It is only efficent for making many slices one after the other.

An empty builder can be created with the `var b Builder[T]` syntax or with the `new(Builder[T])` constructor.

func (*Builder[E]) Append added in v0.1.1

func (b *Builder[E]) Append(elem E) error

Append appends an element to the slice being built.

Parameters:

  • elem: The element to append.

Returns:

  • error: An error if the element could not be appended.

Errors:

  • ErrNilReceiver: If the receiver is nil.

func (Builder[E]) Build added in v0.1.1

func (b Builder[E]) Build() []E

Build builds the slice being built.

Returns:

  • []E: The slice being built.

func (*Builder[E]) Reset added in v0.1.1

func (b *Builder[E]) Reset() error

Reset resets the builder to its initial state, freeing up any allocated memory so that it can be reused to build another slice. This is more efficient than creating a new builder if you need to build many slices in a row.

Returns:

  • error: An error if the builder could not be reset.

Errors:

  • ErrNilReceiver: If the receiver is nil.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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