lo

package module
v0.0.0-...-c0b717a Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 8 Imported by: 0

README ΒΆ

lo

tag Go Version GoDoc Build Status Go report Coverage Contributors License

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

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

As expected, benchmarks demonstrate that generics are much faster than implementations based on the "reflect" package. Benchmarks also show similar performance gains compared 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). I feel this library is legitimate and offers many more valuable abstractions.

See also:

  • samber/do: A dependency injection toolkit based on Go 1.18+ Generics
  • samber/mo: Monads based on Go 1.18+ Generics (Option, Result, Either...)

Why this name?

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

πŸš€ Install

go get github.com/samber/lo@v1

This library is v1 and follows SemVer strictly.

No breaking changes will be made to exported APIs before v2.0.0.

πŸ’‘ Usage

You can import lo using:

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

Then use one of the helpers below:

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

Most of the time, the compiler will be able to infer the type so that you can call: lo.Uniq([]string{...}).

Tips for lazy developers

I cannot recommend it, but in case you are too lazy for repeating lo. everywhere, you can import the entire library into the namespace.

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

I take no responsibility on this junk. 😁 πŸ’©

🀠 Spec

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

Supported helpers for slices:

Supported helpers for maps:

Supported math helpers:

Supported helpers for strings:

Supported helpers for tuples:

Supported helpers for channels:

Supported intersection helpers:

Supported search helpers:

Conditional helpers:

Type manipulation helpers:

Function helpers:

Concurrency helpers:

Error handling:

Constraints:

  • Clonable
Filter

Iterates over a collection and returns an array of all the elements the predicate function returns true for.

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

[play]

Map

Manipulates a slice of one type and transforms it into a slice of another type:

import "github.com/samber/lo"

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

[play]

Parallel processing: like lo.Map(), but the mapper function is called in a 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)
})
// []string{"1", "2", "3", "4"}
FilterMap

Returns a slice which obtained after both filtering and mapping using the given callback function.

The callback function should return two values: the result of the mapping operation and whether the result element should be included or not.

matching := lo.FilterMap[string, string]([]string{"cpu", "gpu", "mouse", "keyboard"}, func(x string, _ int) (string, bool) {
    if strings.HasSuffix(x, "pu") {
        return "xpu", true
    }
    return "", false
})
// []string{"xpu", "xpu"}

[play]

FlatMap

Manipulates a slice and transforms and flattens it to a slice of another type.

lo.FlatMap[int, string]([]int{0, 1, 2}, func(x int, _ int) []string {
	return []string{
		strconv.FormatInt(x, 10),
		strconv.FormatInt(x, 10),
	}
})
// []string{"0", "0", "1", "1", "2", "2"}

[play]

Reduce

Reduces a collection to a single value. The value is calculated by accumulating the result of running each element in the collection through an accumulator function. Each successive invocation is supplied with the return value returned by the previous call.

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

[play]

ReduceRight

Like lo.Reduce except that it iterates over elements of collection from right to left.

result := lo.ReduceRight[[]int, []int]([][]int{{0, 1}, {2, 3}, {4, 5}}, func(agg []int, item []int, _ int) []int {
	  return append(agg, item...)
}, []int{})
// []int{4, 5, 2, 3, 0, 1}

[play]

ForEach

Iterates over elements of a collection and invokes the function over each element.

import "github.com/samber/lo"

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

[play]

Parallel processing: like lo.ForEach(), but the callback is called as a 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"}

[play]

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}

[play]

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}

[play]

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}}

[play]

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}}

[play]

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}}

[play]

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 := lop.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}

[play]

Interleave

Round-robin alternating input slices and sequentially appending value at index into result.

interleaved := lo.Interleave[int]([]int{1, 4, 7}, []int{2, 5, 8}, []int{3, 6, 9})
// []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

interleaved := lo.Interleave[int]([]int{1}, []int{2, 5, 8}, []int{3, 6}, []int{4, 7, 9, 10})
// []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

[play]

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{1, 4, 0, 3, 5, 2}

[play]

Reverse

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

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

[play]

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"}}

[play]

Repeat

Builds a slice with N copies of initial value.

type foo struct {
	bar string
}

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

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

[play]

RepeatBy

Builds a slice with values returned by N calls of callback.

slice := lo.RepeatBy[string](0, func (i int) string {
    return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})
// []string{}

slice := lo.RepeatBy[string](5, func(i int) string {
    return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})
// []string{"0", "1", "4", "9", "16"}

[play]

KeyBy

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

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

type Character struct {
	dir  string
	code int
}
characters := []Character{
    {dir: "left", code: 97},
    {dir: "right", code: 100},
}
result := lo.KeyBy[string, Character](characters, func(char Character) string {
    return string(rune(char.code))
})
//map[a:{dir:left code:97} d:{dir:right code:100}]

[play]

Associate (alias: SliceToMap)

Returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs would have the same key the last one gets added to the map.

The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.

in := []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}}

aMap := lo.Associate[*foo, string, int](in, func (f *foo) (string, int) {
	return f.baz, f.bar
})
// map[string][int]{ "apple":1, "banana":2 }

[play]

Drop

Drops n elements from the beginning of a slice or array.

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

[play]

DropRight

Drops n elements from the end of a slice or array.

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

[play]

DropWhile

Drop elements from the beginning of a slice or array while the predicate returns true.

l := lo.DropWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
	return len(val) <= 2
})
// []string{"aaa", "aa", "aa"}

[play]

DropRightWhile

Drop elements from the end of a slice or array while the predicate returns true.

l := lo.DropRightWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
	return len(val) <= 2
})
// []string{"a", "aa", "aaa"}

[play]

Reject

The opposite of Filter, this method returns the elements of collection that predicate does not return truthy for.

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

[play]

Count

Counts the number of elements in the collection that compare equal to value.

count := lo.Count[int]([]int{1, 5, 1}, 1)
// 2

[play]

CountBy

Counts the number of elements in the collection for which predicate is true.

count := lo.CountBy[int]([]int{1, 5, 1}, func(i int) bool {
    return i < 4
})
// 2

[play]

CountValues

Counts the number of each element in the collection.

lo.CountValues([]int{})
// map[int]int{}

lo.CountValues([]int{1, 2})
// map[int]int{1: 1, 2: 1}

lo.CountValues([]int{1, 2, 2})
// map[int]int{1: 1, 2: 2}

lo.CountValues([]string{"foo", "bar", ""})
// map[string]int{"": 1, "foo": 1, "bar": 1}

lo.CountValues([]string{"foo", "bar", "bar"})
// map[string]int{"foo": 1, "bar": 2}

[play]

CountValuesBy

Counts the number of each element in the collection. It ss equivalent to chaining lo.Map and lo.CountValues.

isEven := func(v int) bool {
    return v%2==0
}

lo.CountValuesBy([]int{}, isEven)
// map[bool]int{}

lo.CountValuesBy([]int{1, 2}, isEven)
// map[bool]int{false: 1, true: 1}

lo.CountValuesBy([]int{1, 2, 2}, isEven)
// map[bool]int{false: 1, true: 2}

length := func(v string) int {
    return len(v)
}

lo.CountValuesBy([]string{"foo", "bar", ""}, length)
// map[int]int{0: 1, 3: 2}

lo.CountValuesBy([]string{"foo", "bar", "bar"}, length)
// map[int]int{3: 3}

[play]

Subset

Returns a copy of a slice from offset up to length elements. Like slice[start:start+length], but does not panic on overflow.

in := []int{0, 1, 2, 3, 4}

sub := lo.Subset(in, 2, 3)
// []int{2, 3, 4}

sub := lo.Subset(in, -4, 3)
// []int{1, 2, 3}

sub := lo.Subset(in, -2, math.MaxUint)
// []int{3, 4}

[play]

Slice

Returns a copy of a slice from start up to, but not including end. Like slice[start:end], but does not panic on overflow.

in := []int{0, 1, 2, 3, 4}

slice := lo.Slice(in, 0, 5)
// []int{0, 1, 2, 3, 4}

slice := lo.Slice(in, 2, 3)
// []int{2}

slice := lo.Slice(in, 2, 6)
// []int{2, 3, 4}

slice := lo.Slice(in, 4, 3)
// []int{}

[play]

Replace

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.

in := []int{0, 1, 0, 1, 2, 3, 0}

slice := lo.Replace(in, 0, 42, 1)
// []int{42, 1, 0, 1, 2, 3, 0}

slice := lo.Replace(in, -1, 42, 1)
// []int{0, 1, 0, 1, 2, 3, 0}

slice := lo.Replace(in, 0, 42, 2)
// []int{42, 1, 42, 1, 2, 3, 0}

slice := lo.Replace(in, 0, 42, -1)
// []int{42, 1, 42, 1, 2, 3, 42}

[play]

ReplaceAll

Returns a copy of the slice with all non-overlapping instances of old replaced by new.

in := []int{0, 1, 0, 1, 2, 3, 0}

slice := lo.ReplaceAll(in, 0, 42)
// []int{42, 1, 42, 1, 2, 3, 42}

slice := lo.ReplaceAll(in, -1, 42)
// []int{0, 1, 0, 1, 2, 3, 0}

[play]

Compact

Returns a slice of all non-zero elements.

in := []string{"", "foo", "", "bar", ""}

slice := lo.Compact[string](in)
// []string{"foo", "bar"}

[play]

IsSorted

Checks if a slice is sorted.

slice := lo.IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
// true

[play]

IsSortedByKey

Checks if a slice is sorted by iteratee.

slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
    return len(s)
})
// true

[play]

Keys

Creates an array of the map keys.

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

[play]

Values

Creates an array of the map values.

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

[play]

PickBy

Returns same map type filtered by given predicate.

m := lo.PickBy[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
    return value%2 == 1
})
// map[string]int{"foo": 1, "baz": 3}

[play]

PickByKeys

Returns same map type filtered by given keys.

m := lo.PickByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// map[string]int{"foo": 1, "baz": 3}

[play]

PickByValues

Returns same map type filtered by given values.

m := lo.PickByValues[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"foo": 1, "baz": 3}

[play]

OmitBy

Returns same map type filtered by given predicate.

m := lo.OmitBy[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
    return value%2 == 1
})
// map[string]int{"bar": 2}

[play]

OmitByKeys

Returns same map type filtered by given keys.

m := lo.OmitByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// map[string]int{"bar": 2}

[play]

OmitByValues

Returns same map type filtered by given values.

m := lo.OmitByValues[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"bar": 2}

[play]

Entries (alias: ToPairs)

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,
//     },
// }

[play]

FromEntries (alias: FromPairs)

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}

[play]

Invert

Creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values.

m1 := lo.Invert[string, int](map[string]int{"a": 1, "b": 2})
// map[int]string{1: "a", 2: "b"}

m2 := lo.Invert[string, int](map[string]int{"a": 1, "b": 2, "c": 1})
// map[int]string{1: "c", 2: "b"}

[play]

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}

[play]

MapKeys

Manipulates a map keys and transforms it to a map of another type.

m2 := lo.MapKeys[int, int, string](map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(_ int, v int) string {
    return strconv.FormatInt(int64(v), 10)
})
// map[string]int{"1": 1, "2": 2, "3": 3, "4": 4}

[play]

MapValues

Manipulates a map values and transforms it to a map of another type.

m1 := map[int]int64{1: 1, 2: 2, 3: 3}

m2 := lo.MapValues[int, int64, string](m1, func(x int64, _ int) string {
	return strconv.FormatInt(x, 10)
})
// map[int]string{1: "1", 2: "2", 3: "3"}

[play]

MapEntries

Manipulates a map entries and transforms it to a map of another type.

in := map[string]int{"foo": 1, "bar": 2}

out := lo.MapEntries(in, func(k string, v int) (int, string) {
    return v,k
})
// map[int]string{1: "foo", 2: "bar"}

[play]

MapToSlice

Transforms a map into a slice based on specific iteratee.

m := map[int]int64{1: 4, 2: 5, 3: 6}

s := lo.MapToSlice(m, func(k int, v int64) string {
    return fmt.Sprintf("%d_%d", k, v)
})
// []string{"1_4", "2_5", "3_6"}

[play]

Range / RangeFrom / RangeWithSteps

Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.

result := lo.Range(4)
// [0, 1, 2, 3]

result := lo.Range(-4)
// [0, -1, -2, -3]

result := lo.RangeFrom(1, 5)
// [1, 2, 3, 4, 5]

result := lo.RangeFrom[float64](1.0, 5)
// [1.0, 2.0, 3.0, 4.0, 5.0]

result := lo.RangeWithSteps(0, 20, 5)
// [0, 5, 10, 15]

result := lo.RangeWithSteps[float32](-1.0, -4.0, -1.0)
// [-1.0, -2.0, -3.0]

result := lo.RangeWithSteps(1, 4, -1)
// []

result := lo.Range(0)
// []

[play]

Clamp

Clamps number within the inclusive lower and upper bounds.

r1 := lo.Clamp(0, -10, 10)
// 0

r2 := lo.Clamp(-42, -10, 10)
// -10

r3 := lo.Clamp(42, -10, 10)
// 10

[play]

Sum

Sums the values in a collection.

If collection is empty 0 is returned.

list := []int{1, 2, 3, 4, 5}
sum := lo.Sum(list)
// 15

[play]

SumBy

Summarizes the values in a collection using the given return value from the iteration function.

If collection is empty 0 is returned.

strings := []string{"foo", "bar"}
sum := lo.SumBy(strings, func(item string) int {
    return len(item)
})
// 6

[play]

Substring

Return part of a string.

sub := lo.Substring("hello", 2, 3)
// "llo"

sub := lo.Substring("hello", -4, 3)
// "ell"

sub := lo.Substring("hello", -2, math.MaxUint)
// "lo"

[play]

ChunkString

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

lo.ChunkString("123456", 2)
// []string{"12", "34", "56"}

lo.ChunkString("1234567", 2)
// []string{"12", "34", "56", "7"}

lo.ChunkString("", 2)
// []string{""}

lo.ChunkString("1", 2)
// []string{"1"}

[play]

RuneLength

An alias to utf8.RuneCountInString which returns the number of runes in string.

sub := lo.RuneLength("hellΓ΄")
// 5

sub := len("hellΓ΄")
// 6

[play]

T2 -> T9

Creates a tuple from a list of values.

tuple1 := lo.T2("x", 1)
// Tuple2[string, int]{A: "x", B: 1}

func example() (string, int) { return "y", 2 }
tuple2 := lo.T2(example())
// Tuple2[string, int]{A: "y", B: 2}

[play]

Unpack2 -> Unpack9

Returns values contained in tuple.

r1, r2 := lo.Unpack2[string, int](lo.Tuple2[string, int]{"a", 1})
// "a", 1

Unpack is also available as a method of TupleX.

tuple2 := lo.T2("a", 1)
a, b := tuple2.Unpack()
// "a" 1

[play]

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}}

[play]

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}

[play]

ChannelDispatcher

Distributes messages from input channels into N child channels. Close events are propagated to children.

Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0.

ch := make(chan int, 42)
for i := 0; i <= 10; i++ {
    ch <- i
}

children := lo.ChannelDispatcher(ch, 5, 10, DispatchingStrategyRoundRobin[int])
// []<-chan int{...}

consumer := func(c <-chan int) {
    for {
        msg, ok := <-c
        if !ok {
            println("closed")
            break
        }

        println(msg)
    }
}

for i := range children {
    go consumer(children[i])
}

Many distributions strategies are available:

Some strategies bring fallback, in order to favor non-blocking behaviors. See implementations.

For custom strategies, just implement the lo.DispatchingStrategy prototype:

type DispatchingStrategy[T any] func(message T, messageIndex uint64, channels []<-chan T) int

Eg:

type Message struct {
    TenantID uuid.UUID
}

func hash(id uuid.UUID) int {
	h := fnv.New32a()
	h.Write([]byte(id.String()))
	return int(h.Sum32())
}

// Routes messages per TenantID.
customStrategy := func(message pubsub.AMQPSubMessage, messageIndex uint64, channels []<-chan pubsub.AMQPSubMessage) int {
    destination := hash(message.TenantID) % len(channels)

    // check if channel is full
    if len(channels[destination]) < cap(channels[destination]) {
        return destination
    }

    // fallback when child channel is full
    return utils.DispatchingStrategyRoundRobin(message, uint64(destination), channels)
}

children := lo.ChannelDispatcher(ch, 5, 10, customStrategy)
...
SliceToChannel

Returns a read-only channels of collection elements. Channel is closed after last element. Channel capacity can be customized.

list := []int{1, 2, 3, 4, 5}

for v := range lo.SliceToChannel(2, list) {
    println(v)
}
// prints 1, then 2, then 3, then 4, then 5
ChannelToSlice

Returns a slice built from channels items. Blocks until channel closes.

list := []int{1, 2, 3, 4, 5}
ch := lo.SliceToChannel(2, list)

items := ChannelToSlice(ch)
// []int{1, 2, 3, 4, 5}
Generator

Implements the generator design pattern. Channel is closed after last element. Channel capacity can be customized.

generator := func(yield func(int)) {
    yield(1)
    yield(2)
    yield(3)
}

for v := range lo.Generator(2, generator) {
    println(v)
}
// prints 1, then 2, then 3
Batch

Creates a slice of n elements from a channel. Returns the slice, the slice length, the read time and the channel status (opened/closed).

ch := lo.SliceToChannel(2, []int{1, 2, 3, 4, 5})

items1, length1, duration1, ok1 := lo.Batch(ch, 3)
// []int{1, 2, 3}, 3, 0s, true
items2, length2, duration2, ok2 := lo.Batch(ch, 3)
// []int{4, 5}, 2, 0s, false

Example: RabbitMQ consumer πŸ‘‡

ch := readFromQueue()

for {
    // read 1k items
    items, length, _, ok := lo.Batch(ch, 1000)

    // do batching stuff

    if !ok {
        break
    }
}
BatchWithTimeout

Creates a slice of n elements from a channel, with timeout. Returns the slice, the slice length, the read time and the channel status (opened/closed).

generator := func(yield func(int)) {
    for i := 0; i < 5; i++ {
        yield(i)
        time.Sleep(35*time.Millisecond)
    }
}

ch := lo.Generator(0, generator)

items1, length1, duration1, ok1 := lo.BatchWithTimeout(ch, 3, 100*time.Millisecond)
// []int{1, 2}, 2, 100ms, true
items2, length2, duration2, ok2 := lo.BatchWithTimeout(ch, 3, 100*time.Millisecond)
// []int{3, 4, 5}, 3, 75ms, true
items3, length3, duration2, ok3 := lo.BatchWithTimeout(ch, 3, 100*time.Millisecond)
// []int{}, 0, 10ms, false

Example: RabbitMQ consumer πŸ‘‡

ch := readFromQueue()

for {
    // read 1k items
    // wait up to 1 second
    items, length, _, ok := lo.BatchWithTimeout(ch, 1000, 1*time.Second)

    // do batching stuff

    if !ok {
        break
    }
}

Example: Multithreaded RabbitMQ consumer πŸ‘‡

ch := readFromQueue()

// 5 workers
// prefetch 1k messages per worker
children := lo.ChannelDispatcher(ch, 5, 1000, DispatchingStrategyFirst[int])

consumer := func(c <-chan int) {
    for {
        // read 1k items
        // wait up to 1 second
        items, length, _, ok := lo.BatchWithTimeout(ch, 1000, 1*time.Second)

        // do batching stuff

        if !ok {
            break
        }
    }
}

for i := range children {
    go consumer(children[i])
}
ChannelMerge

Collects messages from multiple input channels into a single buffered channel. Output messages has no priority.

stream1 := make(chan int, 42)
stream2 := make(chan int, 42)
stream3 := make(chan int, 42)

all := lo.ChannelMerge(100, stream1, stream2, stream3)
Contains

Returns true if an element is present in a collection.

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

Returns true if the predicate function returns true.

present := lo.ContainsBy[int]([]int{0, 1, 2, 3, 4, 5}, func(x int) bool {
    return x == 3
})
// true
Every

Returns true if all elements of a subset are contained into a collection or if the subset is empty.

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
EveryBy

Returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.

b := EveryBy[int]([]int{1, 2, 3, 4}, func(x int) bool {
    return x < 5
})
// true
Some

Returns true if at least 1 element of a subset is contained into a collection. If the subset is empty Some returns false.

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
SomeBy

Returns true if the predicate returns true for any of the elements in the collection. If the collection is empty SomeBy returns false.

b := SomeBy[int]([]int{1, 2, 3, 4}, func(x int) bool {
    return x < 3
})
// true
None

Returns true if no element of a subset are contained into a collection or if the subset is empty.

b := None[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// false
b := None[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// true
NoneBy

Returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.

b := NoneBy[int]([]int{1, 2, 3, 4}, func(x int) bool {
    return x < 0
})
// true
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 := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
// []int{}, []int{}
Union

Returns all distinct elements from given collections. Result will not change the order of elements relatively.

union := lo.Union[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2}, []int{0, 10})
// []int{0, 1, 2, 3, 4, 5, 10}
Without

Returns slice excluding all given values.

subset := lo.Without[int]([]int{0, 2, 10}, 2)
// []int{0, 10}

subset := lo.Without[int]([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
// []int{10}
WithoutEmpty

Returns slice excluding empty values.

subset := lo.WithoutEmpty[int]([]int{0, 2, 10})
// []int{2, 10}
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
LastIndexOf

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
FindIndexOf

FindIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.

str, index, ok := lo.FindIndexOf[string]([]string{"a", "b", "a", "b"}, func(i string) bool {
    return i == "b"
})
// "b", 1, true

str, index, ok := lo.FindIndexOf[string]([]string{"foobar"}, func(i string) bool {
    return i == "b"
})
// "", -1, false
FindLastIndexOf

FindLastIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.

str, index, ok := lo.FindLastIndexOf[string]([]string{"a", "b", "a", "b"}, func(i string) bool {
    return i == "b"
})
// "b", 4, true

str, index, ok := lo.FindLastIndexOf[string]([]string{"foobar"}, func(i string) bool {
    return i == "b"
})
// "", -1, false
FindKey

Returns the key of the first value matching.

result1, ok1 := lo.FindKey(map[string]int{"foo": 1, "bar": 2, "baz": 3}, 2)
// "bar", true

result2, ok2 := lo.FindKey(map[string]int{"foo": 1, "bar": 2, "baz": 3}, 42)
// "", false

type test struct {
    foobar string
}
result3, ok3 := lo.FindKey(map[string]test{"foo": test{"foo"}, "bar": test{"bar"}, "baz": test{"baz"}}, test{"foo"})
// "foo", true
FindKeyBy

Returns the key of the first element predicate returns truthy for.

result1, ok1 := lo.FindKeyBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string, v int) bool {
    return k == "foo"
})
// "foo", true

result2, ok2 := lo.FindKeyBy(map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(k string, v int) bool {
    return false
})
// "", false
FindUniques

Returns a slice with all the unique elements of the collection. The order of result values is determined by the order they occur in the array.

uniqueValues := lo.FindUniques[int]([]int{1, 2, 2, 1, 2, 3})
// []int{3}
FindUniquesBy

Returns a slice with all the unique elements of the collection. 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.

uniqueValues := lo.FindUniquesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
    return i%3
})
// []int{5}
FindDuplicates

Returns a slice with the first occurrence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array.

duplicatedValues := lo.FindDuplicates[int]([]int{1, 2, 2, 1, 2, 3})
// []int{1, 2}
FindDuplicatesBy

Returns a slice with the first occurrence of each duplicated elements of the collection. 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.

duplicatedValues := lo.FindDuplicatesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
    return i%3
})
// []int{3, 4}
Min

Search the minimum value of a collection.

Returns zero value when collection is empty.

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

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

Search the minimum value of a collection using the given comparison function.

If several values of the collection are equal to the smallest value, returns the first such value.

Returns zero value when collection is empty.

min := lo.MinBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
    return len(item) < len(min)
})
// "s1"

min := lo.MinBy([]string{}, func(item string, min string) bool {
    return len(item) < len(min)
})
// ""
Max

Search the maximum value of a collection.

Returns zero value when collection is empty.

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

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

Search the maximum value of a collection using the given comparison function.

If several values of the collection are equal to the greatest value, returns the first such value.

Returns zero value when collection is empty.

max := lo.MaxBy([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
    return len(item) > len(max)
})
// "string1"

max := lo.MaxBy([]string{}, func(item string, max string) bool {
    return len(item) > len(max)
})
// ""
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"

[play]

TernaryF

A 1 line if/else statement whose options are functions.

result := lo.TernaryF[string](true, func() string { return "a" }, func() string { return "b" })
// "a"

result := lo.TernaryF[string](false, func() string { return "a" }, func() string { return "b" })
// "b"

Useful to avoid nil-pointer dereferencing in intializations, or avoid running unnecessary code

var s *string

someStr := TernaryF[string](s == nil, func() string { return uuid.New().String() }, func() string { return *s })
// ef782193-c30c-4e2e-a7ae-f8ab5e125e02

[play]

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

Using callbacks:

result := lo.IfF[int](true, func () int {
        return 1
    }).
    ElseIfF(false, func () int {
        return 2
    }).
    ElseF(func () int {
        return 3
    })
// 1

Mixed:

result := lo.IfF[int](true, func () int {
        return 1
    }).
    Else(42)
// 1

[play]

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"

Mixed:

result := lo.Switch[int, string](1).
    CaseF(1, func() string {
        return "1"
    }).
    Default("42")
// "1"

[play]

ToPtr

Returns a pointer copy of value.

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

Returns the pointer value or empty.

str := "hello world"
value := lo.FromPtr[string](&str)
// "hello world"

value := lo.FromPtr[string](nil)
// ""
FromPtrOr

Returns the pointer value or the fallback value.

str := "hello world"
value := lo.FromPtrOr[string](&str, "empty")
// "hello world"

value := lo.FromPtrOr[string](nil, "empty")
// "empty"
ToSlicePtr

Returns a slice of pointer copy of value.

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

Returns a slice with all elements mapped to any type.

elements := lo.ToAnySlice[int]([]int{1, 5, 1})
// []any{1, 5, 1}
FromAnySlice

Returns an any slice with all elements mapped to a type. Returns false in case of type conversion failure.

elements, ok := lo.FromAnySlice[string]([]any{"foobar", 42})
// []string{}, false

elements, ok := lo.FromAnySlice[string]([]any{"foobar", "42"})
// []string{"foobar", "42"}, true
Empty

Returns an empty value.

lo.Empty[int]()
// 0
lo.Empty[string]()
// ""
lo.Empty[bool]()
// false
IsEmpty

Returns true if argument is a zero value.

lo.IsEmpty[int](0)
// true
lo.IsEmpty[int](42)
// false

lo.IsEmpty[string]("")
// true
lo.IsEmpty[bool]("foobar")
// false

type test struct {
    foobar string
}

lo.IsEmpty[test](test{foobar: ""})
// true
lo.IsEmpty[test](test{foobar: "foobar"})
// false
IsNotEmpty

Returns true if argument is a zero value.

lo.IsNotEmpty[int](0)
// false
lo.IsNotEmpty[int](42)
// true

lo.IsNotEmpty[string]("")
// false
lo.IsNotEmpty[bool]("foobar")
// true

type test struct {
    foobar string
}

lo.IsNotEmpty[test](test{foobar: ""})
// false
lo.IsNotEmpty[test](test{foobar: "foobar"})
// true
Coalesce

Returns the first non-empty arguments. Arguments must be comparable.

result, ok := lo.Coalesce(0, 1, 2, 3)
// 1 true

result, ok := lo.Coalesce("")
// "" false

var nilStr *string
str := "foobar"
result, ok := lo.Coalesce[*string](nil, nilStr, &str)
// &"foobar" true
Partial

Returns new function that, when called, has its first argument set to the provided value.

add := func(x, y int) int { return x + y }
f := lo.Partial(add, 5)

f(10)
// 15

f(42)
// 47
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 successful 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.

[play]

AttemptWithDelay

Invokes a function N times until it returns valid output, with a pause between each call. Returning either the caught error or nil.

When first argument is less than 1, the function runs until a successful response is returned.

iter, duration, err := lo.AttemptWithDelay(5, 2*time.Second, func(i int, duration time.Duration) error {
    if i == 2 {
        return nil
    }

    return fmt.Errorf("failed")
})
// 3
// ~ 4 seconds
// nil

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

[play]

Debounce

NewDebounce creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed, until cancel is called.

f := func() {
    println("Called once after 100ms when debounce stopped invoking!")
}

debounce, cancel := lo.NewDebounce(100 * time.Millisecond, f)
for j := 0; j < 10; j++ {
    debounce()
}

time.Sleep(1 * time.Second)
cancel()

[play]

Synchronize

Wraps the underlying callback in a mutex. It receives an optional mutex.

s := lo.Synchronize()

for i := 0; i < 10; i++ {
    go s.Do(func () {
        println("will be called sequentially")
    })
}

It is equivalent to:

mu := sync.Mutex{}

func foobar() {
    mu.Lock()
    defer mu.Unlock()

    // ...
}
Async

Executes a function in a goroutine and returns the result in a channel.

ch := lo.Async(func() error { time.Sleep(10 * time.Second); return nil })
// chan error (nil)
Async{0->6}

Executes a function in a goroutine and returns the result in a channel. For function with multiple return values, the results will be returned as a tuple inside the channel. For function without return, struct{} will be returned in the channel.

ch := lo.Async0(func() { time.Sleep(10 * time.Second) })
// chan struct{}

ch := lo.Async1(func() int {
  time.Sleep(10 * time.Second);
  return 42
})
// chan int (42)

ch := lo.Async2(func() (int, string) {
  time.Sleep(10 * time.Second);
  return 42, "Hello"
})
// chan lo.Tuple2[int, string] ({42, "Hello"})
Validate

Helper function that creates an error when a condition is not met.

slice := []string{"a"}
val := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", slice)
// error("Slice should be empty but contains [a]")

slice := []string{}
val := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", slice)
// nil

[play]

Must

Wraps a function call to panics if second argument is error or false, returns the value otherwise.

val := lo.Must(time.Parse("2006-01-02", "2022-01-15"))
// 2022-01-15

val := lo.Must(time.Parse("2006-01-02", "bad-value"))
// panics

[play]

Must{0->6}

Must* has the same behavior as Must, but returns multiple values.

func example0() (error)
func example1() (int, error)
func example2() (int, string, error)
func example3() (int, string, time.Date, error)
func example4() (int, string, time.Date, bool, error)
func example5() (int, string, time.Date, bool, float64, error)
func example6() (int, string, time.Date, bool, float64, byte, error)

lo.Must0(example0())
val1 := lo.Must1(example1())    // alias to Must
val1, val2 := lo.Must2(example2())
val1, val2, val3 := lo.Must3(example3())
val1, val2, val3, val4 := lo.Must4(example4())
val1, val2, val3, val4, val5 := lo.Must5(example5())
val1, val2, val3, val4, val5, val6 := lo.Must6(example6())

You can wrap functions like func (...) (..., ok bool).

// math.Signbit(float64) bool
lo.Must0(math.Signbit(v))

// bytes.Cut([]byte,[]byte) ([]byte, []byte, bool)
before, after := lo.Must2(bytes.Cut(s, sep))

You can give context to the panic message by adding some printf-like arguments.

val, ok := lo.Find(myString, func(i string) bool {
    return i == requiredChar
})
lo.Must0(ok, "'%s' must always contain '%s'", myString, requiredChar)

list := []int{0, 1, 2}
item := 5
lo.Must0(lo.Contains[int](list, item), "'%s' must always contain '%s'", list, item)
...

[play]

Try

Calls the function and return false in case of error and on panic.

ok := lo.Try(func() error {
    panic("error")
    return nil
})
// false

ok := lo.Try(func() error {
    return nil
})
// true

ok := lo.Try(func() error {
    return fmt.Errorf("error")
})
// false

[play]

Try{0->6}

The same behavior than Try, but callback returns 2 variables.

ok := lo.Try2(func() (string, error) {
    panic("error")
    return "", nil
})
// false

[play]

TryOr

Calls the function and return a default value in case of error and on panic.

str, ok := lo.TryOr(func() (string, error) {
    panic("error")
    return "hello", nil
}, "world")
// world
// false

ok := lo.TryOr(func() error {
    return "hello", nil
}, "world")
// hello
// true

ok := lo.TryOr(func() error {
    return "hello", fmt.Errorf("error")
}, "world")
// world
// false

[play]

TryOr{0->6}

The same behavior than TryOr, but callback returns 2 variables.

str, nbr, ok := lo.TryOr2(func() (string, int, error) {
    panic("error")
    return "hello", 42, nil
}, "world", 21)
// world
// 21
// false

[play]

TryWithErrorValue

The same behavior than Try, but also returns value passed to panic.

err, ok := lo.TryWithErrorValue(func() error {
    panic("error")
    return nil
})
// "error", false

[play]

TryCatch

The same behavior than Try, but calls the catch function in case of error.

caught := false

ok := lo.TryCatch(func() error {
    panic("error")
    return nil
}, func() {
    caught = true
})
// false
// caught == true

[play]

TryCatchWithErrorValue

The same behavior than TryWithErrorValue, but calls the catch function in case of error.

caught := false

ok := lo.TryCatchWithErrorValue(func() error {
    panic("error")
    return nil
}, func(val any) {
    caught = val == "error"
})
// false
// caught == true

[play]

ErrorsAs

A shortcut for:

err := doSomething()

var rateLimitErr *RateLimitError
if ok := errors.As(err, &rateLimitErr); ok {
    // retry later
}

1 line lo helper:

err := doSomething()

if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
    // retry later
}

[play]

πŸ›© 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 reflection-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 useful for long-running callbacks, such as i/o bound processing.
  • for beats other implementations for memory and CPU.

🀝 Contributing

Don't hesitate ;)

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

# Run tests
make test
# or
make watch-test

πŸ‘€ Contributors

Contributors

πŸ’« Show your support

Give a ⭐️ if this project helped you!

support us

πŸ“ License

Copyright Β© 2022 Samuel Berthe.

This project is MIT licensed.

Documentation ΒΆ

Index ΒΆ

Examples ΒΆ

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. Play: https://go.dev/play/p/VhwfJOyxf5o

Example ΒΆ
result := Assign(
	map[string]int{"a": 1, "b": 2},
	map[string]int{"b": 3, "c": 4},
)

fmt.Printf("%v %v %v %v", len(result), result["a"], result["b"], result["c"])
Output:

3 1 3 4

func Associate ΒΆ

func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V

Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs would have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original array. Play: https://go.dev/play/p/WHa2CfMO3Lr

Example ΒΆ
list := []string{"a", "aa", "aaa"}

result := Associate(list, func(str string) (string, int) {
	return str, len(str)
})

fmt.Printf("%v", result)
Output:

map[a:1 aa:2 aaa:3]

func Async ΒΆ

func Async[A any](f func() A) chan A

Async executes a function in a goroutine and returns the result in a channel.

func Async0 ΒΆ

func Async0(f func()) chan struct{}

Async0 executes a function in a goroutine and returns a channel set once the function finishes.

func Async1 ΒΆ

func Async1[A any](f func() A) chan A

Async1 is an alias to Async.

func Async2 ΒΆ

func Async2[A any, B any](f func() (A, B)) chan Tuple2[A, B]

Async2 has the same behavior as Async, but returns the 2 results as a tuple inside the channel.

func Async3 ΒΆ

func Async3[A any, B any, C any](f func() (A, B, C)) chan Tuple3[A, B, C]

Async3 has the same behavior as Async, but returns the 3 results as a tuple inside the channel.

func Async4 ΒΆ

func Async4[A any, B any, C any, D any](f func() (A, B, C, D)) chan Tuple4[A, B, C, D]

Async4 has the same behavior as Async, but returns the 4 results as a tuple inside the channel.

func Async5 ΒΆ

func Async5[A any, B any, C any, D any, E any](f func() (A, B, C, D, E)) chan Tuple5[A, B, C, D, E]

Async5 has the same behavior as Async, but returns the 5 results as a tuple inside the channel.

func Async6 ΒΆ

func Async6[A any, B any, C any, D any, E any, F any](f func() (A, B, C, D, E, F)) chan Tuple6[A, B, C, D, E, F]

Async6 has the same behavior as Async, but returns the 6 results as a tuple inside the channel.

func Attempt ΒΆ

func Attempt(maxIteration int, f func(index 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 successful response is returned. Play: https://go.dev/play/p/3ggJZ2ZKcMj

Example ΒΆ
count1, err1 := Attempt(2, func(i int) error {
	if i == 0 {
		return fmt.Errorf("error")
	}

	return nil
})

count2, err2 := Attempt(2, func(i int) error {
	if i < 10 {
		return fmt.Errorf("error")
	}

	return nil
})

fmt.Printf("%v %v\n", count1, err1)
fmt.Printf("%v %v\n", count2, err2)
Output:

2 <nil>
2 error

func AttemptWithDelay ΒΆ

func AttemptWithDelay(maxIteration int, delay time.Duration, f func(index int, duration time.Duration) error) (int, time.Duration, error)

AttemptWithDelay invokes a function N times until it returns valid output, with a pause between each call. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/tVs6CygC7m1

Example ΒΆ
count1, time1, err1 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
	if i == 0 {
		return fmt.Errorf("error")
	}

	return nil
})

count2, time2, err2 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
	if i < 10 {
		return fmt.Errorf("error")
	}

	return nil
})

fmt.Printf("%v %v %v\n", count1, time1.Truncate(time.Millisecond), err1)
fmt.Printf("%v %v %v\n", count2, time2.Truncate(time.Millisecond), err2)
Output:

2 1ms <nil>
2 1ms error

func Batch ΒΆ

func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool)

Batch creates a slice of n elements from a channel. Returns the slice and the slice length. @TODO: we should probably provide an helper that reuse the same buffer.

func BatchWithTimeout ΒΆ

func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool)

BatchWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length. @TODO: we should probably provide an helper that reuse the same buffer.

func ChannelDispatcher ΒΆ

func ChannelDispatcher[T any](stream <-chan T, count int, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T

ChannelDispatcher distributes messages from input channels into N child channels. Close events are propagated to children. Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0.

func ChannelMerge ΒΆ

func ChannelMerge[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T

ChannelMerge collects messages from multiple input channels into a single buffered channel. Output messages has no priority.

func ChannelToSlice ΒΆ

func ChannelToSlice[T any](ch <-chan T) []T

ChannelToSlice returns a slice built from channels items. Blocks until channel closes.

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. Play: https://go.dev/play/p/EeKl0AuTehH

Example ΒΆ
list := []int{0, 1, 2, 3, 4}

result := Chunk(list, 2)

for _, item := range result {
	fmt.Printf("%v\n", item)
}
Output:

[0 1]
[2 3]
[4]

func ChunkString ΒΆ

func ChunkString[T ~string](str T, size int) []T

ChunkString returns an array of strings split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements. Play: https://go.dev/play/p/__FLTuJVz54

Example ΒΆ
result1 := ChunkString("123456", 2)
result2 := ChunkString("1234567", 2)
result3 := ChunkString("", 2)
result4 := ChunkString("1", 2)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
Output:

[12 34 56]
[12 34 56 7]
[]
[1]

func Clamp ΒΆ

func Clamp[T constraints.Ordered](value T, min T, max T) T

Clamp clamps number within the inclusive lower and upper bounds. Play: https://go.dev/play/p/RU4lJNC2hlI

Example ΒΆ
result1 := Clamp(0, -10, 10)
result2 := Clamp(-42, -10, 10)
result3 := Clamp(42, -10, 10)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
Output:

0
-10
10

func Coalesce ΒΆ

func Coalesce[T comparable](v ...T) (result T, ok bool)

Coalesce returns the first non-empty arguments. Arguments must be comparable.

func Compact ΒΆ

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

Compact returns a slice of all non-zero elements. Play: https://go.dev/play/p/tXiy-iK6PAc

func Contains ΒΆ

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

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

func ContainsBy ΒΆ

func ContainsBy[T any](collection []T, predicate func(item T) bool) bool

ContainsBy returns true if predicate function return true.

func Count ΒΆ

func Count[T comparable](collection []T, value T) (count int)

Count counts the number of elements in the collection that compare equal to value. Play: https://go.dev/play/p/Y3FlK54yveC

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}

result := Count(list, 2)

fmt.Printf("%v", result)
Output:

2

func CountBy ΒΆ

func CountBy[T any](collection []T, predicate func(item T) bool) (count int)

CountBy counts the number of elements in the collection for which predicate is true. Play: https://go.dev/play/p/ByQbNYQQi4X

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}

result := CountBy(list, func(i int) bool {
	return i < 4
})

fmt.Printf("%v", result)
Output:

8

func CountValues ΒΆ

func CountValues[T comparable](collection []T) map[T]int

CountValues counts the number of each element in the collection. Play: https://go.dev/play/p/-p-PyLT4dfy

Example ΒΆ
result1 := CountValues([]int{})
result2 := CountValues([]int{1, 2})
result3 := CountValues([]int{1, 2, 2})
result4 := CountValues([]string{"foo", "bar", ""})
result5 := CountValues([]string{"foo", "bar", "bar"})

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

map[]
map[1:1 2:1]
map[1:1 2:2]
map[:1 bar:1 foo:1]
map[bar:2 foo:1]

func CountValuesBy ΒΆ

func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) map[U]int

CountValuesBy counts the number of each element return from mapper function. Is equivalent to chaining lo.Map and lo.CountValues. Play: https://go.dev/play/p/2U0dG1SnOmS

Example ΒΆ
isEven := func(v int) bool {
	return v%2 == 0
}

result1 := CountValuesBy([]int{}, isEven)
result2 := CountValuesBy([]int{1, 2}, isEven)
result3 := CountValuesBy([]int{1, 2, 2}, isEven)

length := func(v string) int {
	return len(v)
}

result4 := CountValuesBy([]string{"foo", "bar", ""}, length)
result5 := CountValuesBy([]string{"foo", "bar", "bar"}, length)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output:

map[]
map[false:1 true:1]
map[false:1 true:2]
map[0:1 3:2]
map[3:3]

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 DispatchingStrategyFirst ΒΆ

func DispatchingStrategyFirst[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyFirst distributes messages in the first non-full channel. If the capacity of the first channel is exceeded, the second channel will be selected and so on.

func DispatchingStrategyLeast ΒΆ

func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyLeast distributes messages in the emptiest channel.

func DispatchingStrategyMost ΒΆ

func DispatchingStrategyMost[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyMost distributes messages in the fullest channel. If the channel capacity is exceeded, the next channel will be selected and so on.

func DispatchingStrategyRandom ΒΆ

func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyRandom distributes messages in a random manner. If the channel capacity is exceeded, another random channel will be selected and so on.

func DispatchingStrategyRoundRobin ΒΆ

func DispatchingStrategyRoundRobin[T any](msg T, index uint64, channels []<-chan T) int

DispatchingStrategyRoundRobin distributes messages in a rotating sequential manner. If the channel capacity is exceeded, the next channel will be selected and so on.

func Drop ΒΆ

func Drop[T any](collection []T, n int) []T

Drop drops n elements from the beginning of a slice or array. Play: https://go.dev/play/p/JswS7vXRJP2

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := Drop(list, 2)

fmt.Printf("%v", result)
Output:

[2 3 4 5]

func DropRight ΒΆ

func DropRight[T any](collection []T, n int) []T

DropRight drops n elements from the end of a slice or array. Play: https://go.dev/play/p/GG0nXkSJJa3

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := DropRight(list, 2)

fmt.Printf("%v", result)
Output:

[0 1 2 3]

func DropRightWhile ΒΆ

func DropRightWhile[T any](collection []T, predicate func(item T) bool) []T

DropRightWhile drops elements from the end of a slice or array while the predicate returns true. Play: https://go.dev/play/p/3-n71oEC0Hz

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := DropRightWhile(list, func(val int) bool {
	return val > 2
})

fmt.Printf("%v", result)
Output:

[0 1 2]

func DropWhile ΒΆ

func DropWhile[T any](collection []T, predicate func(item T) bool) []T

DropWhile drops elements from the beginning of a slice or array while the predicate returns true. Play: https://go.dev/play/p/7gBPYw2IK16

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := DropWhile(list, func(val int) bool {
	return val < 2
})

fmt.Printf("%v", result)
Output:

[2 3 4 5]

func Empty ΒΆ

func Empty[T any]() T

Empty returns an empty value.

func ErrorsAs ΒΆ

func ErrorsAs[T error](err error) (T, bool)

ErrorsAs is a shortcut for errors.As(err, &&T). Play: https://go.dev/play/p/8wk5rH8UfrE

Example ΒΆ
doSomething := func() error {
	return &myError{}
}

err := doSomething()

if rateLimitErr, ok := ErrorsAs[*myError](err); ok {
	fmt.Printf("is type myError, err: %s", rateLimitErr.Error())
} else {
	fmt.Printf("is not type myError")
}
Output:

is type myError, err: my error

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 or if the subset is empty.

func EveryBy ΒΆ

func EveryBy[T any](collection []T, predicate func(item T) bool) bool

EveryBy returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.

func Fill ΒΆ

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

Fill fills elements of array with `initial` value. Play: https://go.dev/play/p/VwR34GzqEub

Example ΒΆ
list := []foo{foo{"a"}, foo{"a"}}

result := Fill[foo](list, foo{"b"})

fmt.Printf("%v", result)
Output:

[{b} {b}]

func Filter ΒΆ

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

Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for. Play: https://go.dev/play/p/Apjg3WeSi7K

Example ΒΆ
list := []int64{1, 2, 3, 4}

result := Filter(list, func(nbr int64, index int) bool {
	return nbr%2 == 0
})

fmt.Printf("%v", result)
Output:

[2 4]

func FilterMap ΒΆ

func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R

FilterMap returns a slice which obtained after both filtering and mapping using the given callback function. The callback function should return two values:

  • the result of the mapping operation and
  • whether the result element should be included or not.

Play: https://go.dev/play/p/-AuYXfy7opz

Example ΒΆ
list := []int64{1, 2, 3, 4}

result := FilterMap(list, func(nbr int64, index int) (string, bool) {
	return strconv.FormatInt(nbr*2, 10), nbr%2 == 0
})

fmt.Printf("%v", result)
Output:

[4 8]

func Find ΒΆ

func Find[T any](collection []T, predicate func(item 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 FindAll ΒΆ

func FindAll[T any](collection []T, predicate func(item T) bool) ([]T, bool)

FindAll searches all elements in a slice based on a predicate. It returns the elements and true if at least one element was found.

func FindDuplicates ΒΆ

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

FindDuplicates returns a slice with the first occurrence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the collection.

func FindDuplicatesBy ΒΆ

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

FindDuplicatesBy returns a slice with the first occurrence of each duplicated elements of the collection. 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 FindIndexOf ΒΆ

func FindIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)

FindIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.

func FindKey ΒΆ

func FindKey[K comparable, V comparable](object map[K]V, value V) (K, bool)

FindKey returns the key of the first value matching.

func FindKeyBy ΒΆ

func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value V) bool) (K, bool)

FindKeyBy returns the key of the first element predicate returns truthy for.

func FindLastIndexOf ΒΆ

func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)

FindLastIndexOf searches last element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.

func FindOrElse ΒΆ

func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) T

FindOrElse search an element in a slice based on a predicate. It returns the element if found or a given fallback value otherwise.

func FindUniques ΒΆ

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

FindUniques returns a slice with all the unique elements of the collection. The order of result values is determined by the order they occur in the collection.

func FindUniquesBy ΒΆ

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

FindUniquesBy returns a slice with all the unique elements of the collection. 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 FlatMap ΒΆ

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

FlatMap manipulates a slice and transforms and flattens it to a slice of another type. Play: https://go.dev/play/p/YSoYmQTA8-U

Example ΒΆ
list := []int64{1, 2, 3, 4}

result := FlatMap(list, func(nbr int64, index int) []string {
	return []string{
		strconv.FormatInt(nbr, 10), // base 10
		strconv.FormatInt(nbr, 2),  // base 2
	}
})

fmt.Printf("%v", result)
Output:

[1 1 2 10 3 11 4 100]

func Flatten ΒΆ

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

Flatten returns an array a single level deep. Play: https://go.dev/play/p/rbp9ORaMpjw

Example ΒΆ
list := [][]int{{0, 1, 2}, {3, 4, 5}}

result := Flatten(list)

fmt.Printf("%v", result)
Output:

[0 1 2 3 4 5]

func ForEach ΒΆ

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

ForEach iterates over elements of collection and invokes iteratee for each element. Play: https://go.dev/play/p/oofyiUPRf8t

Example ΒΆ
list := []int64{1, 2, 3, 4}

ForEach(list, func(x int64, _ int) {
	fmt.Println(x)
})
Output:

1
2
3
4

func FromAnySlice ΒΆ

func FromAnySlice[T any](in []any) (out []T, ok bool)

FromAnySlice returns an `any` slice with all elements mapped to a type. Returns false in case of type conversion failure.

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. Play: https://go.dev/play/p/oIr5KHFGCEN

Example ΒΆ
result := FromEntries([]Entry[string, int]{
	{
		Key:   "foo",
		Value: 1,
	},
	{
		Key:   "bar",
		Value: 2,
	},
	{
		Key:   "baz",
		Value: 3,
	},
})

fmt.Printf("%v %v %v %v", len(result), result["foo"], result["bar"], result["baz"])
Output:

3 1 2 3

func FromPairs ΒΆ

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

FromPairs transforms an array of key/value pairs into a map. Alias of FromEntries(). Play: https://go.dev/play/p/oIr5KHFGCEN

func FromPtr ΒΆ

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

FromPtr returns the pointer value or empty.

func FromPtrOr ΒΆ

func FromPtrOr[T any](x *T, fallback T) T

FromPtrOr returns the pointer value or the fallback value.

func Generator ΒΆ

func Generator[T any](bufferSize int, generator func(yield func(T))) <-chan T

Generator implements the generator design pattern.

func GroupBy ΒΆ

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

GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee. Play: https://go.dev/play/p/XnQBd_v6brd

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := GroupBy(list, func(i int) int {
	return i % 3
})

fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output:

[0 3]
[1 4]
[2 5]

func If ΒΆ

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

If. Play: https://go.dev/play/p/WSw3ApMxhyW

Example ΒΆ
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

func IfF ΒΆ

func IfF[T any](condition bool, resultF func() T) *ifElse[T]

IfF. Play: https://go.dev/play/p/WSw3ApMxhyW

Example ΒΆ
result1 := If(true, 1).
	ElseIf(false, 2).
	Else(3)

result2 := If(false, 1).
	ElseIf(true, 2).
	Else(3)

result3 := If(false, 1).
	ElseIf(false, 2).
	Else(3)

result4 := IfF(true, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

result5 := IfF(false, func() int { return 1 }).
	ElseIfF(true, func() int { return 2 }).
	ElseF(func() int { return 3 })

result6 := IfF(false, func() int { return 1 }).
	ElseIfF(false, func() int { return 2 }).
	ElseF(func() int { return 3 })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

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 Interleave ΒΆ

func Interleave[T any](collections ...[]T) []T

Interleave round-robin alternating input slices and sequentially appending value at index into result Play: https://go.dev/play/p/DDhlwrShbwe

Example ΒΆ
list1 := [][]int{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}
list2 := [][]int{{1}, {2, 5, 8}, {3, 6}, {4, 7, 9, 10}}

result1 := Interleave(list1...)
result2 := Interleave(list2...)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
Output:

[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9 10]

func Intersect ΒΆ

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

Intersect returns the intersection between two collections.

func Invert ΒΆ

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

Invert creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values. Play: https://go.dev/play/p/rFQ4rak6iA1

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := Invert(kv)

fmt.Printf("%v %v %v %v", len(result), result[1], result[2], result[3])
Output:

3 foo bar baz

func IsEmpty ΒΆ

func IsEmpty[T comparable](v T) bool

IsEmpty returns true if argument is a zero value.

func IsNotEmpty ΒΆ

func IsNotEmpty[T comparable](v T) bool

IsNotEmpty returns true if argument is not a zero value.

func IsSorted ΒΆ

func IsSorted[T constraints.Ordered](collection []T) bool

IsSorted checks if a slice is sorted. Play: https://go.dev/play/p/mc3qR-t4mcx

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

result := IsSorted(list)

fmt.Printf("%v", result)
Output:

true

func IsSortedByKey ΒΆ

func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(item T) K) bool

IsSortedByKey checks if a slice is sorted by iteratee. Play: https://go.dev/play/p/wiG6XyBBu49

Example ΒΆ
list := []string{"a", "bb", "ccc"}

result := IsSortedByKey(list, func(s string) int {
	return len(s)
})

fmt.Printf("%v", result)
Output:

true

func KeyBy ΒΆ

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

KeyBy transforms a slice or an array of structs to a map based on a pivot callback. Play: https://go.dev/play/p/mdaClUAT-zZ

Example ΒΆ
list := []string{"a", "aa", "aaa"}

result := KeyBy[int, string](list, func(str string) int {
	return len(str)
})

fmt.Printf("%v", result)
Output:

map[1:a 2:aa 3:aaa]

func Keys ΒΆ

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

Keys creates an array of the map keys. Play: https://go.dev/play/p/Uu11fHASqrU

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2}

result := Keys(kv)

sort.StringSlice(result).Sort()
fmt.Printf("%v", result)
Output:

[bar foo]

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

LastIndexOf 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(item T, index int) R) []R

Map manipulates a slice and transforms it to a slice of another type. Play: https://go.dev/play/p/OkPcYAhBo0D

Example ΒΆ
list := []int64{1, 2, 3, 4}

result := Map(list, func(nbr int64, index int) string {
	return strconv.FormatInt(nbr*2, 10)
})

fmt.Printf("%v", result)
Output:

[2 4 6 8]

func MapEntries ΒΆ

func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2

MapEntries manipulates a map entries and transforms it to a map of another type. Play: https://go.dev/play/p/VuvNQzxKimT

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2}

result := MapEntries(kv, func(k string, v int) (int, string) {
	return v, k
})

fmt.Printf("%v\n", result)
Output:

map[1:foo 2:bar]

func MapKeys ΒΆ

func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]V

MapKeys manipulates a map keys and transforms it to a map of another type. Play: https://go.dev/play/p/9_4WPIqOetJ

Example ΒΆ
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}

result := MapKeys(kv, func(_ int, v int) string {
	return strconv.FormatInt(int64(v), 10)
})

fmt.Printf("%v %v %v %v %v", len(result), result["1"], result["2"], result["3"], result["4"])
Output:

4 1 2 3 4

func MapToSlice ΒΆ

func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []R

MapToSlice transforms a map into a slice based on specific iteratee Play: https://go.dev/play/p/ZuiCZpDt6LD

Example ΒΆ
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}

result := MapToSlice(kv, func(k int, v int64) string {
	return fmt.Sprintf("%d_%d", k, v)
})

sort.StringSlice(result).Sort()
fmt.Printf("%v", result)
Output:

[1_1 2_2 3_3 4_4]

func MapValues ΒΆ

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

MapValues manipulates a map values and transforms it to a map of another type. Play: https://go.dev/play/p/T_8xAfvcf0W

Example ΒΆ
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}

result := MapValues(kv, func(_ int, v int) string {
	return strconv.FormatInt(int64(v), 10)
})

fmt.Printf("%v %v %v %v %v", len(result), result[1], result[2], result[3], result[4])
Output:

4 1 2 3 4

func Max ΒΆ

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

Max searches the maximum value of a collection. Returns zero value when collection is empty.

func MaxBy ΒΆ

func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T

MaxBy search the maximum value of a collection using the given comparison function. If several values of the collection are equal to the greatest value, returns the first such value. Returns zero value when collection is empty.

func Min ΒΆ

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

Min search the minimum value of a collection. Returns zero value when collection is empty.

func MinBy ΒΆ

func MinBy[T any](collection []T, comparison func(a T, b T) bool) T

MinBy search the minimum value of a collection using the given comparison function. If several values of the collection are equal to the smallest value, returns the first such value. Returns zero value when collection is empty.

func Must ΒΆ

func Must[T any](val T, err any, messageArgs ...interface{}) T

Must is a helper that wraps a call to a function returning a value and an error and panics if err is error or false. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must(42, nil)

// won't panic
cb := func() (int, error) {
	return 42, nil
}
Must(cb())

// will panic
Must(42, fmt.Errorf("my error"))

// will panic with error message
Must(42, fmt.Errorf("world"), "hello")
Output:

func Must0 ΒΆ

func Must0(err any, messageArgs ...interface{})

Must0 has the same behavior as Must, but callback returns no variable. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must0(nil)

// will panic
Must0(fmt.Errorf("my error"))

// will panic with error message
Must0(fmt.Errorf("world"), "hello")
Output:

func Must1 ΒΆ

func Must1[T any](val T, err any, messageArgs ...interface{}) T

Must1 is an alias to Must Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must1(42, nil)

// won't panic
cb := func() (int, error) {
	return 42, nil
}
Must1(cb())

// will panic
Must1(42, fmt.Errorf("my error"))

// will panic with error message
Must1(42, fmt.Errorf("world"), "hello")
Output:

func Must2 ΒΆ

func Must2[T1 any, T2 any](val1 T1, val2 T2, err any, messageArgs ...interface{}) (T1, T2)

Must2 has the same behavior as Must, but callback returns 2 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must2(42, "hello", nil)

// will panic
Must2(42, "hello", fmt.Errorf("my error"))

// will panic with error message
Must2(42, "hello", fmt.Errorf("world"), "hello")
Output:

func Must3 ΒΆ

func Must3[T1 any, T2 any, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...interface{}) (T1, T2, T3)

Must3 has the same behavior as Must, but callback returns 3 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must3(42, "hello", 4.2, nil)

// will panic
Must3(42, "hello", 4.2, fmt.Errorf("my error"))

// will panic with error message
Must3(42, "hello", 4.2, fmt.Errorf("world"), "hello")
Output:

func Must4 ΒΆ

func Must4[T1 any, T2 any, T3 any, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...interface{}) (T1, T2, T3, T4)

Must4 has the same behavior as Must, but callback returns 4 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must4(42, "hello", 4.2, true, nil)

// will panic
Must4(42, "hello", 4.2, true, fmt.Errorf("my error"))

// will panic with error message
Must4(42, "hello", 4.2, true, fmt.Errorf("world"), "hello")
Output:

func Must5 ΒΆ

func Must5[T1 any, T2 any, T3 any, T4 any, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...interface{}) (T1, T2, T3, T4, T5)

Must5 has the same behavior as Must, but callback returns 5 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must5(42, "hello", 4.2, true, foo{}, nil)

// will panic
Must5(42, "hello", 4.2, true, foo{}, fmt.Errorf("my error"))

// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, fmt.Errorf("world"), "hello")
Output:

func Must6 ΒΆ

func Must6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, messageArgs ...interface{}) (T1, T2, T3, T4, T5, T6)

Must6 has the same behavior as Must, but callback returns 6 variables. Play: https://go.dev/play/p/TMoWrRp3DyC

Example ΒΆ
defer func() {
	_ = recover()
}()

// won't panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", nil)

// will panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", fmt.Errorf("my error"))

// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, "foobar", fmt.Errorf("world"), "hello")
Output:

func NewDebounce ΒΆ

func NewDebounce(duration time.Duration, f ...func()) (func(), func())

NewDebounce creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed. Play: https://go.dev/play/p/mz32VMK2nqe

Example ΒΆ
i := 0
calls := []int{}
mu := sync.Mutex{}

debounce, cancel := NewDebounce(time.Millisecond, func() {
	mu.Lock()
	defer mu.Unlock()
	calls = append(calls, i)
})

debounce()
i++

time.Sleep(5 * time.Millisecond)

debounce()
i++
debounce()
i++
debounce()
i++

time.Sleep(5 * time.Millisecond)

cancel()

fmt.Printf("%v", calls)
Output:

[1 4]

func None ΒΆ

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

None returns true if no element of a subset are contained into a collection or if the subset is empty.

func NoneBy ΒΆ

func NoneBy[T any](collection []T, predicate func(item T) bool) bool

NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.

func Nth ΒΆ

func Nth[T any, N constraints.Integer](collection []T, nth N) (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 OmitBy ΒΆ

func OmitBy[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) map[K]V

OmitBy returns same map type filtered by given predicate. Play: https://go.dev/play/p/EtBsR43bdsd

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := OmitBy(kv, func(key string, value int) bool {
	return value%2 == 1
})

fmt.Printf("%v", result)
Output:

map[bar:2]

func OmitByKeys ΒΆ

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

OmitByKeys returns same map type filtered by given keys. Play: https://go.dev/play/p/t1QjCrs-ysk

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := OmitByKeys(kv, []string{"foo", "baz"})

fmt.Printf("%v", result)
Output:

map[bar:2]

func OmitByValues ΒΆ

func OmitByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V

OmitByValues returns same map type filtered by given values. Play: https://go.dev/play/p/9UYZi-hrs8j

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := OmitByValues(kv, []int{1, 3})

fmt.Printf("%v", result)
Output:

map[bar:2]

func Partial ΒΆ

func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R

Partial returns new function that, when called, has its first argument set to the provided value.

func PartitionBy ΒΆ

func PartitionBy[T any, K comparable](collection []T, iteratee func(item 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. Play: https://go.dev/play/p/NfQ_nGjkgXW

Example ΒΆ
list := []int{-2, -1, 0, 1, 2, 3, 4}

result := PartitionBy(list, func(x int) string {
	if x < 0 {
		return "negative"
	} else if x%2 == 0 {
		return "even"
	}
	return "odd"
})

for _, item := range result {
	fmt.Printf("%v\n", item)
}
Output:

[-2 -1]
[0 2 4]
[1 3]

func PickBy ΒΆ

func PickBy[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) map[K]V

PickBy returns same map type filtered by given predicate. Play: https://go.dev/play/p/kdg8GR_QMmf

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := PickBy(kv, func(key string, value int) bool {
	return value%2 == 1
})

fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output:

2 1 3

func PickByKeys ΒΆ

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

PickByKeys returns same map type filtered by given keys. Play: https://go.dev/play/p/R1imbuci9qU

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := PickByKeys(kv, []string{"foo", "baz"})

fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output:

2 1 3

func PickByValues ΒΆ

func PickByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V

PickByValues returns same map type filtered by given values. Play: https://go.dev/play/p/1zdzSvbfsJc

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := PickByValues(kv, []int{1, 3})

fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output:

2 1 3

func Range ΒΆ

func Range(elementNum int) []int

Range creates an array of numbers (positive and/or negative) with given length. Play: https://go.dev/play/p/0r6VimXAi9H

Example ΒΆ
result1 := Range(4)
result2 := Range(-4)
result3 := RangeFrom(1, 5)
result4 := RangeFrom(1.0, 5)
result5 := RangeWithSteps(0, 20, 5)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
result7 := RangeWithSteps(1, 4, -1)
result8 := Range(0)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
fmt.Printf("%v\n", result7)
fmt.Printf("%v\n", result8)
Output:

[0 1 2 3]
[0 -1 -2 -3]
[1 2 3 4 5]
[1 2 3 4 5]
[0 5 10 15]
[-1 -2 -3]
[]
[]

func RangeFrom ΒΆ

func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []T

RangeFrom creates an array of numbers from start with specified length. Play: https://go.dev/play/p/0r6VimXAi9H

func RangeWithSteps ΒΆ

func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T

RangeWithSteps creates an array of numbers (positive and/or negative) progressing from start up to, but not including end. step set to zero will return empty array. Play: https://go.dev/play/p/0r6VimXAi9H

func Reduce ΒΆ

func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index 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. Play: https://go.dev/play/p/R4UHXZNaaUG

Example ΒΆ
list := []int64{1, 2, 3, 4}

result := Reduce(list, func(agg int64, item int64, index int) int64 {
	return agg + item
}, 0)

fmt.Printf("%v", result)
Output:

10

func ReduceRight ΒΆ

func ReduceRight[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R

ReduceRight helper is like Reduce except that it iterates over elements of collection from right to left. Play: https://go.dev/play/p/Fq3W70l7wXF

Example ΒΆ
list := [][]int{{0, 1}, {2, 3}, {4, 5}}

result := ReduceRight(list, func(agg []int, item []int, index int) []int {
	return append(agg, item...)
}, []int{})

fmt.Printf("%v", result)
Output:

[4 5 2 3 0 1]

func Reject ΒΆ

func Reject[V any](collection []V, predicate func(item V, index int) bool) []V

Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return truthy for. Play: https://go.dev/play/p/YkLMODy1WEL

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := Reject(list, func(x int, _ int) bool {
	return x%2 == 0
})

fmt.Printf("%v", result)
Output:

[1 3 5]

func Repeat ΒΆ

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

Repeat builds a slice with N copies of initial value. Play: https://go.dev/play/p/g3uHXbmc3b6

Example ΒΆ
result := Repeat[foo](2, foo{"a"})

fmt.Printf("%v", result)
Output:

[{a} {a}]

func RepeatBy ΒΆ

func RepeatBy[T any](count int, predicate func(index int) T) []T

RepeatBy builds a slice with values returned by N calls of callback. Play: https://go.dev/play/p/ozZLCtX_hNU

Example ΒΆ
result := RepeatBy[string](5, func(i int) string {
	return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})

fmt.Printf("%v", result)
Output:

[0 1 4 9 16]

func Replace ΒΆ

func Replace[T comparable](collection []T, old T, new T, n int) []T

Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new. Play: https://go.dev/play/p/XfPzmf9gql6

Example ΒΆ
list := []int{0, 1, 0, 1, 2, 3, 0}

result := Replace(list, 0, 42, 1)
fmt.Printf("%v\n", result)

result = Replace(list, -1, 42, 1)
fmt.Printf("%v\n", result)

result = Replace(list, 0, 42, 2)
fmt.Printf("%v\n", result)

result = Replace(list, 0, 42, -1)
fmt.Printf("%v\n", result)
Output:

[42 1 0 1 2 3 0]
[0 1 0 1 2 3 0]
[42 1 42 1 2 3 0]
[42 1 42 1 2 3 42]

func ReplaceAll ΒΆ

func ReplaceAll[T comparable](collection []T, old T, new T) []T

ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new. Play: https://go.dev/play/p/a9xZFUHfYcV

Example ΒΆ
list := []string{"", "foo", "", "bar", ""}

result := Compact(list)

fmt.Printf("%v", result)
Output:

[foo bar]

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. Play: https://go.dev/play/p/fhUMLvZ7vS6

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := Reverse(list)

fmt.Printf("%v", result)
Output:

[5 4 3 2 1 0]

func RuneLength ΒΆ

func RuneLength(str string) int

RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string. Play: https://go.dev/play/p/tuhgW_lWY8l

Example ΒΆ
result1, chars1 := RuneLength("hellΓ΄"), len("hellΓ΄")
result2, chars2 := RuneLength("🀘"), len("🀘")

fmt.Printf("%v %v\n", result1, chars1)
fmt.Printf("%v %v\n", result2, chars2)
Output:

5 6
1 4

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. Play: https://go.dev/play/p/Qp73bnTDnc7

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := Shuffle(list)

fmt.Printf("%v", result)
Output:

func Slice ΒΆ

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

Slice returns a copy of a slice from `start` up to, but not including `end`. Like `slice[start:end]`, but does not panic on overflow. Play: https://go.dev/play/p/8XWYhfMMA1h

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := Slice(list, 1, 4)
fmt.Printf("%v\n", result)

result = Slice(list, 4, 1)
fmt.Printf("%v\n", result)

result = Slice(list, 4, 5)
fmt.Printf("%v\n", result)
Output:

[1 2 3]
[]
[4]

func SliceToChannel ΒΆ

func SliceToChannel[T any](bufferSize int, collection []T) <-chan T

SliceToChannel returns a read-only channels of collection elements.

func SliceToMap ΒΆ

func SliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V

SliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs would have the same key the last one gets added to the map. The order of keys in returned map is not specified and is not guaranteed to be the same from the original array. Alias of Associate(). Play: https://go.dev/play/p/WHa2CfMO3Lr

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. If the subset is empty Some returns false.

func SomeBy ΒΆ

func SomeBy[T any](collection []T, predicate func(item T) bool) bool

SomeBy returns true if the predicate returns true for any of the elements in the collection. If the collection is empty SomeBy returns false.

func Subset ΒΆ

func Subset[T any](collection []T, offset int, length uint) []T

Subset returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow. Play: https://go.dev/play/p/tOQu1GhFcog

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := Subset(list, 2, 3)

fmt.Printf("%v", result)
Output:

[2 3 4]

func Substring ΒΆ

func Substring[T ~string](str T, offset int, length uint) T

Substring return part of a string. Play: https://go.dev/play/p/TQlxQi82Lu1

Example ΒΆ
result1 := Substring("hello", 2, 3)
result2 := Substring("hello", -4, 3)
result3 := Substring("hello", -2, math.MaxUint)

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
Output:

llo
ell
lo

func Sum ΒΆ

func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T

Sum sums the values in a collection. If collection is empty 0 is returned. Play: https://go.dev/play/p/upfeJVqs4Bt

Example ΒΆ
list := []int{1, 2, 3, 4, 5}

sum := Sum(list)

fmt.Printf("%v", sum)
Output:

15

func SumBy ΒΆ

func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R

SumBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned. Play: https://go.dev/play/p/Dz_a_7jN_ca

Example ΒΆ
list := []string{"foo", "bar"}

result := SumBy(list, func(item string) int {
	return len(item)
})

fmt.Printf("%v", result)
Output:

6

func Switch ΒΆ

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

Switch is a pure functional switch/case/default statement. Play: https://go.dev/play/p/TGbKUMAeRUd

Example ΒΆ
result1 := Switch[int, string](1).
	Case(1, "1").
	Case(2, "2").
	Default("3")

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

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

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

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

result6 := Switch[int, string](42).
	CaseF(1, func() string { return "1" }).
	CaseF(2, func() string { return "2" }).
	DefaultF(func() string { return "3" })

fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output:

1
2
3
1
2
3

func Synchronize ΒΆ

func Synchronize(opt ...sync.Locker) *synchronize

Synchronize wraps the underlying callback in a mutex. It receives an optional mutex.

func Ternary ΒΆ

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

Ternary is a 1 line if/else statement. Play: https://go.dev/play/p/t-D7WBL44h2

Example ΒΆ
result := Ternary(true, "a", "b")

fmt.Printf("%v", result)
Output:

a

func TernaryF ΒΆ

func TernaryF[T any](condition bool, ifFunc func() T, elseFunc func() T) T

TernaryF is a 1 line if/else statement whose options are functions Play: https://go.dev/play/p/AO4VW20JoqM

Example ΒΆ
result := TernaryF(true, func() string { return "a" }, func() string { return "b" })

fmt.Printf("%v", result)
Output:

a

func Times ΒΆ

func Times[T any](count int, iteratee func(index 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. Play: https://go.dev/play/p/vgQj3Glr6lT

Example ΒΆ
result := Times(3, func(i int) string {
	return strconv.FormatInt(int64(i), 10)
})

fmt.Printf("%v", result)
Output:

[0 1 2]

func ToAnySlice ΒΆ

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

ToAnySlice returns a slice with all elements mapped to `any` type

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

ToSlicePtr returns a slice of pointer copy of value.

func Try ΒΆ

func Try(callback func() error) (ok bool)

Try calls the function and return false in case of error.

Example ΒΆ
ok1 := Try(func() error {
	return nil
})
ok2 := Try(func() error {
	return fmt.Errorf("my error")
})
ok3 := Try(func() error {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try0 ΒΆ

func Try0(callback func()) bool

Try0 has the same behavior as Try, but callback returns no variable. Play: https://go.dev/play/p/mTyyWUvn9u4

func Try1 ΒΆ

func Try1(callback func() error) bool

Try1 is an alias to Try. Play: https://go.dev/play/p/mTyyWUvn9u4

Example ΒΆ
ok1 := Try1(func() error {
	return nil
})
ok2 := Try1(func() error {
	return fmt.Errorf("my error")
})
ok3 := Try1(func() error {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try2 ΒΆ

func Try2[T any](callback func() (T, error)) bool

Try2 has the same behavior as Try, but callback returns 2 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example ΒΆ
ok1 := Try2(func() (int, error) {
	return 42, nil
})
ok2 := Try2(func() (int, error) {
	return 42, fmt.Errorf("my error")
})
ok3 := Try2(func() (int, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try3 ΒΆ

func Try3[T, R any](callback func() (T, R, error)) bool

Try3 has the same behavior as Try, but callback returns 3 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example ΒΆ
ok1 := Try3(func() (int, string, error) {
	return 42, "foobar", nil
})
ok2 := Try3(func() (int, string, error) {
	return 42, "foobar", fmt.Errorf("my error")
})
ok3 := Try3(func() (int, string, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try4 ΒΆ

func Try4[T, R, S any](callback func() (T, R, S, error)) bool

Try4 has the same behavior as Try, but callback returns 4 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example ΒΆ
ok1 := Try4(func() (int, string, float64, error) {
	return 42, "foobar", 4.2, nil
})
ok2 := Try4(func() (int, string, float64, error) {
	return 42, "foobar", 4.2, fmt.Errorf("my error")
})
ok3 := Try4(func() (int, string, float64, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try5 ΒΆ

func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool

Try5 has the same behavior as Try, but callback returns 5 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example ΒΆ
ok1 := Try5(func() (int, string, float64, bool, error) {
	return 42, "foobar", 4.2, true, nil
})
ok2 := Try5(func() (int, string, float64, bool, error) {
	return 42, "foobar", 4.2, true, fmt.Errorf("my error")
})
ok3 := Try5(func() (int, string, float64, bool, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func Try6 ΒΆ

func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool

Try6 has the same behavior as Try, but callback returns 6 variables. Play: https://go.dev/play/p/mTyyWUvn9u4

Example ΒΆ
ok1 := Try6(func() (int, string, float64, bool, foo, error) {
	return 42, "foobar", 4.2, true, foo{}, nil
})
ok2 := Try6(func() (int, string, float64, bool, foo, error) {
	return 42, "foobar", 4.2, true, foo{}, fmt.Errorf("my error")
})
ok3 := Try6(func() (int, string, float64, bool, foo, error) {
	panic("my error")
})

fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output:

true
false
false

func TryCatch ΒΆ

func TryCatch(callback func() error, catch func())

TryCatch has the same behavior as Try, but calls the catch function in case of error. Play: https://go.dev/play/p/PnOON-EqBiU

func TryCatchWithErrorValue ΒΆ

func TryCatchWithErrorValue(callback func() error, catch func(any))

TryCatchWithErrorValue has the same behavior as TryWithErrorValue, but calls the catch function in case of error. Play: https://go.dev/play/p/8Pc9gwX_GZO

Example ΒΆ
TryCatchWithErrorValue(
	func() error {
		panic("trigger an error")
	},
	func(err any) {
		fmt.Printf("catch: %s", err)
	},
)
Output:

catch: trigger an error

func TryOr ΒΆ

func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool)

TryOr has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, ok1 := TryOr(func() (int, error) {
	return 42, nil
}, 21)
value2, ok2 := TryOr(func() (int, error) {
	return 42, fmt.Errorf("my error")
}, 21)
value3, ok3 := TryOr(func() (int, error) {
	panic("my error")
}, 21)

fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
Output:

42 true
21 false
21 false

func TryOr1 ΒΆ

func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool)

TryOr1 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, ok1 := TryOr1(func() (int, error) {
	return 42, nil
}, 21)
value2, ok2 := TryOr1(func() (int, error) {
	return 42, fmt.Errorf("my error")
}, 21)
value3, ok3 := TryOr1(func() (int, error) {
	panic("my error")
}, 21)

fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
Output:

42 true
21 false
21 false

func TryOr2 ΒΆ

func TryOr2[A any, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool)

TryOr2 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, value2, ok3 := TryOr2(func() (int, string, error) {
	panic("my error")
}, 21, "hello")

fmt.Printf("%v %v %v\n", value1, value2, ok3)
Output:

21 hello false

func TryOr3 ΒΆ

func TryOr3[A any, B any, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool)

TryOr3 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, value2, value3, ok3 := TryOr3(func() (int, string, bool, error) {
	panic("my error")
}, 21, "hello", false)

fmt.Printf("%v %v %v %v\n", value1, value2, value3, ok3)
Output:

21 hello false false

func TryOr4 ΒΆ

func TryOr4[A any, B any, C any, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool)

TryOr4 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, value2, value3, value4, ok3 := TryOr4(func() (int, string, bool, foo, error) {
	panic("my error")
}, 21, "hello", false, foo{bar: "bar"})

fmt.Printf("%v %v %v %v %v\n", value1, value2, value3, value4, ok3)
Output:

21 hello false {bar} false

func TryOr5 ΒΆ

func TryOr5[A any, B any, C any, D any, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool)

TryOr5 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, value2, value3, value4, value5, ok3 := TryOr5(func() (int, string, bool, foo, float64, error) {
	panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2)

fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
Output:

21 hello false {bar} 4.2 false

func TryOr6 ΒΆ

func TryOr6[A any, B any, C any, D any, E any, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool)

TryOr6 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X

Example ΒΆ
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
	panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2, "world")

fmt.Printf("%v %v %v %v %v %v %v\n", value1, value2, value3, value4, value5, value6, ok3)
Output:

21 hello false {bar} 4.2 world false

func TryWithErrorValue ΒΆ

func TryWithErrorValue(callback func() error) (errorValue any, ok bool)

TryWithErrorValue has the same behavior as Try, but also returns value passed to panic. Play: https://go.dev/play/p/Kc7afQIT2Fs

Example ΒΆ
err1, ok1 := TryWithErrorValue(func() error {
	return nil
})
err2, ok2 := TryWithErrorValue(func() error {
	return fmt.Errorf("my error")
})
err3, ok3 := TryWithErrorValue(func() error {
	panic("my error")
})

fmt.Printf("%v %v\n", err1, ok1)
fmt.Printf("%v %v\n", err2, ok2)
fmt.Printf("%v %v\n", err3, ok3)
Output:

<nil> true
my error false
my error false

func Union ΒΆ

func Union[T comparable](lists ...[]T) []T

Union returns all distinct elements from given collections. result returns will not change the order of elements relatively.

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. Play: https://go.dev/play/p/DTzbeXZ6iEN

Example ΒΆ
list := []int{1, 2, 2, 1}

result := Uniq(list)

fmt.Printf("%v", result)
Output:

[1 2]

func UniqBy ΒΆ

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

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. Play: https://go.dev/play/p/g42Z3QSb53u

Example ΒΆ
list := []int{0, 1, 2, 3, 4, 5}

result := UniqBy(list, func(i int) int {
	return i % 3
})

fmt.Printf("%v", result)
Output:

[0 1 2]

func Unpack2 ΒΆ

func Unpack2[A any, B any](tuple Tuple2[A, B]) (A, B)

Unpack2 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b := Unpack2(T2("hello", 2))
fmt.Printf("%v %v", a, b)
Output:

hello 2

func Unpack3 ΒΆ

func Unpack3[A any, B any, C any](tuple Tuple3[A, B, C]) (A, B, C)

Unpack3 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c := Unpack3(T3("hello", 2, true))
fmt.Printf("%v %v %v", a, b, c)
Output:

hello 2 true

func Unpack4 ΒΆ

func Unpack4[A any, B any, C any, D any](tuple Tuple4[A, B, C, D]) (A, B, C, D)

Unpack4 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c, d := Unpack4(T4("hello", 2, true, foo{bar: "bar"}))
fmt.Printf("%v %v %v %v", a, b, c, d)
Output:

hello 2 true {bar}

func Unpack5 ΒΆ

func Unpack5[A any, B any, C any, D any, E any](tuple Tuple5[A, B, C, D, E]) (A, B, C, D, E)

Unpack5 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c, d, e := Unpack5(T5("hello", 2, true, foo{bar: "bar"}, 4.2))
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
Output:

hello 2 true {bar} 4.2

func Unpack6 ΒΆ

func Unpack6[A any, B any, C any, D any, E any, F any](tuple Tuple6[A, B, C, D, E, F]) (A, B, C, D, E, F)

Unpack6 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c, d, e, f := Unpack6(T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop"))
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
Output:

hello 2 true {bar} 4.2 plop

func Unpack7 ΒΆ

func Unpack7[A any, B any, C any, D any, E any, F any, G any](tuple Tuple7[A, B, C, D, E, F, G]) (A, B, C, D, E, F, G)

Unpack7 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c, d, e, f, g := Unpack7(T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false))
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
Output:

hello 2 true {bar} 4.2 plop false

func Unpack8 ΒΆ

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

Unpack8 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c, d, e, f, g, h := Unpack8(T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42))
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
Output:

hello 2 true {bar} 4.2 plop false 42

func Unpack9 ΒΆ

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

Unpack9 returns values contained in tuple. Play: https://go.dev/play/p/xVP_k0kJ96W

Example ΒΆ
a, b, c, d, e, f, g, h, i := Unpack9(T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world"))
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
Output:

hello 2 true {bar} 4.2 plop false 42 hello world

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b := Unzip2([]Tuple2[string, int]{T2("hello", 2)})
fmt.Printf("%v %v", a, b)
Output:

[hello] [2]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c := Unzip3([]Tuple3[string, int, bool]{T3("hello", 2, true)})
fmt.Printf("%v %v %v", a, b, c)
Output:

[hello] [2] [true]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c, d := Unzip4([]Tuple4[string, int, bool, foo]{T4("hello", 2, true, foo{bar: "bar"})})
fmt.Printf("%v %v %v %v", a, b, c, d)
Output:

[hello] [2] [true] [{bar}]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c, d, e := Unzip5([]Tuple5[string, int, bool, foo, float64]{T5("hello", 2, true, foo{bar: "bar"}, 4.2)})
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
Output:

[hello] [2] [true] [{bar}] [4.2]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c, d, e, f := Unzip6([]Tuple6[string, int, bool, foo, float64, string]{T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")})
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c, d, e, f, g := Unzip7([]Tuple7[string, int, bool, foo, float64, string, bool]{T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)})
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop] [false]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c, d, e, f, g, h := Unzip8([]Tuple8[string, int, bool, foo, float64, string, bool, int]{T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)})
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop] [false] [42]

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. Play: https://go.dev/play/p/ciHugugvaAW

Example ΒΆ
a, b, c, d, e, f, g, h, i := Unzip9([]Tuple9[string, int, bool, foo, float64, string, bool, int, string]{T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")})
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
Output:

[hello] [2] [true] [{bar}] [4.2] [plop] [false] [42] [hello world]

func Validate ΒΆ

func Validate(ok bool, format string, args ...any) error

Validate is a helper that creates an error when a condition is not met. Play: https://go.dev/play/p/vPyh51XpCBt

Example ΒΆ
i := 42

err1 := Validate(i < 0, "expected %d < 0", i)
err2 := Validate(i > 0, "expected %d > 0", i)

fmt.Printf("%v\n%v", err1, err2)
Output:

expected 42 < 0
<nil>

func Values ΒΆ

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

Values creates an array of the map values. Play: https://go.dev/play/p/nnRTQkzQfF6

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2}

result := Values(kv)

sort.IntSlice(result).Sort()
fmt.Printf("%v", result)
Output:

[1 2]

func Without ΒΆ

func Without[T comparable](collection []T, exclude ...T) []T

Without returns slice excluding all given values.

func WithoutEmpty ΒΆ

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

WithoutEmpty returns slice excluding empty values.

Types ΒΆ

type Clonable ΒΆ

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

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

type DispatchingStrategy ΒΆ

type DispatchingStrategy[T any] func(msg T, index uint64, channels []<-chan T) int

func DispatchingStrategyWeightedRandom ΒΆ

func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy[T]

DispatchingStrategyWeightedRandom distributes messages in a weighted manner. If the channel capacity is exceeded, another random channel will be selected and so on.

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. Play:

Example ΒΆ
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}

result := Entries(kv)

sort.Slice(result, func(i, j int) bool {
	return strings.Compare(result[i].Key, result[j].Key) < 0
})
fmt.Printf("%v", result)
Output:

[{bar 2} {baz 3} {foo 1}]

func ToPairs ΒΆ

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

ToPairs transforms a map into array of key/value pairs. Alias of Entries(). Play: https://go.dev/play/p/3Dhgx46gawJ

type Tuple2 ΒΆ

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

Tuple2 is a group of 2 elements (pair).

func T2 ΒΆ

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

T2 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T2("hello", 2)
fmt.Printf("%v %v", result.A, result.B)
Output:

hello 2

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip2([]string{"hello"}, []int{2})
fmt.Printf("%v", result)
Output:

[{hello 2}]

func (Tuple2[A, B]) Unpack ΒΆ

func (t Tuple2[A, B]) Unpack() (A, B)

Unpack returns values contained in tuple.

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 T3 ΒΆ

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

T3 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T3("hello", 2, true)
fmt.Printf("%v %v %v", result.A, result.B, result.C)
Output:

hello 2 true

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip3([]string{"hello"}, []int{2}, []bool{true})
fmt.Printf("%v", result)
Output:

[{hello 2 true}]

func (Tuple3[A, B, C]) Unpack ΒΆ

func (t Tuple3[A, B, C]) Unpack() (A, B, C)

Unpack returns values contained in tuple.

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 T4 ΒΆ

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

T4 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T4("hello", 2, true, foo{bar: "bar"})
fmt.Printf("%v %v %v %v", result.A, result.B, result.C, result.D)
Output:

hello 2 true {bar}

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip4([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar}}]

func (Tuple4[A, B, C, D]) Unpack ΒΆ

func (t Tuple4[A, B, C, D]) Unpack() (A, B, C, D)

Unpack returns values contained in tuple.

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 T5 ΒΆ

func T5[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]

T5 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T5("hello", 2, true, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v", result.A, result.B, result.C, result.D, result.E)
Output:

hello 2 true {bar} 4.2

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip5([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2}]

func (Tuple5[A, B, C, D, E]) Unpack ΒΆ

func (t Tuple5[A, B, C, D, E]) Unpack() (A, B, C, D, E)

Unpack returns values contained in tuple.

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 T6 ΒΆ

func T6[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]

T6 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")
fmt.Printf("%v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F)
Output:

hello 2 true {bar} 4.2 plop

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip6([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop}]

func (Tuple6[A, B, C, D, E, F]) Unpack ΒΆ

func (t Tuple6[A, B, C, D, E, F]) Unpack() (A, B, C, D, E, F)

Unpack returns values contained in tuple.

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 T7 ΒΆ

func T7[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]

T7 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)
fmt.Printf("%v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G)
Output:

hello 2 true {bar} 4.2 plop false

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip7([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop false}]

func (Tuple7[A, B, C, D, E, F, G]) Unpack ΒΆ

func (t Tuple7[A, B, C, D, E, F, G]) Unpack() (A, B, C, D, E, F, G)

Unpack returns values contained in tuple.

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 T8 ΒΆ

func T8[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]

T8 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)
fmt.Printf("%v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H)
Output:

hello 2 true {bar} 4.2 plop false 42

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip8([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop false 42}]

func (Tuple8[A, B, C, D, E, F, G, H]) Unpack ΒΆ

func (t Tuple8[A, B, C, D, E, F, G, H]) Unpack() (A, B, C, D, E, F, G, H)

Unpack returns values contained in tuple.

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 T9 ΒΆ

func T9[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]

T9 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm

Example ΒΆ
result := T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")
fmt.Printf("%v %v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H, result.I)
Output:

hello 2 true {bar} 4.2 plop false 42 hello world

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. Play: https://go.dev/play/p/jujaA6GaJTp

Example ΒΆ
result := Zip9([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
fmt.Printf("%v", result)
Output:

[{hello 2 true {bar} 4.2 plop false 42 hello world}]

func (Tuple9[A, B, C, D, E, F, G, H, I]) Unpack ΒΆ

func (t Tuple9[A, B, C, D, E, F, G, H, I]) Unpack() (A, B, C, D, E, F, G, H, I)

Unpack returns values contained in tuple.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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