sliceutils

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sliceutils defines utilities for working with slices; before using this package, consider using the package "slices".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnySliceToSlice

func AnySliceToSlice[T any](value []any) maybe.Maybe[[]T]

AnySliceToSlice returns a typed slice from the specified 'any' slice; additionally, returns false when the specified 'any' slice cannot be converted into a typed slice, otherwise true.

func AnyToAnySlice

func AnyToAnySlice(value any) maybe.Maybe[[]any]

AnyToAnySlice returns an 'any' slice from the specified 'any' value; additionally, returns false when the specified 'any' value cannot be converted to an 'any' slice, otherwise true.

func AssertContains

func AssertContains[T comparable](target []T, value T) error

AssertContains returns an error if the given target slice of values does not contain the give value, otherwise it returns nil. The returned error describes the missing values. For a non-asserting contains function, use slices.Contains from the Go standard library.

func CollectErrors

func CollectErrors[T any](root error, elements ...result.Result[T]) error

CollectErrors returns a multi-error for all errors in the given slice of result.Result.

func ContainsAll added in v0.2.0

func ContainsAll[T comparable](target []T, values []T) bool

ContainsAll returns true if the given target slice of values contains all the given values, otherwise it returns false.

func Detect

func Detect[T any](
	detect filters.Filter[T],
	values []T,
) result.Result[maybe.Maybe[T]]

Detect returns the first value found in the given slice of values that matches the given filter. Returns a maybe.Nothing if no value is found. Returns a result.Error if the filter fails.

func DetectNoError

func DetectNoError[T any](
	detect filters.FilterNoError[T],
	values []T,
) maybe.Maybe[T]

DetectNoError returns the first value found in the given slice of values that matches the given filter. Returns a maybe.Nothing if no value is found.

func DetectNoErrorResult

func DetectNoErrorResult[T any](
	filter filters.FilterNoError[T],
	values result.Result[[]T],
) result.Result[maybe.Maybe[T]]

DetectNoErrorResult calls DetectNoError on the slice encapsulated in the given result. If the given result encapsulates an error, that error is returned in a result of the return type.

func DetectResult

func DetectResult[T any](
	filter filters.Filter[T],
	source result.Result[[]T],
) result.Result[maybe.Maybe[T]]

DetectResult returns the first value found in the given slice of values that matches the given filter. Returns a maybe.Nothing if no value is found. Returns a result.Error if the filter fails.

func Disjoint

func Disjoint[T comparable](left []T, right []T) result.Result[[]T]

Disjoint returns the disjoint set of the given two slices; never nil.

func Duplicate added in v0.2.0

func Duplicate[T any](src []T) []T

Duplicate returns a newly allocated copy of the given slice.

func FirstError

func FirstError[T any](elements ...result.Result[T]) error

FirstError returns the first error, if any, in the given slice of result.Result.

func Fold

func Fold[T, S any](
	folder folders.Folder[T, S],
	initial T,
	source []S,
) result.Result[T]

Fold passes the first element of the given slice and the given initial value to the given folder. The next element and the value in the result returned from the last call to the given folder is passed to the folder until all elements have been passed or an error is returned from the folder.

func FoldNoError

func FoldNoError[T, S any](
	folder folders.FolderNoError[T, S],
	initial T,
	source []S,
) T

FoldNoError passes the first element of the given slice and the given initial value to the given folder. The next element and the result of the last call to the given folder is passed to the folder until all elements have been passed.

func FoldNoErrorResult

func FoldNoErrorResult[T, S any](
	folder folders.FolderNoError[T, S],
	initial T,
	source result.Result[[]S],
) result.Result[T]

FoldNoErrorResult calls Fold on the slice encapsulated in the given result. If the given result encapsulates an error, that error is returned in a result of the return type.

func FoldResult

func FoldResult[T, S any](
	folder folders.Folder[T, S],
	initial T,
	source result.Result[[]S],
) result.Result[T]

FoldResult calls Fold on the slice encapsulated in the given result. If the given result encapsulates an error, that error is returned in a result of the return type.

func Head[T any](source []T) maybe.Maybe[T]

Head returns the first element of the given slice.

func Intersection

func Intersection[T comparable](left []T, right []T) result.Result[[]T]

Intersection returns the intersection of the given two slices; never nil.

func LenAll

func LenAll[T any](values ...[]T) int

LenAll returns the accumulated lengths of each of the given values; never negative.

func Map

func Map[T, S any](
	mapper result.FlatMapper[S, T],
	source []S,
) result.Result[[]T]

Map the given source slice, using the given mapper, into a result of a new slice. Returns a result of the new slice containing the mapped values, never nil.

func MapNoError

func MapNoError[T, S any](
	mapper MapperNoError[S, T],
	source []S,
) []T

MapNoError the given source slice, using the given mapper, into a new slice. Returns the new slice containing the mapped values, never nil.

func MapNoErrorResult

func MapNoErrorResult[T, S any](
	mapper MapperNoError[S, T],
	source result.Result[[]S],
) result.Result[[]T]

MapNoErrorResult calls MapNoError on the slice encapsulated in the given result. If the given result encapsulates an error, that error is returned in a result of the return type.

func MapResult

func MapResult[T, S any](
	mapper result.FlatMapper[S, T],
	source result.Result[[]S],
) result.Result[[]T]

MapResult calls Map on the slice encapsulated in the given result. If the given result encapsulates an error, that error is returned in a result of the return type.

func Prepend

func Prepend[T any](target []T, values ...T) []T

Prepend the given values on the target. Returns a new slice, never nil.

func Remove

func Remove[T comparable](target []T, values ...T) []T

Remove from the given slice the given value; returns a new slice, never nil.

func Reverse

func Reverse[T any](values []T) []T

Reverse the given slice. Returns a new slice; never nil.

func Select

func Select[T any](
	filter filters.Filter[T],
	source []T,
) result.Result[[]T]

Select returns a slice the values containing the values from the given slice that match the given filter; never nil.

func SelectNoError

func SelectNoError[T any](
	filter filters.FilterNoError[T],
	source []T,
) []T

SelectNoError returns a slice the values containing the values from the given slice that match the given filter; never nil.

func SelectNoErrorResult

func SelectNoErrorResult[T any](
	filter filters.FilterNoError[T],
	source result.Result[[]T],
) result.Result[[]T]

SelectNoErrorResult returns a slice the values containing the values from the given slice that match the given filter; never nil.

func SelectResult

func SelectResult[T any](
	filter filters.Filter[T],
	source result.Result[[]T],
) result.Result[[]T]

SelectResult returns a slice the values containing the values from the given slice that match the given filter; never nil.

func SelectUsingSubsetInterfaceFilter

func SelectUsingSubsetInterfaceFilter[T any, S any](filter filters.Filter[S], source []T) result.Result[[]T]

SelectUsingSubsetInterfaceFilter mirrors Select, with support for a filter function that acts against an interface of the type to be filtered. If the type to be filtered is the interface itself, then just use Select.

func SelectUsingSubsetInterfaceFilterNoError

func SelectUsingSubsetInterfaceFilterNoError[T any, S any](
	filter filters.FilterNoError[S],
	source []T,
) result.Result[[]T]

SelectUsingSubsetInterfaceFilterNoError mirrors Select, with support for a filter function that acts against an interface of the type to be filtered. If the type to be filtered is the interface itself, then just use Select.

func Tail

func Tail[T any](source []T) maybe.Maybe[[]T]

Tail returns all the elements of the given slice except for the first one.

func ToSlice

func ToSlice[T any](values ...T) []T

ToSlice returns the given variadic argument list as a slice.

func Union

func Union[T comparable](values ...[]T) result.Result[[]T]

Union returns a slice containing the union of the given slices; never nil.

func UniqueUnion

func UniqueUnion[T comparable](values ...[]T) result.Result[[]T]

UniqueUnion returns a slice containing the union of the unique elements of the given slices; never nil.

Types

type MapperNoError

type MapperNoError[T, R any] func(
	T,
) R

MapperNoError defines a function with one argument of any type that returns a single value.

Jump to

Keyboard shortcuts

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