list

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package list provides utility functions for working with slices and lists in Go.

Package list provides utility functions for working with slices and lists in Go.

Package list provides utility functions for working with slices and lists in Go.

Package list provides utility functions for working with slices and lists in Go.

Package list provides utility functions for working with slices and lists in Go.

Package list provides utility functions for working with slices and lists in Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T any](item T, list []T, compare func(T, T) bool) bool

Contains checks if an item exists in a slice based on a custom comparison function.

Parameters:

  • item: The value to search for in the slice.
  • list: The slice of items to search within.
  • compare: A comparison function that takes two items of type T and returns true if they are considered equal, false otherwise.

Returns:

  • true if the item is found in the slice according to the comparison function, false otherwise.

Example:

items := []string{"apple", "banana", "cherry"}
exists := Contains("BANANA", items, func(a, b string) bool {
    return strings.ToLower(a) == strings.ToLower(b)
}) // true

func ContainsComparable

func ContainsComparable[T comparable](item T, list []T) bool

ContainsComparable checks if a comparable item exists in a slice.

Parameters:

  • item: The value to search for in the slice.
  • list: The slice of comparable items to search within.

Returns:

  • true if the item is found in the slice, false otherwise.

Example:

items := []int{1, 2, 3}
exists := ContainsComparable(2, items) // true

func FindOne

func FindOne[T any](list []T, f func(T) bool) optional.Optional[T]

FindOne searches a slice for the first element that satisfies a given predicate function.

Parameters:

  • list: The slice of items to search within.
  • f: A predicate function that takes an item of type T and returns true if the item satisfies the condition.

Returns:

  • An optional value containing the first element that satisfies the predicate function, or an empty optional if no such element is found.

Example:

numbers := []int{1, 2, 3, 4, 5}
firstEven := FindOne(numbers, func(n int) bool { return n%2 == 0 }) // optional.Of(2)

strings := []string{"apple", "banana", "cherry"}
firstBanana := FindOne(strings, func(s string) bool { return s == "banana" }) // optional.Of("banana")

noMatch := FindOne(numbers, func(n int) bool { return n > 10 }) // optional.Empty[int]()

func InsertOneBefore

func InsertOneBefore[T any](before T, input []T) []T

InsertOneBefore inserts a single element at the beginning of a slice.

Parameters:

  • before: The element to insert at the beginning of the slice.
  • input: The original slice where the element will be inserted.

Returns:

  • A new slice with the `before` element added at the start, followed by all elements of the original slice.

Example:

original := []int{2, 3, 4}
withOne := InsertOneBefore(1, original) // []int{1, 2, 3, 4}

strings := []string{"banana", "cherry"}
withApple := InsertOneBefore("apple", strings) // []string{"apple", "banana", "cherry"}

func Map

func Map[T, U any](input []T, mapperFunc func(T) U) []U

Map transforms a slice of type T into a slice of type U by applying a mapping function to each element.

Parameters:

  • input: The slice of type T to be transformed.
  • mapperFunc: A function that takes an element of type T and returns an element of type U.

Returns:

  • A new slice of type U where each element is the result of applying `mapperFunc` to the corresponding element of `input`.

Example:

numbers := []int{1, 2, 3}
squared := Map(numbers, func(n int) int { return n * n }) // []int{1, 4, 9}

strings := []string{"apple", "banana", "cherry"}
lengths := Map(strings, func(s string) int { return len(s) }) // []int{5, 6, 6}

func MatchAny

func MatchAny[T, U any](list []T, u U, matchFunc func(U, T) bool) bool

MatchAny checks if any element in a slice matches a given value using a custom matching function.

Parameters:

  • list: The slice of type T to search through.
  • u: The value of type U to match against elements in the slice.
  • matchFunc: A function that takes a value of type U and an element of type T, and returns true if they match, false otherwise.

Returns:

  • true if at least one element in the slice matches the value according to the `matchFunc`, false otherwise.

Example:

numbers := []int{1, 2, 3, 4, 5}
isEvenPresent := MatchAny(numbers, true, func(isEven bool, n int) bool {
    return isEven && n%2 == 0
}) // true

strings := []string{"apple", "banana", "cherry"}
containsCherry := MatchAny(strings, "cherry", func(search string, s string) bool {
    return search == s
}) // true

func Split

func Split[T SplitTable[U, V], U, V any](input []T) ([]U, []V)

Split takes a slice of elements that implement the SplitTable interface and splits them into two separate slices.

Parameters:

  • input: A slice of elements of type T, where T implements SplitTable[U, V].

Returns:

  • Two slices:
  • The first slice contains all elements of type U extracted from the input.
  • The second slice contains all elements of type V extracted from the input.

Example:

type Pair struct {
    Key   string
    Value int
}

func (p Pair) Split() (string, int) {
    return p.Key, p.Value
}

pairs := []Pair{
    {"apple", 5},
    {"banana", 3},
    {"cherry", 7},
}
keys, values := Split(pairs) // keys: []string{"apple", "banana", "cherry"}, values: []int{5, 3, 7}

Types

type SplitTable

type SplitTable[U, V any] interface {
	Split() (U, V)
}

SplitTable is an interface representing a type that can be split into two components of types U and V.

Example:

type Pair struct {
    First  string
    Second int
}

func (p Pair) Split() (string, int) {
    return p.First, p.Second
}

Pairs implementing SplitTable[string, int] can be split into slices of strings and integers using the Split function.

Jump to

Keyboard shortcuts

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