slices

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 1 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComplexFilter added in v0.1.1

func ComplexFilter[T any](slice *[]T, fn func(indices *[]int) bool)

ComplexFilter applies a filter function on a slice of elements based on the provided filter function. As with Filter, this function modifies the original list in-place.

This function uses indices for optimization reasons.

Parameters:

  • slice: The slice of elements to filter.
  • fn: The filter function that takes a list of indices and returns a boolean indicating whether to early exit.

Behavior:

  • If the provided slice is empty or the filter function is nil, the original slice is cleared and set to nil.
  • The filter function is called repeatedly with the current list of indices until it returns true or the list of indices is empty.
  • The filtered slice contains only the elements corresponding to the selected indices.

func Filter added in v0.1.2

func Filter[T any](slice *[]T, p Predicate[T])

Filter applies a predicate function on a slice of elements; keeping only those elements that satisfy the predicate. This function modifies the original list in-place.

Parameters:

  • slice: the list of elements to filter.
  • p: the predicate function to apply.

Behavior:

  • If the list is empty, the predicate is nil, or there is no element that satisfies the predicate, the slice is cleared and set to nil.

func IndicesOf added in v0.1.2

func IndicesOf[T comparable](slice []T, sep T) []int

IndicesOf returns a slice of indices that specify where the separator occurs in the data.

Parameters:

  • slice: The data.
  • sep: The separator.

Returns:

  • []int: The indices. Nil if no separator is found.

func Reject added in v0.1.2

func Reject[T any](slice *[]T, p Predicate[T])

Reject applies a predicate function on a slice of elements; keeping only those elements that do not satisfy the predicate. This function modifies the original list in-place.

Parameters:

  • slice: the list of elements to filter.
  • p: the predicate function to apply.

Returns:

  • []T: the list of elements that do not satisfy the predicate.

Behavior:

  • If the list is empty or the predicate is nil, returns nil.

func RejectNils

func RejectNils[T any](slice *[]*T)

RejectNils works like Reject but keeps only non-nil elements.

Parameters:

  • slice: the list of elements to filter.

Types

type Builder added in v0.1.1

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

Builder is a builder for slices. While this is normally not needed, it can have some uses when making many slices one after the other.

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

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

Append appends an element to the slice being built.

Parameters:

  • elem: The element to append.

Returns:

  • error: An error if the receiver is nil.

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

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

Build builds the slice being built.

Returns:

  • []T: The slice being built. Nil if no elements were appended.

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

func (b *Builder[T]) Reset()

Reset resets the builder for reuse.

type Predicate

type Predicate[T any] func(elem T) bool

Predicate is a type of function that checks whether an element satisfies a given condition.

Parameters:

  • elem: the element to check.

Returns:

  • bool: True if the element satisfies the condition, false otherwise.

Jump to

Keyboard shortcuts

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