lo

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2022 License: MIT Imports: 4 Imported by: 0

README ΒΆ

lo

Build Status GoDoc Go report

✨ lo is a Lodash-style Go library based on Go 1.18+ Generics.

This project have started as an experiment to discover generics implementation. It may look like Lodash in some aspects. I used to code with the awesome go-funk package, but it uses reflection and therefore is not typesafe.

As expected, benchmarks demonstrate that generics will be much faster than implementations based on reflect stdlib package. Benchmarks also shows similar performances to pure for loops. See below.

In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package names slices and maps). Anyway I feel this library legitimate to offer many more such useful abstractions.

Why this name?

I wanted a short name, similar to "Lodash", and no Go package currently use this name.

πŸš€ Install

go get github.com/samber/lo

πŸ’‘ Usage

You can import lo using a basic statement:

import (
    "github.com/samber/lo"
    lop "github.com/samber/lo/parallel"
)

Then use one of the helpers below:

names := lo.Uniq[string]([]string{"Samuel", "Marc", "Samuel"})
// []string{"Samuel", "Marc"}

🀠 Spec

GoDoc: https://godoc.org/github.com/samber/lo

Supported helpers for slices:

  • Filter
  • Map
  • Reduce
  • ForEach
  • Times
  • Uniq
  • UniqBy
  • GroupBy
  • Chunk
  • PartitionBy
  • Flatten
  • Shuffle
  • Reverse
  • Fill
  • Repeat
  • ToMap

Supported helpers for maps:

  • Keys
  • Values
  • Entries
  • FromEntries
  • Assign (merge of maps)

Supported helpers for tuples:

  • Zip2 -> Zip9
  • Unzip2 -> Unzip9

Supported intersection helpers:

  • Contains
  • Every
  • Some
  • Intersect
  • Difference

Supported search helpers:

  • IndexOf
  • LastIndexOf
  • Find
  • Min
  • Max
  • Last
  • Nth
  • Sample
  • Samples

Other functional programming helpers:

  • Ternary (1 line if/else statement)
  • If / ElseIf / Else
  • Switch / Case / Default
  • ToPtr
  • ToSlicePtr
  • Attempt

Constraints:

  • Clonable
Map

Manipulates a slice and transforms it to a slice of another type:

import "github.com/samber/lo"

lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
    return strconv.FormatInt(x, 10)
})
// []string{"1", "2", "3", "4"}

Parallel processing: like lo.Map(), but mapper is called in goroutine. Results are returned in the same order.

import lop "github.com/samber/lo/parallel"

lop.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
    return strconv.FormatInt(x, 10)
}, 2)
// []string{"1", "2", "3", "4"}
Filter

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

even := lo.Filter[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool {
    return x%2 == 0
})
// []int{2, 4}
Contains

Returns true if an element is present in a collection.

present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5)
// true
Reduce

Reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous.

sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
    return agg + item
}, 0)
// 10
ForEach

Iterates over elements of collection and invokes iteratee for each element.

import "github.com/samber/lo"

lo.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) {
    println(x)
})
// prints "hello\nworld\n"

Parallel processing: like lo.ForEach(), but callback is called in goroutine.

import lop "github.com/samber/lo/parallel"

lop.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) {
    println(x)
})
// prints "hello\nworld\n" or "world\nhello\n"
Times

Times invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with index as argument.

import "github.com/samber/lo"

lo.Times[string](3, func(i int) string {
    return strconv.FormatInt(int64(i), 10)
})
// []string{"0", "1", "2"}

Parallel processing: like lo.Times(), but callback is called in goroutine.

import lop "github.com/samber/lo/parallel"

lop.Times[string](3, func(i int) string {
    return strconv.FormatInt(int64(i), 10)
})
// []string{"0", "1", "2"}
Uniq

Returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.

uniqValues := lo.Uniq[int]([]int{1, 2, 2, 1})
// []int{1, 2}
UniqBy

Returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array. It accepts iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed.

uniqValues := lo.UniqBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
    return i%3
})
// []int{0, 1, 2}
GroupBy

Returns an object composed of keys generated from the results of running each element of collection through iteratee.

import lo "github.com/samber/lo"

groups := lo.GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
    return i%3
})
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}

Parallel processing: like lo.GroupBy(), but callback is called in goroutine.

import lop "github.com/samber/lo/parallel"

lop.GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
    return i%3
})
// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
Chunk

Returns an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5}, 2)
// [][]int{{0, 1}, {2, 3}, {4, 5}}

lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5, 6}, 2)
// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}

lo.Chunk[int]([]int{}, 2)
// [][]int{}

lo.Chunk[int]([]int{0}, 2)
// [][]int{{0}}
PartitionBy

Returns an array of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee.

import lo "github.com/samber/lo"

partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
    if x < 0 {
        return "negative"
    } else if x%2 == 0 {
        return "even"
    }
    return "odd"
})
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}

Parallel processing: like lo.PartitionBy(), but callback is called in goroutine. Results are returned in the same order.

import lop "github.com/samber/lo/parallel"

partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
    if x < 0 {
        return "negative"
    } else if x%2 == 0 {
        return "even"
    }
    return "odd"
})
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
Flatten

Returns an array a single level deep.

flat := lo.Flatten[int]([][]int{{0, 1}, {2, 3, 4, 5}})
// []int{0, 1, 2, 3, 4, 5}
Shuffle

Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.

randomOrder := lo.Shuffle[int]([]int{0, 1, 2, 3, 4, 5})
// []int{0, 1, 2, 3, 4, 5}
Reverse

Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.

reverseOder := lo.Reverse[int]([]int{0, 1, 2, 3, 4, 5})
// []int{5, 4, 3, 2, 1, 0}
Fill

Fills elements of array with initial value.

type foo struct {
	bar string
}

func (f foo) Clone() foo {
	return foo{f.bar}
}

initializedSlice := lo.Fill[foo]([]foo{foo{"a"}, foo{"a"}}, foo{"b"})
// []foo{foo{"b"}, foo{"b"}}
Repeat

Builds a slice with N copies of initial value.

type foo struct {
	bar string
}

func (f foo) Clone() foo {
	return foo{f.bar}
}

initializedSlice := lo.Repeat[foo](2, foo{"a"})
// []foo{foo{"a"}, foo{"a"}}
ToMap

Transforms a slice or an array of structs to a map based on a pivot callback.

m := lo.ToMap[int, string]([]string{"a", "aa", "aaa"}, func(str string) int {
    return len(str)
})
// map[int]string{1: "a", 2: "aa", 3: "aaa"}
Keys

Creates an array of the map keys.

keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2})
// []string{"bar", "foo"}
Values

Creates an array of the map values.

values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2})
// []int{1, 2}
Entries

Transforms a map into array of key/value pairs.

entries := lo.Entries[string, int](map[string]int{"foo": 1, "bar": 2})
// []lo.Entry[string, int]{
//     {
//         Key: "foo",
//         Value: 1,
//     },
//     {
//         Key: "bar",
//         Value: 2,
//     },
// }
FromEntries

Transforms an array of key/value pairs into a map.

m := lo.FromEntries[string, int]([]lo.Entry[string, int]{
    {
        Key: "foo",
        Value: 1,
    },
    {
        Key: "bar",
        Value: 2,
    },
})
// map[string]int{"foo": 1, "bar": 2}
Assign

Merges multiple maps from left to right.

mergedMaps := lo.Assign[string, int](
    map[string]int{"a": 1, "b": 2},
    map[string]int{"b": 3, "c": 4},
)
// map[string]int{"a": 1, "b": 3, "c": 4}
Zip2 -> Zip9

Zip creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.

When collections have different size, the Tuple attributes are filled with zero value.

tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2})
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
Unzip2 -> Unzip9

Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
// []string{"a", "b"}
// []int{1, 2}
Every

Returns true if all elements of a subset are contained into a collection.

ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// true

ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
// false
Some

Returns true if at least 1 element of a subset is contained into a collection.

ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// true

ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// false
Intersect

Returns the intersection between two collections.

result1 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// []int{0, 2}

result2 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6}
// []int{0}

result3 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// []int{}
Difference

Returns the difference between two collections.

  • The first value is the collection of element absent of list2.
  • The second value is the collection of element absent of list1.
left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6})
// []int{1, 3, 4, 5}, []int{6}

left, right := Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
// []int{}, []int{}
IndexOf

Returns the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.

found := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
// 2

notFound := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
// -1
LastIndex

Returns the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found.

found := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
// 4

notFound := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
// -1
Find

Search an element in a slice based on a predicate. It returns element and true if element was found.

str, ok := lo.Find[string]([]string{"a", "b", "c", "d"}, func(i string) bool {
    return i == "b"
})
// "b", true

str, ok := lo.Find[string]([]string{"foobar"}, func(i string) bool {
    return i == "b"
})
// "", false
Min

Search the minimum value of a collection.

min := lo.Min[int]([]int{1, 2, 3})
// 1

min := lo.Min[int]([]int{})
// 0
Max

Search the maximum value of a collection.

max := lo.Max[int]([]int{1, 2, 3})
// 3

max := lo.Max[int]([]int{})
// 0
Last

Returns the last element of a collection or error if empty.

last, err := lo.Last[int]([]int{1, 2, 3})
// 3
Nth

Returns the element at index nth of collection. If nth is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.

nth, err := lo.Nth[int]([]int{0, 1, 2, 3}, 2)
// 2

nth, err := lo.Nth[int]([]int{0, 1, 2, 3}, -2)
// 2
Sample

Returns a random item from collection.

lo.Sample[string]([]string{"a", "b", "c"})
// a random string from []string{"a", "b", "c"}

lo.Sample[string]([]string{})
// ""
Samples

Returns N random unique items from collection.

lo.Samples[string]([]string{"a", "b", "c"}, 3)
// []string{"a", "b", "c"} in random order
Ternary

A 1 line if/else statement.

result := lo.Ternary[string](true, "a", "b")
// "a"

result := lo.Ternary[string](false, "a", "b")
// "b"
If / ElseIf / Else
result := lo.If[int](true, 1).
    ElseIf(false, 2).
    Else(3)
// 1

result := lo.If[int](false, 1).
    ElseIf(true, 2).
    Else(3)
// 2

result := lo.If[int](false, 1).
    ElseIf(false, 2).
    Else(3)
// 3
Switch / Case / Default
result := lo.Switch[int, string](1).
    Case(1, "1").
    Case(2, "2").
    Default("3")
// "1"

result := lo.Switch[int, string](2).
    Case(1, "1").
    Case(2, "2").
    Default("3")
// "2"

result := lo.Switch[int, string](42).
    Case(1, "1").
    Case(2, "2").
    Default("3")
// "3"

Using callbacks:

result := lo.Switch[int, string](1).
    CaseF(1, func() string {
        return "1"
    }).
    CaseF(2, func() string {
        return "2"
    }).
    DefaultF(func() string {
        return "3"
    })
// "1"
ToPtr

Returns a pointer copy of value.

ptr := lo.ToPtr[string]("hello world")
// *string{"hello world"}
ToSlicePtr

Returns a slice of pointer copy of value.

ptr := lo.ToSlicePtr[string]([]string{"hello", "world"})
// []*string{"hello", "world"}
Attempt

Invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than 1, the function runs until a sucessfull response is returned.

iter, err := lo.Attempt(42, func(i int) error {
    if i == 5 {
        return nil
    }

    return fmt.Errorf("failed")
})
// 6
// nil

iter, err := lo.Attempt(2, func(i int) error {
    if i == 5 {
        return nil
    }

    return fmt.Errorf("failed")
})
// 2
// error "failed"

iter, err := lo.Attempt(0, func(i int) error {
    if i < 42 {
        return fmt.Errorf("failed")
    }

    return nil
})
// 43
// nil

For more advanced retry strategies (delay, exponential backoff...), please take a look on cenkalti/backoff.

πŸ›© Benchmark

We executed a simple benchmark with the a dead-simple lo.Map loop:

See the full implementation here.

_ = lo.Map[int64](arr, func(x int64, i int) string {
    return strconv.FormatInt(x, 10)
})

Result:

Here is a comparison between lo.Map, lop.Map, go-funk library and a simple Go for loop.

$ go test -benchmem -bench ./...
goos: linux
goarch: amd64
pkg: github.com/samber/lo
cpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
cpu: Intel(R) Core(TM) i7 CPU         920  @ 2.67GHz
BenchmarkMap/lo.Map-8         	       8	 132728237 ns/op	39998945 B/op	 1000002 allocs/op
BenchmarkMap/lop.Map-8        	       2	 503947830 ns/op	119999956 B/op	 3000007 allocs/op
BenchmarkMap/reflect-8        	       2	 826400560 ns/op	170326512 B/op	 4000042 allocs/op
BenchmarkMap/for-8            	       9	 126252954 ns/op	39998674 B/op	 1000001 allocs/op
PASS
ok  	github.com/samber/lo	6.657s
  • lo.Map is way faster (x7) than go-funk, a relection-based Map implementation.
  • lo.Map have the same allocation profile than for.
  • lo.Map is 4% slower than for.
  • lop.Map is slower than lo.Map because it implies more memory allocation and locks. lop.Map will be usefull for long-running callbacks, such as i/o bound processing.
  • for beats other implementations for memory and CPU.

🀝 Contributing

Don't hesitate ;)

Install go 1.18
make go1.18beta1

If your OS currently not default to Go 1.18, replace BIN=go by BIN=go1.18beta1 in the Makefile.

With Docker
docker-compose run --rm dev
Without Docker
# Install some dev dependencies
make tools

# Run tests
make test
# or
make watch-test

πŸ‘€ Authors

  • Samuel Berthe

πŸ’« Show your support

Give a ⭐️ if this project helped you!

support us

πŸ“ License

Copyright Β© 2022 Samuel Berthe.

This project is MIT licensed.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Assign ΒΆ

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

Assign merges multiple maps from left to right.

func Attempt ΒΆ

func Attempt(maxIteration int, f func(int) error) (int, error)

Attempt invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned.

func Chunk ΒΆ

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

Chunk returns an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

func Contains ΒΆ

func Contains[T comparable](collection []T, element T) bool

Contains returns true if an element is present in a collection.

func Difference ΒΆ

func Difference[T comparable](list1 []T, list2 []T) ([]T, []T)

Difference returns the difference between two collections. The first value is the collection of element absent of list2. The second value is the collection of element absent of list1.

func Empty ΒΆ

func Empty[T any]() T

Empty returns an empty value.

func Every ΒΆ

func Every[T comparable](collection []T, subset []T) bool

Every returns true if all elements of a subset are contained into a collection.

func Fill ΒΆ

func Fill[T Clonable[T]](collection []T, initial T) []T

Fill fills elements of array with `initial` value.

func Filter ΒΆ

func Filter[V any](collection []V, predicate func(V, int) bool) []V

Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.

func Find ΒΆ

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

Find search an element in a slice based on a predicate. It returns element and true if element was found.

func FlatMap ΒΆ

func FlatMap[T any, R any](collection []T, iteratee func(T, int) []R) []R

FlatMap manipulates a slice and transforms and flattens it to a slice of another type.

func Flatten ΒΆ

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

Flattens returns an array a single level deep.

func ForEach ΒΆ

func ForEach[T any](collection []T, iteratee func(T, int))

ForEach iterates over elements of collection and invokes iteratee for each element.

func FromEntries ΒΆ

func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V

FromEntries transforms an array of key/value pairs into a map.

func GroupBy ΒΆ

func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T

GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.

func If ΒΆ

func If[T any](condition bool, result T) *ifElse[T]

If.

func IndexOf ΒΆ

func IndexOf[T comparable](collection []T, element T) int

IndexOf returns the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.

func Intersect ΒΆ

func Intersect[T comparable](list1 []T, list2 []T) []T

Intersect returns the intersection between two collections.

func Keys ΒΆ

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

Keys creates an array of the map keys.

func Last ΒΆ

func Last[T any](collection []T) (T, error)

Last returns the last element of a collection or error if empty.

func LastIndexOf ΒΆ

func LastIndexOf[T comparable](collection []T, element T) int

IndexOf returns the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found.

func Map ΒΆ

func Map[T any, R any](collection []T, iteratee func(T, int) R) []R

Map manipulates a slice and transforms it to a slice of another type.

func MapValues ΒΆ

func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) map[K]R

MapValues manipulates a map values and transforms it to a map of another type.

func Max ΒΆ

func Max[T constraints.Ordered](collection []T) T

Max search the maximum value of a collection.

func Min ΒΆ

func Min[T constraints.Ordered](collection []T) T

Min search the minimum value of a collection.

func Nth ΒΆ

func Nth[T any](collection []T, nth int) (T, error)

Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.

func PartitionBy ΒΆ

func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T

PartitionBy returns an array of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee.

func Reduce ΒΆ

func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R

Reduce reduces collection to a value which is the accumulated result of running each element in collection through accumulator, where each successive invocation is supplied the return value of the previous.

func Repeat ΒΆ

func Repeat[T Clonable[T]](count int, initial T) []T

Repeat builds a slice with N copies of initial value.

func Reverse ΒΆ

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

Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.

func Sample ΒΆ

func Sample[T any](collection []T) T

Sample returns a random item from collection.

func Samples ΒΆ

func Samples[T any](collection []T, count int) []T

Samples returns N random unique items from collection.

func Shuffle ΒΆ

func Shuffle[T any](collection []T) []T

Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.

func Some ΒΆ

func Some[T comparable](collection []T, subset []T) bool

Some returns true if at least 1 element of a subset is contained into a collection.

func Switch ΒΆ

func Switch[T comparable, R any](predicate T) *switchCase[T, R]

Switch is a pure functional switch/case/default statement.

func Ternary ΒΆ

func Ternary[T any](condition bool, ifOutput T, elseOutput T) T

Ternary is a 1 line if/else statement.

func Times ΒΆ

func Times[T any](count int, iteratee func(int) T) []T

Times invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with index as argument.

func ToMap ΒΆ

func ToMap[K comparable, V any](collection []V, iteratee func(V) K) map[K]V

ToMap transforms a slice or an array of structs to a map based on a pivot callback.

func ToPtr ΒΆ

func ToPtr[T any](x T) *T

ToPtr returns a pointer copy of value.

func ToSlicePtr ΒΆ

func ToSlicePtr[T any](collection []T) []*T

ToPtr returns a slice of pointer copy of value.

func Uniq ΒΆ

func Uniq[T comparable](collection []T) []T

Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.

func UniqBy ΒΆ

func UniqBy[T any, U comparable](collection []T, iteratee func(T) U) []T

Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.

func Unzip2 ΒΆ

func Unzip2[A any, B any](tuples []Tuple2[A, B]) ([]A, []B)

Unzip2 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip3 ΒΆ

func Unzip3[A any, B any, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C)

Unzip3 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip4 ΒΆ

func Unzip4[A any, B any, C any, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D)

Unzip4 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip5 ΒΆ

func Unzip5[A any, B any, C any, D any, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E)

Unzip5 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip6 ΒΆ

func Unzip6[A any, B any, C any, D any, E any, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F)

Unzip6 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip7 ΒΆ

func Unzip7[A any, B any, C any, D any, E any, F any, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G)

Unzip7 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip8 ΒΆ

func Unzip8[A any, B any, C any, D any, E any, F any, G any, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H)

Unzip8 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Unzip9 ΒΆ

func Unzip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)

Unzip9 accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.

func Values ΒΆ

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

Values creates an array of the map values.

Types ΒΆ

type Clonable ΒΆ

type Clonable[T any] interface {
	Clone() T
}

Clonable defines a constraint of types having Clone() T method.

type Entry ΒΆ

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

Entry defines a key/value pairs.

func Entries ΒΆ

func Entries[K comparable, V any](in map[K]V) []Entry[K, V]

Entries transforms a map into array of key/value pairs.

type Tuple2 ΒΆ

type Tuple2[A any, B any] struct {
	A A
	B B
}

Tuple2 is a group of 2 elements (pair).

func Zip2 ΒΆ

func Zip2[A any, B any](a []A, b []B) []Tuple2[A, B]

Zip2 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple3 ΒΆ

type Tuple3[A any, B any, C any] struct {
	A A
	B B
	C C
}

Tuple3 is a group of 3 elements.

func Zip3 ΒΆ

func Zip3[A any, B any, C any](a []A, b []B, c []C) []Tuple3[A, B, C]

Zip3 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple4 ΒΆ

type Tuple4[A any, B any, C any, D any] struct {
	A A
	B B
	C C
	D D
}

Tuple4 is a group of 4 elements.

func Zip4 ΒΆ

func Zip4[A any, B any, C any, D any](a []A, b []B, c []C, d []D) []Tuple4[A, B, C, D]

Zip4 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple5 ΒΆ

type Tuple5[A any, B any, C any, D any, E any] struct {
	A A
	B B
	C C
	D D
	E E
}

Tuple5 is a group of 5 elements.

func Zip5 ΒΆ

func Zip5[A any, B any, C any, D any, E any](a []A, b []B, c []C, d []D, e []E) []Tuple5[A, B, C, D, E]

Zip5 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple6 ΒΆ

type Tuple6[A any, B any, C any, D any, E any, F any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
}

Tuple6 is a group of 6 elements.

func Zip6 ΒΆ

func Zip6[A any, B any, C any, D any, E any, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F]

Zip6 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple7 ΒΆ

type Tuple7[A any, B any, C any, D any, E any, F any, G any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
}

Tuple7 is a group of 7 elements.

func Zip7 ΒΆ

func Zip7[A any, B any, C any, D any, E any, F any, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G]

Zip7 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple8 ΒΆ

type Tuple8[A any, B any, C any, D any, E any, F any, G any, H any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
	H H
}

Tuple8 is a group of 8 elements.

func Zip8 ΒΆ

func Zip8[A any, B any, C any, D any, E any, F any, G any, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H]

Zip8 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

type Tuple9 ΒΆ

type Tuple9[A any, B any, C any, D any, E any, F any, G any, H any, I any] struct {
	A A
	B B
	C C
	D D
	E E
	F F
	G G
	H H
	I I
}

Tuple9 is a group of 9 elements.

func Zip9 ΒΆ

func Zip9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I) []Tuple9[A, B, C, D, E, F, G, H, I]

Zip9 creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. When collections have different size, the Tuple attributes are filled with zero value.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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