SliceExt

package
v0.2.27 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterByNegativeWeight

func FilterByNegativeWeight[T any](S []T, weightFunc func(T) int) []T

FilterByNegativeWeight is a function that iterates over the slice and applies the weight function to each element. The returned slice contains the elements with the minimum weight. If multiple elements have the same minimum weight, they are all returned.

Parameters:

  • S: slice of elements.
  • weightFunc: function that takes an element and returns an integer.

Returns:

  • []T: slice of elements with the minimum weight.

func FilterByPositiveWeight

func FilterByPositiveWeight[T any](S []T, weightFunc func(T) (int, bool)) []T

FilterByPositiveWeight is a function that iterates over the slice and applies the weight function to each element. The returned slice contains the elements with the maximum weight. If multiple elements have the same maximum weight, they are all returned.

Parameters:

  • S: slice of elements.
  • weightFunc: function that takes an element and returns an integer.

Returns:

  • []T: slice of elements with the maximum weight.

func IndexOfDuplicate added in v0.2.24

func IndexOfDuplicate[T itf.Comparable](S []T) int

IndexOfDuplicate returns the index of the first duplicate element in the slice. If there are no duplicates, it returns -1.

Parameters:

  • S: slice of elements.

Returns:

  • int: index of the first duplicate element or -1 if there are no duplicates.

func IndexOfDuplicateFunc added in v0.2.24

func IndexOfDuplicateFunc[T any](S []T, equals func(T, T) bool) int

IndexOfDuplicateFunc returns the index of the first duplicate element in the slice. If there are no duplicates, it returns -1.

Parameters:

  • S: slice of elements.
  • equals: function that takes two elements and returns a bool.

Returns:

  • int: index of the first duplicate element or -1 if there are no duplicates.

func RemoveDuplicates added in v0.2.24

func RemoveDuplicates[T itf.Comparable](S []T) []T

RemoveDuplicates removes duplicate elements from the slice.

Parameters:

  • S: slice of elements.

Returns:

  • []T: slice of elements with duplicates removed.

func RemoveDuplicatesFunc added in v0.2.24

func RemoveDuplicatesFunc[T any](S []T, equals func(T, T) bool) []T

RemoveDuplicatesFunc removes duplicate elements from the slice.

Parameters:

  • S: slice of elements.
  • equals: function that takes two elements and returns a bool.

Returns:

  • []T: slice of elements with duplicates removed.

func SFSeparate added in v0.2.21

func SFSeparate[T any](S []T, filter PredicateFilter[T]) ([]T, []T)

SFSeparate is a function that iterates over the slice and applies the filter function to each element. The returned slices contain the elements that satisfy and do not satisfy the filter function.

Parameters:

  • S: slice of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • []T: slice of elements that satisfy the filter function.
  • []T: slice of elements that do not satisfy the filter function.

func SFSeparateEarly added in v0.2.21

func SFSeparateEarly[T any](S []T, filter PredicateFilter[T]) ([]T, bool)

SFSeparateEarly is a variant of SFSeparate that returns all successful elements. If there are none, it returns the original slice and false.

Parameters:

  • S: slice of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • []T: slice of elements that satisfy the filter function or the original slice.
  • bool: true if there are successful elements, otherwise false.

func SliceFilter

func SliceFilter[T any](S []T, filter PredicateFilter[T]) []T

SliceFilter is a function that iterates over the slice and applies the filter function to each element. The returned slice contains the elements that satisfy the filter function.

Parameters:

  • S: slice of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • []T: slice of elements that satisfy the filter function.

Types

type PredicateFilter

type PredicateFilter[T any] func(T) bool

PredicateFilter is a type that defines a slice filter function.

Parameters:

  • T: The type of the elements in the slice.

Returns:

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

func Intersect

func Intersect[T any](funcs ...PredicateFilter[T]) PredicateFilter[T]

Intersect returns a PredicateFilter function that checks if an element satisfies all the PredicateFilter functions in funcs. It returns false as soon as it finds a function in funcs that the element does not satisfy.

Parameters:

  • funcs: A slice of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies all the PredicateFilter functions in funcs.

func Union

func Union[T any](funcs ...PredicateFilter[T]) PredicateFilter[T]

Union returns a PredicateFilter function that checks if an element satisfies at least one of the PredicateFilter functions in funcs. It returns true as soon as it finds a function in funcs that the element satisfies.

Parameters:

  • funcs: A slice of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies at least one of the PredicateFilter functions in funcs.

Jump to

Keyboard shortcuts

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