slices

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyFilter

func ApplyFilter[T any](elems []T, f Predicate[T]) []T

ApplyFilter applies a predicate function on a list of elements; keeping only those elements that satisfy the predicate.

Parameters:

  • elems: the list of elements to filter.
  • f: the predicate function to apply.

Returns:

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

Behaviors:

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

WARNING: This function modifies the original list if there is at least one element that does not satisfy the predicate. Make sure to copy the list before calling this function if you want to keep the original list.

func ApplyReject

func ApplyReject[T any](elems []T, f Predicate[T]) []T

ApplyReject applies a predicate function on a list of elements; keeping only those elements that do not satisfy the predicate.

Parameters:

  • elems: the list of elements to filter.
  • f: the predicate function to apply.

Returns:

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

Behaviors:

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

WARNING: This function modifies the original list if there is at least one element that satisfies the predicate. Make sure to copy the list before calling this function if you want to keep the original list.

func ComplexFilter added in v0.1.1

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

ComplexFilter applies a filter function on a slice of elements based on the provided filter function.

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 list of selected indices and a flag indicating early exit.

Returns:

  • []T: The filtered slice of elements.

Behavior:

  • If the provided slice is empty or the filter function is nil, returns nil.
  • The filter function should return the selected indices and a flag to indicate early exit.
  • The filtered slice contains only the elements corresponding to the selected indices.

func RejectNils

func RejectNils[T interface {
	IsNil() bool
}](elems []T) []T

RejectNils removes all nil elements from the slices if they implement the IsNil() method.

IsNil must return false if the element is nil, otherwise true.

Parameters:

  • elems: The elements to remove nils from.

Returns:

  • []T: The elements without nils. Nil if all the elements are nil or no elements were specified.

Types

type Builder added in v0.1.1

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

Builder is a slice builder.

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.

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