ectolinq

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2025 License: MIT Imports: 8 Imported by: 1

README

ectolinq

ectolinq is a powerful Go library that provides LINQ-like operations for slices and collections, along with additional utility functions for working with structs and pointers. It leverages Go's generics to provide type-safe operations.

Features

Slice Operations
  • Filtering: Filter, Where, Take, Skip, TakeWhile, SkipWhile
  • Transformation: Map, Reverse, Chunk, Flatten
  • Aggregation: Reduce, Sum, Average, Min, Max
  • Search: Find, FindIndex, FindLast, Contains, Any, All
  • Set Operations: Distinct, Union, Intersect, Except
  • Grouping: Group, GroupWhere
  • Sorting: SortWhere
  • Array Manipulation: Push, Pop, Shift, Unshift, Replace, ReplaceAll
List Type

A convenient List[T] type that wraps slices and provides all operations as methods:

go
    list := NewListint
    list.Push(1).Push(2).Push(3)
    filtered := list.Filter(func(x int) bool {
        return x > 1
    })
Struct Utilities
  • Field Access: Get, Set, HasField, GetFieldNames
  • Conversion: ToMap, FromMap
  • Deep Copy: DeepCopy
  • Comparison: Equals, IsEmpty
Pointer Utilities
  • Safety: Safe - Safely dereference pointers
  • Creation: New - Create new pointers. Helpful for primitive types like &bool, &int, &string, etc.
  • Type Checking: IsPointer
General Utilities
  • Ternary - Conditional operator
  • Default - Default value handling
  • IsZero - Zero value checking

Installation

go get -u github.com/Gobusters/ectolinq
import "github.com/Gobusters/ectolinq"
numbers := []int{1, 2, 3, 4, 5}
// Filter even numbers
evens := ectolinq.Filter(numbers, func(n int) bool {
    return n%2 == 0
})
// Map numbers to their squares
squares := ectolinq.Map(numbers, func(n int) int {
    return n * n
})
// Find first number greater than 3
first := ectolinq.Find(numbers, func(n int) bool {
    return n > 3
})
Using List Type
list := ectolinq.NewListstring
list.Push("apple").Push("banana").Push("cherry")
filtered := list.
    Filter(func(s string) bool { return len(s) > 5 }).
    Map(func(s string) string { return strings.ToUpper(s) })
Working with Structs
type Person struct {
    Name string
    Age int
    Address struct {
        City string
    }
}
person := Person{Name: "John", Age: 30}
// Get field value
city, := ectolinq.Get(person, "Address.City")
// Set field value
ectolinq.Set(&person, "Age", 31)
// Convert to map
personMap, := ectolinq.ToMap(person)
Using Pointer Utilities
import "github.com/Gobusters/ectolinq/pointer"

ptr := pointer.New("hello")
safe := pointer.Safe(ptr) // Returns "hello"
var nilPtr *string
safe = pointer.Safe(nilPtr) // Returns empty string instead of panicking

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](items []T, predicate func(T) bool) bool

All determines whether all elements of an slice satisfy a condition items: The slice to search predicate: The predicate to test each element against

func Any

func Any[T any](items []T, predicate func(T) bool) bool

Any determines whether any element of an slice satisfies a condition items: The slice to search predicate: The predicate to test each element against

func At

func At[T any](items []T, index int) T

At returns the value at the provided index or the default value if the index is out of bounds items: the slice index: the index

func Average

func Average[T int | int32 | int64 | float32 | float64](items []T) float64

Average returns the average of all elements in an slice items: The slice to average

func Chunk

func Chunk[T any](items []T, size int) [][]T

Chunk returns a new slice with elements grouped into chunks of the specified size items: The slice to chunk size: The size of each chunk

func Contains

func Contains[T comparable](items []T, value T) bool

Contains determines whether an slice contains a specific value items: The slice to search value: The value to locate in the slice

func ContainsValue

func ContainsValue[T comparable](slice []T, element T) bool

ContainsValue checks if a slice contains a specific element

func Count

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

Count returns the number of elements in an slice that satisfy a condition items: The slice to search predicate: The predicate to test each element against

func CountEntries

func CountEntries[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) int

CountEntries returns the number of key-value pairs in the map that satisfy the predicate

func DeepCopy added in v1.0.1

func DeepCopy[T any](s T) (T, error)

DeepCopy creates a deep copy of a value

func Default added in v1.0.2

func Default[T any](value T, defaultValue T) T

Default returns the value if it is not zero, otherwise it returns the default value

func Distinct

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

Distinct returns distinct elements from an slice items: The slice to search

func DistinctBy

func DistinctBy[T any, U comparable](items []T, keySelector func(T) U) []T

DistinctBy returns distinct elements from an slice based on a key selector items: The slice to search keySelector: The function to extract the key from each element

func Equals

func Equals[T any](a T, b T) bool

Equals compares two values for equality. It uses reflect.DeepEqual for the comparison.

func Except

func Except[T comparable](items []T, other []T) []T

Except returns the elements of an slice that do not appear in a second slice items: The slice to search other: The slice whose elements that also occur in the first slice will cause those elements to be removed from the returned slice

func Filter

func Filter[T any](items []T, predicate func(T) bool) []T

Filter removes all elements from an slice that satisfy the predicate items: The slice to filter predicate: The predicate to test each element against

func FilterMap

func FilterMap[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

FilterMap Returns a map with the elements that satisfy the predicate

func Find

func Find[T any](items []T, predicate func(T) bool) T

Find returns the first element in the slice that satisfies the predicate items: The slice to search predicate: The predicate to test each element against

func FindIndex

func FindIndex[T any](items []T, value T) int

FindIndex returns the index of the first occurrence of a value in the slice items: The slice to search value: The value to locate in the slice

func FindIndexWhere

func FindIndexWhere[T any](items []T, predicate func(T) bool) int

FindIndexWhere returns the index of the first element in the slice that satisfies the predicate items: The slice to search predicate: The predicate to test each element against

func FindKey

func FindKey[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) (K, bool)

FindKey returns the key of the first element that satisfies the predicate

func FindLast

func FindLast[T any](items []T, val T) T

FindLast returns the last element in the slice that satisfies the predicate items: The slice to search predicate: The predicate to test each element against

func FindLastIndex

func FindLastIndex[T any](items []T, value T) int

FindLastIndex returns the index of the last occurrence of a value in the slice items: The slice to search value: The value to locate in the slice

func FindLastIndexWhere

func FindLastIndexWhere[T any](items []T, predicate func(T) bool) int

FindLastIndexWhere returns the index of the last element in the slice that satisfies the predicate items: The slice to search predicate: The predicate to test each element against

func FindLastWhere

func FindLastWhere[T any](items []T, predicate func(T) bool) T

FindLastWhere returns the last element in the slice that satisfies the predicate items: The slice to search predicate: The predicate to test each element against

func FindValue

func FindValue[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) (V, bool)

FindValue returns the value of the first element that satisfies the predicate

func First

func First[T any](items []T) T

First returns the first element in an slice items: The slice to get the first element from

func Flatten

func Flatten[T any](items [][]T) []T

Flatten returns a new slice with all sub-slice elements concatenated items: The slice to flatten

func ForEach

func ForEach[T any](items []T, action func(T))

ForEach performs the specified action on each element of an slice items: The slice to iterate action: The action to perform on each element

func ForEachEntry

func ForEachEntry[K comparable, V any](m map[K]V, f func(key K, value V))

ForEachEntry executes a provided function once for each map entry

func FromMap

func FromMap(m map[string]interface{}, s any) error

FromMap creates a struct from a map[string]interface{} m: The map to create the struct from s: The struct to set the values in

func Get

func Get(s any, path string) (any, error)

Get returns the value of a field in a struct s: The struct to get the value from path: The path to the field

func GetFieldNames

func GetFieldNames(s any) []string

GetFieldNames returns a slice of all field names in a struct s: The struct to get the field names from

func Group

func Group[T any, U comparable](items []T, path string) map[U][]T

Group returns the a Map of the slice where the key is the value of the field at the specified path and the value is an slice of all the elements that match the key items: The slice to convert to a Map path: The path to the field to use as the key. If the field is not found, the item will not be added to the Map

func GroupBy

func GroupBy[K comparable, V any, G comparable](m map[K]V, keySelector func(K, V) G) map[G][]MapEntry[K, V]

GroupBy groups the map entries by a key selector function

func GroupWhere

func GroupWhere[T any, U comparable](items []T, selector func(T) U) map[U][]T

GroupWhere returns the a Map of the slice where the key is the result of the selector function and the value is an slice of all the elements that match the key items: The slice to convert to a Map selector: The selector function to use

func HasField

func HasField(s any, fieldName string) bool

HasField checks if a struct has a specific field s: The struct to check fieldName: The name of the field to check

func IfEmpty

func IfEmpty[T any](val T, def T) T

IfEmpty returns a default value if a value is empty val: The value to check def: The default value

func Intersect

func Intersect[T comparable](items []T, other []T) []T

Intersect returns the elements that appear in two slices items: The slice to search other: The slice whose distinct elements that also appear in the first slice will be returned

func Invert

func Invert[K comparable, V comparable](m map[K]V) map[V]K

Invert swaps the keys and values of the map

func IsEmpty

func IsEmpty[T any](val T) bool

IsEmpty checks if a value is considered empty. It handles various types including slices, maps, strings, channels, pointers, and interfaces.

func IsZero added in v1.0.2

func IsZero[T any](value T) bool

IsZero checks if the value is zero

func Key

func Key[T any, U comparable](items []T, path string) map[U]T

Key returns the a Map of the slice where the key is the value of the field at the specified path items: The slice to convert to a Map path: The path to the field to use as the key. If the field is not found, the item will not be added to the Map

func KeyWhere

func KeyWhere[T any, U comparable](items []T, selector func(T) U) map[U]T

KeyWhere returns the a Map of the slice where the key is the result of the selector function items: The slice to convert to a Map selector: The selector function to use

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys Returns a slice of keys from the map

func Last

func Last[T any](items []T) T

Last returns the last element in an slice items: The slice to get the last element from

func Map

func Map[T any, U any](items []T, selector func(T) U) []U

Map projects each element of an slice into a new form items: The slice to map selector: The selector function to use

func MapEntries

func MapEntries[K comparable, V any](m map[K]V, f func(key K, value V) (K, V)) map[K]V

MapEntries returns a new map with the entries mapped by the function

func MapFromEntries

func MapFromEntries[K comparable, V any](entries []MapEntry[K, V]) map[K]V

MapFromEntries Returns a map from a slice of key-value pairs

func MapKeys

func MapKeys[K comparable, V any](m map[K]V, f func(K) K) map[K]V

MapKeys returns a new map with the function applied to each key

func MapValues

func MapValues[K comparable, V any](m map[K]V, f func(V) V) map[K]V

MapValues returns a new map with the function applied to each value

func Max

func Max[T int | int32 | int64 | float32 | float64](items []T) T

Max returns the maximum value in an slice items: The slice to get the maximum value from

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge combines two or more maps into a new map

func Min

func Min[T int | int32 | int64 | float32 | float64](items []T) T

Min returns the minimum value in an slice items: The slice to get the minimum value from

func Omit

func Omit[K comparable, V any](m map[K]V, keys ...K) map[K]V

Omit creates a new map without the specified keys

func Partition

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

Partition splits an slice into two slices based on a predicate items: The slice to partition predicate: The predicate to test each element against

func Pick

func Pick[K comparable, V any](m map[K]V, keys ...K) map[K]V

Pick creates a new map with only the specified keys

func Pop

func Pop[T any](items []T) (T, []T)

Pop removes the last element from an slice and returns it items: The slice to remove the element from

func Push

func Push[T any](items []T, item T) []T

Push adds an element to the end of an slice items: The slice to add the element to item: The element to add

func Randomize

func Randomize[T any](items []T) []T

Randomize returns a new slice with the elements in a random order items: The slice to randomize

func Reduce

func Reduce[T any, U any](items []T, accumulator func(U, T) U, initialValue ...U) U

Reduce applies an accumulator function over an slice items: The slice to reduce accumulator: The accumulator function to use

func ReduceEntries

func ReduceEntries[K comparable, V any, R any](m map[K]V, initial R, f func(acc R, key K, value V) R) R

ReduceEntries reduces the map to a single value

func Remove

func Remove[T any](items []T, value T) []T

Remove removes the first occurrence of a specific object from an slice items: The slice to remove elements from value: The value to remove from the slice

func RemoveAt

func RemoveAt[T any](items []T, index int) []T

RemoveAt removes an element from an slice at the specified index items: The slice to remove elements from

func RemoveWhere

func RemoveWhere[T any](items []T, predicate func(T) bool) []T

RemoveWhere removes the first element in the slice that satisfies the predicate items: The slice to remove elements from predicate: The predicate to test each element against

func Replace

func Replace[T any](items []T, oldValue T, newValue T) []T

Replace replaces the first occurrence of a value in an slice with another value items: The slice to replace values in oldValue: The value to replace newValue: The value to replace with

func ReplaceAll

func ReplaceAll[T any](items []T, oldValue T, newValue T) []T

ReplaceAll replaces all occurrences of a value in an slice with another value items: The slice to replace values in oldValue: The value to replace newValue: The value to replace with

func ReplaceAllWhere

func ReplaceAllWhere[T any](items []T, value T, predicate func(T) bool) []T

ReplaceAllWhere replaces all occurrences of a value in an slice with the result of the selector function items: The slice to replace values in value: The value to replace with predicate: The selector function to use

func ReplaceWhere

func ReplaceWhere[T any](items []T, value T, predicate func(T) bool) []T

ReplaceWhere replaces the first occurrence of a value in an slice with the result of the selector function items: The slice to replace values in value: The value to replace with predicate: The selector function to use

func Reverse

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

Reverse reverses the order of the elements in the slice in-place items: The slice to reverse

func SequenceEqual

func SequenceEqual[T any](items []T, other []T) bool

SequenceEqual determines whether two slices are equal items: The first slice to compare other: The second slice to compare

func Set

func Set(s any, path string, value any) error

Set sets the value of a field in a struct s: The struct to set the value in path: The path to the field value: The value to set

func Shift

func Shift[T any](items []T) (T, []T)

Shift removes the first element from an slice and returns it items: The slice to remove the element from

func Skip

func Skip[T any](items []T, count int) []T

Skip returns a new slice with the specified number of elements removed from the start of the slice items: The slice to skip elements from count: The number of elements to skip

func SkipLast

func SkipLast[T any](items []T, count int) []T

SkipLast returns a new slice with the specified number of elements removed from the end of the slice items: The slice to skip elements from count: The number of elements to skip

func SkipWhile

func SkipWhile[T any](items []T, predicate func(T) bool) []T

SkipWhile returns a new slice with elements removed from the start of the slice while the predicate returns true items: The slice to skip elements from predicate: The predicate to test each element against

func Slice

func Slice[T any](items []T, start int, end int) []T

Slice returns a new slice with the elements from the specified start index to the specified end index items: The slice to slice start: The index to start at end: The index to end at

func SortWhere

func SortWhere[T any](items []T, comparer func(T, T) bool) []T

SortWhere sorts the elements of an slice in place using the specified comparer function items: The slice to sort comparer: The comparer function to use

func Sum

func Sum[T int | int32 | int64 | float32 | float64](items []T) T

Sum returns the sum of all elements in an slice items: The slice to sum

func Take

func Take[T any](items []T, count int) []T

Take returns a new slice with the specified number of elements from the start of the slice items: The slice to take elements from count: The number of elements to take

func TakeLast

func TakeLast[T any](items []T, count int) []T

TakeLast returns a new slice with the specified number of elements from the end of the slice items: The slice to take elements from count: The number of elements to take

func TakeWhile

func TakeWhile[T any](items []T, predicate func(T) bool) []T

TakeWhile returns a new slice with elements from the start of the slice while the predicate returns true items: The slice to take elements from predicate: The predicate to test each element against

func Ternary added in v1.0.2

func Ternary[T any](condition bool, trueVal, falseVal T) T

Ternary returns the trueVal if the condition is true, otherwise it returns the falseVal

func ToMap

func ToMap(s any) (map[string]interface{}, error)

ToMap converts a struct to a map[string]interface{}

func Union

func Union[T comparable](items []T, other []T) []T

Union returns the elements that appear in either of two slices items: The first slice to search other: The second slice to search

func Unshift

func Unshift[T any](items []T, item T) []T

Unshift adds an element to the start of an slice items: The slice to add the element to item: The element to add

func Values

func Values[K comparable, V any](m map[K]V) []V

Returns a slice of values from the map

func Zip

func Zip[T any, U any, V any](items []T, other []U, selector func(T, U) V) []V

Zip combines two slices into a new slice using a selector function items: The slice to combine other: The slice to combine selector: The selector function to use

Types

type ConcurrentDictionary

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

ConcurrentDictionary is a thread-safe dictionary utilizes a read-write mutex to allow multiple readers or a single writer

func NewConcurrentDictionary

func NewConcurrentDictionary[T any]() *ConcurrentDictionary[T]

NewConcurrentDictionary creates a new dictionary

func ToConcurrentDictionary

func ToConcurrentDictionary[T any](m map[string]T) *ConcurrentDictionary[T]

ToConcurrentDictionary creates a new dictionary from a map m: The map to create the dictionary from

func (*ConcurrentDictionary[T]) Clear

func (d *ConcurrentDictionary[T]) Clear()

Clear removes all values from the dictionary

func (*ConcurrentDictionary[T]) ContainsKey

func (d *ConcurrentDictionary[T]) ContainsKey(key string) bool

ContainsKey returns if the dictionary contains the given key key: The key to check for

func (*ConcurrentDictionary[T]) ContainsValue

func (d *ConcurrentDictionary[T]) ContainsValue(value T) bool

ContainsValue returns if the dictionary contains the given value value: The value to check for

func (*ConcurrentDictionary[T]) ContainsWhere

func (d *ConcurrentDictionary[T]) ContainsWhere(fn func(string, T) bool) bool

ContainsWhere returns if the dictionary contains a value that satisfies the given predicate fn: The predicate to check for

func (*ConcurrentDictionary[T]) Count

func (d *ConcurrentDictionary[T]) Count() int

Count returns the number of values in the dictionary

func (*ConcurrentDictionary[T]) Get

func (d *ConcurrentDictionary[T]) Get(key string) (T, bool)

Get returns the value in the dictionary for the given key key: The key to get the value for

func (*ConcurrentDictionary[T]) Keys

func (d *ConcurrentDictionary[T]) Keys() []string

Keys returns the keys in the dictionary

func (*ConcurrentDictionary[T]) Merge

func (d *ConcurrentDictionary[T]) Merge(dicts ...*ConcurrentDictionary[T])

Merge merges the given dictionaries into the dictionary dicts: The dictionaries to merge

func (*ConcurrentDictionary[T]) MergeMaps

func (d *ConcurrentDictionary[T]) MergeMaps(maps ...map[string]T)

MergeMaps merges the given maps into the dictionary maps: The maps to merge

func (*ConcurrentDictionary[T]) Remove

func (d *ConcurrentDictionary[T]) Remove(key string)

Remove removes the value in the dictionary for the given key key: The key to remove the value for

func (*ConcurrentDictionary[T]) RemoveValue

func (d *ConcurrentDictionary[T]) RemoveValue(value T)

RemoveValue removes the given value from the dictionary value: The value to remove

func (*ConcurrentDictionary[T]) RemoveWhere

func (d *ConcurrentDictionary[T]) RemoveWhere(fn func(string, T) bool)

RemoveWhere removes the value in the dictionary that satisfies the given predicate fn: The predicate to check for

func (*ConcurrentDictionary[T]) Set

func (d *ConcurrentDictionary[T]) Set(key string, value T)

Set sets the value in the dictionary for the given key key: The key to set the value for value: The value to set

func (*ConcurrentDictionary[T]) ToArray

func (d *ConcurrentDictionary[T]) ToArray() []T

ToArray returns the values in the dictionary as an array

func (*ConcurrentDictionary[T]) ToList

func (d *ConcurrentDictionary[T]) ToList() List[T]

ToList returns the values in the dictionary as a list

func (*ConcurrentDictionary[T]) ToMap

func (d *ConcurrentDictionary[T]) ToMap() map[string]T

ToMap returns the dictionary as a map

type Dictionary

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

Dictionary is a wrapper around a map that provides additional functionality

func NewDictionary

func NewDictionary[T any]() *Dictionary[T]

NewDictionary creates a new dictionary

func NewDictionaryWithCapacity

func NewDictionaryWithCapacity[T any](capacity int) *Dictionary[T]

Add a constructor that takes an initial capacity

func ToDictionary

func ToDictionary[T any](m map[string]T) *Dictionary[T]

ToDictionary creates a new dictionary from a map m: The map to create the dictionary from

func (*Dictionary[T]) Clear

func (d *Dictionary[T]) Clear()

Optimize the Clear method

func (*Dictionary[T]) ContainsKey

func (d *Dictionary[T]) ContainsKey(key string) bool

ContainsKey returns if the dictionary contains the given key key: The key to check for

func (*Dictionary[T]) ContainsValue

func (d *Dictionary[T]) ContainsValue(value T) bool

ContainsValue returns if the dictionary contains the given value value: The value to check for

func (*Dictionary[T]) ContainsWhere

func (d *Dictionary[T]) ContainsWhere(fn func(string, T) bool) bool

ContainsWhere returns if the dictionary contains a value that satisfies the given predicate fn: The predicate to check for

func (*Dictionary[T]) Count

func (d *Dictionary[T]) Count() int

Count returns the number of values in the dictionary

func (*Dictionary[T]) Get

func (d *Dictionary[T]) Get(key string) (T, bool)

Get returns the value in the dictionary for the given key key: The key to get the value for

func (*Dictionary[T]) Keys

func (d *Dictionary[T]) Keys() []string

Keys returns the keys in the dictionary

func (*Dictionary[T]) Merge

func (d *Dictionary[T]) Merge(dicts ...Dictionary[T]) *Dictionary[T]

Modify the Merge method to return the modified dictionary

func (*Dictionary[T]) MergeMaps

func (d *Dictionary[T]) MergeMaps(maps ...map[string]T)

MergeMaps merges the given maps into the dictionary maps: The maps to merge

func (*Dictionary[T]) Remove

func (d *Dictionary[T]) Remove(key string)

Remove removes the value in the dictionary for the given key key: The key to remove the value for

func (*Dictionary[T]) RemoveValue

func (d *Dictionary[T]) RemoveValue(value T)

RemoveValue removes the given value from the dictionary value: The value to remove

func (*Dictionary[T]) RemoveWhere

func (d *Dictionary[T]) RemoveWhere(fn func(string, T) bool)

RemoveWhere removes the value in the dictionary that satisfies the given predicate fn: The predicate to check for

func (*Dictionary[T]) Set

func (d *Dictionary[T]) Set(key string, value T)

Set sets the value in the dictionary for the given key key: The key to set the value for value: The value to set

func (*Dictionary[T]) ToArray

func (d *Dictionary[T]) ToArray() []T

ToArray returns the values in the dictionary as an array

func (*Dictionary[T]) ToList

func (d *Dictionary[T]) ToList() List[T]

ToList returns the values in the dictionary as a list

func (*Dictionary[T]) ToMap

func (d *Dictionary[T]) ToMap() map[string]T

ToMap returns the dictionary as a map

func (*Dictionary[T]) Values

func (d *Dictionary[T]) Values() []T

Add a method to get all values

type List

type List[T any] []T

func NewList

func NewList[T any]() List[T]

NewList returns a new List

func ToList

func ToList[T any](items []T) List[T]

ToList returns a List from an array

func (List[T]) All

func (l List[T]) All(predicate func(T) bool) bool

All determines whether all elements of an array satisfy a condition predicate: The predicate to test each element against

func (List[T]) Any

func (l List[T]) Any(predicate func(T) bool) bool

Any determines whether any element of an array satisfies a condition predicate: The predicate to test each element against

func (List[T]) Chunk

func (l List[T]) Chunk(size int) []List[T]

Chunk returns a new array with elements grouped into chunks of the specified size size: The size of each chunk

func (List[T]) Concat

func (l List[T]) Concat(other List[T]) List[T]

Concat concatenates two lists

func (List[T]) Contains

func (l List[T]) Contains(value T) bool

Contains determines whether an array contains a specific value value: The value to locate in the array

func (List[T]) Count

func (l List[T]) Count(predicate func(T) bool) int

Count returns the number of elements in an array that satisfy a condition predicate: The predicate to test each element against

func (List[T]) Distinct

func (l List[T]) Distinct() List[T]

Distinct returns distinct elements from an array

func (List[T]) Except

func (l List[T]) Except(other []T) List[T]

Except returns the elements of an array that do not appear in a second array other: The array whose elements that also occur in the first array will cause those elements to be removed from the returned array

func (List[T]) Filter

func (l List[T]) Filter(predicate func(T) bool) List[T]

Filter removes all elements from an array that satisfy the predicate predicate: The predicate to test each element against

func (List[T]) Find

func (l List[T]) Find(fn func(T) bool) T

Find returns the first element in the array that satisfies the predicate predicate: The predicate to test each element against

func (List[T]) FindIndex

func (l List[T]) FindIndex(value T) int

FindIndex returns the index of the first occurrence of a value in the array value: The value to locate in the array

func (List[T]) FindIndexWhere

func (l List[T]) FindIndexWhere(predicate func(T) bool) int

FindIndexWhere returns the index of the first element in the array that satisfies the predicate predicate: The predicate to test each element against

func (List[T]) FindLast

func (l List[T]) FindLast(val T) T

FindLast returns the last element in the array that satisfies the predicate predicate: The predicate to test each element against

func (List[T]) FindLastIndex

func (l List[T]) FindLastIndex(value T) int

FindLastIndex returns the index of the last occurrence of a value in the array value: The value to locate in the array

func (List[T]) FindLastIndexWhere

func (l List[T]) FindLastIndexWhere(predicate func(T) bool) int

FindLastIndexWhere returns the index of the last element in the array that satisfies the predicate predicate: The predicate to test each element against

func (List[T]) FindLastWhere

func (l List[T]) FindLastWhere(predicate func(T) bool) T

FindLastWhere returns the last element in the array that satisfies the predicate predicate: The predicate to test each element against

func (List[T]) First

func (l List[T]) First() T

First returns the first element in the array

func (List[T]) ForEach

func (l List[T]) ForEach(fn func(T)) List[T]

ForEach executes an action for each element in the array in parallel action: The action to perform on each element

func (List[T]) Intersect

func (l List[T]) Intersect(other []T) List[T]

Intersect returns the elements that appear in two arrays other: The array whose distinct elements that also appear in the first array will be returned

func (List[T]) Last

func (l List[T]) Last() T

Last returns the last element in the array

func (List[T]) Length

func (l List[T]) Length() int

Length returns the length of the array

func (List[T]) Partition

func (l List[T]) Partition(predicate func(T) bool) (List[T], List[T])

Partition splits an array into two arrays based on a predicate

func (List[T]) Pop

func (l List[T]) Pop() (T, List[T])

Pop removes the last element from an array and returns it

func (List[T]) Push

func (l List[T]) Push(value T) List[T]

Push adds an element to the end of the array value: The value to push

func (List[T]) Randomize

func (l List[T]) Randomize() List[T]

Randomize returns a new array with the elements in a random order

func (List[T]) Reduce

func (l List[T]) Reduce(accumulator func(T, T) T, initialValue ...T) T

Reduce applies an accumulator function over an array accumulator: The accumulator function to use

func (List[T]) Remove

func (l List[T]) Remove(value T) List[T]

Remove removes the first occurrence of a specific object from an array value: The value to remove from the array

func (List[T]) RemoveAt

func (l List[T]) RemoveAt(index int) List[T]

RemoveAt removes an element from an array at the specified index index: The index to remove at

func (List[T]) RemoveWhere

func (l List[T]) RemoveWhere(predicate func(T) bool) List[T]

RemoveWhere removes the first element in the array that satisfies the predicate predicate: The predicate to test each element against

func (List[T]) Replace

func (l List[T]) Replace(oldValue T, newValue T) List[T]

Replace replaces the first occurrence of a value in an array with another value oldValue: The value to replace newValue: The value to replace with

func (List[T]) ReplaceAll

func (l List[T]) ReplaceAll(oldValue T, newValue T) List[T]

ReplaceAll replaces all occurrences of a value in an array with another value oldValue: The value to replace newValue: The value to replace with

func (List[T]) ReplaceAllWhere

func (l List[T]) ReplaceAllWhere(value T, predicate func(T) bool) List[T]

ReplaceAllWhere replaces all occurrences of a value in an array with the result of the selector function value: The value to replace with predicate: The selector function to use

func (List[T]) ReplaceWhere

func (l List[T]) ReplaceWhere(value T, predicate func(T) bool) List[T]

ReplaceWhere replaces the first occurrence of a value in an array with the result of the selector function value: The value to replace with predicate: The selector function to use

func (List[T]) Reverse

func (l List[T]) Reverse() List[T]

Reverse reverses the order of the elements in the array

func (List[T]) SequenceEqual

func (l List[T]) SequenceEqual(other []T) bool

SequenceEqual determines whether two arrays are equal other: The second array to compare

func (List[T]) Shift

func (l List[T]) Shift() (T, List[T])

Shift removes the first element from an array and returns it

func (List[T]) Skip

func (l List[T]) Skip(count int) List[T]

Skip returns a new array with the specified number of elements removed from the start of the array count: The number of elements to skip

func (List[T]) SkipLast

func (l List[T]) SkipLast(count int) List[T]

SkipLast returns a new array with the specified number of elements removed from the end of the array count: The number of elements to skip

func (List[T]) SkipWhile

func (l List[T]) SkipWhile(predicate func(T) bool) List[T]

SkipWhile returns a new array with elements removed from the start of the array while the predicate returns true predicate: The predicate to test each element against

func (List[T]) Slice

func (l List[T]) Slice(start int, end int) List[T]

Slice returns a new array with the elements from the specified start index to the specified end index start: The index to start at end: The index to end at

func (List[T]) SortWhere

func (l List[T]) SortWhere(comparer func(T, T) bool) List[T]

SortWhere sorts the elements of an array in place using the specified comparer function comparer: The comparer function to use

func (List[T]) Take

func (l List[T]) Take(count int) List[T]

Take returns a new array with the specified number of elements from the start of the array count: The number of elements to take

func (List[T]) TakeLast

func (l List[T]) TakeLast(count int) List[T]

TakeLast returns a new array with the specified number of elements from the end of the array count: The number of elements to take

func (List[T]) TakeWhile

func (l List[T]) TakeWhile(predicate func(T) bool) List[T]

TakeWhile returns a new array with elements from the start of the array while the predicate returns true predicate: The predicate to test each element against

func (List[T]) Union

func (l List[T]) Union(other []T) List[T]

Union returns the elements that appear in either of two arrays other: The second array to search

func (List[T]) Unshift

func (l List[T]) Unshift(value T) List[T]

Unshift adds an element to the start of the array value: The value to unshift

func (List[T]) Zip

func (l List[T]) Zip(other []T, selector func(T, T) T) List[T]

Zip combines two arrays into a new array using a selector function

type MapEntry

type MapEntry[K comparable, V any] struct {
	Key   K
	Value V
}

MapEntry is a key-value pair of a map

func Entries

func Entries[K comparable, V any](m map[K]V) []MapEntry[K, V]

Entries Returns a slice of key-value pairs from the map

type Queue

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

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue creates a new queue

func ToQueue

func ToQueue[T any](items []T) *Queue[T]

ToQueue creates a new queue from an array items: The array to create the queue from

func (*Queue[T]) Clear

func (q *Queue[T]) Clear()

Clear removes all items from the queue

func (*Queue[T]) Contains

func (q *Queue[T]) Contains(item T) bool

Contains returns if the queue contains the given item item: The item to check for

func (*Queue[T]) ContainsWhere

func (q *Queue[T]) ContainsWhere(fn func(T) bool) bool

ContainsWhere returns if the queue contains an item that satisfies the given predicate fn: The predicate to check for

func (*Queue[T]) Count

func (q *Queue[T]) Count() int

Count returns the number of items in the queue

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() T

Dequeue removes and returns the item at the beginning of the queue

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(item T)

Enqueue adds an item to the end of the queue item: The item to add

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() T

Peek returns an element that is at the beginning of the queue without removing it

func (*Queue[T]) ToArray

func (q *Queue[T]) ToArray() []T

ToArray returns the items in the queue as an array

func (*Queue[T]) ToList

func (q *Queue[T]) ToList() List[T]

ToList returns the items in the queue as a list

type Stack

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

func FromSlice

func FromSlice[T any](items []T) *Stack[T]

Rename ToStack to FromSlice for consistency

func NewStack

func NewStack[T any]() *Stack[T]

NewStack creates a new stack

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Clear removes all items from the stack

func (*Stack[T]) Contains

func (s *Stack[T]) Contains(item T) bool

Contains returns if the stack contains the given item item: The item to check for

func (*Stack[T]) ContainsWhere

func (s *Stack[T]) ContainsWhere(fn func(T) bool) bool

ContainsWhere returns if the stack contains an item that satisfies the given predicate fn: The predicate to check for

func (*Stack[T]) Count

func (s *Stack[T]) Count() int

Count returns the number of items in the stack

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, error)

Peek returns an element that is at the top of the stack without removing it

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, error)

Pop removes and returns the item on the top of the stack

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Push pushes an item onto the stack item: The item to push

func (*Stack[T]) ToArray

func (s *Stack[T]) ToArray() []T

ToArray returns the items in the stack as an array

func (*Stack[T]) ToList

func (s *Stack[T]) ToList() List[T]

ToList returns the items in the stack as a list

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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