collection

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: LGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain added in v0.1.7

func Chain[T any](value T, functions ...func(T) T) T

Chain applies a series of functions to a value in sequence. Each function must take a value of type T and return a value of type T.

func CloneList

func CloneList[T any](source []T) []T

CloneList creates a shallow copy of the given list.

func CloneMap

func CloneMap[K comparable, V any](source map[K]V) map[K]V

CloneMap creates a shallow copy of the given map.

func Compose added in v0.1.7

func Compose[T1 any, T2 any, T3 any](f func(T2) T3, g func(T1) T2) func(T1) T3

Compose takes two functions f and g, and returns a new function that applies g first and then f.

func Count added in v0.1.6

func Count[T any](slice []T, predicate func(T) bool) int

Count function: Counts elements in a slice based on a predicate function

func Curry added in v0.1.7

func Curry[T1, T2, R any](fn func(T1, T2) R) func(T1) func(T2) R

Curry takes a function fn with two parameters and returns a curried version of it.

func Distinct

func Distinct[T comparable](slice []T) []T

Distinct returns a slice containing only unique elements.

func DistinctFunc

func DistinctFunc[T comparable](slice []T, compareFunc func(a, b T) bool) []T

DistinctFunc returns a slice containing unique elements using a custom comparison function.

func Exists added in v0.1.5

func Exists[T any](collection []T, condition func(T) bool) bool

Exists checks if any element in the collection satisfies the condition. T is a generic type parameter that can represent any type.

func Filter

func Filter[T any](source []T, filterFunc func(item T) bool) []T

Filter returns a filtered list based on the provided function.

func FilterMap

func FilterMap[K comparable, V any](source map[K]V, filteringFunc func(key K, value V) bool) map[K]V

FilterMap filters a hashmap based on a provided function.

func FlatMap

func FlatMap[T1 any](source [][]T1) []T1

FlatMap flattens a list of lists into a single list.

func ForEach

func ForEach[T any](source []T, action func(item T))

ForEach executes a function for each item in the list.

func ForEachWithError

func ForEachWithError[T any](source []T, action func(item T) error) error

ForEachWithError executes a function for each item and handles errors.

func Map

func Map[T1 any, T2 any](source []T1, transform func(item T1) T2) []T2

Map applies a transformation function to each item in the list and returns a new list.

func MapFilter added in v0.1.8

func MapFilter[T any](input []T, mapFn func(T) T, filterFn func(T) bool) []T

MapFilter applies a map then filter function to a slice of any type

func MapFilterMap added in v0.1.8

func MapFilterMap[K comparable, V any](input map[K]V, mapFn func(K, V) (K, V), filterFn func(K, V) bool) map[K]V

MapFilterMap applies a map function and a filter function to a map (hash map).

func MapFilterMapWithError added in v0.1.8

func MapFilterMapWithError[K comparable, V any](input map[K]V, mapFn func(K, V) (K, V, error), filterFn func(K, V) bool) (map[K]V, error)

MapFilterMapWithError applies a map function that may return an error, then filters the results.

func MapFilterWithError added in v0.1.8

func MapFilterWithError[T any](input []T, mapFn func(T) (T, error), filterFn func(T) bool) ([]T, error)

MapFilterWithError applies a map function that may return an error, then filters the results.

func MapReturnWithError

func MapReturnWithError[T1 any, T2 any](source []T1, mappingFunc func(item T1) (T2, error)) ([]T2, error)

MapReturnWithError applies a transformation function to each item and handles errors.

func Max added in v0.1.6

func Max[T constraints.Ordered](slice []T) (max T, found bool)

Generic function to find the highest value

func MaxBy added in v0.1.6

func MaxBy[T any, R constraints.Ordered](slice []T, getter func(T) R) (max T, found bool)

MaxBy function finds the maximum element based on a getter function

func Min added in v0.1.6

func Min[T constraints.Ordered](slice []T) (min T, found bool)

Generic function to find the lowest value

func MinBy added in v0.1.6

func MinBy[T any, R constraints.Ordered](slice []T, getter func(T) R) (min T, found bool)

MinBy function finds the minimum element based on a getter function

func Partition added in v0.1.6

func Partition[T any](slice []T, predicate func(T) bool) ([]T, []T)

Partition function splits a slice into two slices based on a predicate function

func Pipe added in v0.1.7

func Pipe[T1 any, T2 any, T3 any](g func(T1) T2, f func(T2) T3) func(T1) T3

Pipe takes two functions, g and f, and returns a new function that applies g first and then f.

func Reduce

func Reduce[T any](source []T, reduceFunc func(acc T, item T) T, initialValue T) T

Reduce reduces a list to a single value using the provided function.

func Sort

func Sort[T any](list []T, less func(i, j int) bool) []T

Sort sorts a slice using a custom less function.

func Sum

func Sum[T Summable](list []T) T

Sum returns the sum of elements in a slice of summable types.

Types

type Summable

type Summable interface {
	int | int32 | int64 | float32 | float64
}

Summable includes all types that can be summed, such as integers and floats.

Jump to

Keyboard shortcuts

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