From dba0edb9987b16738fb4b1c07e681388a84ce52d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 9 Oct 2022 08:31:14 -0700 Subject: use boxlayout from lazycore --- vendor/github.com/jesseduffield/lazycore/LICENSE | 21 + .../lazycore/pkg/boxlayout/boxlayout.go | 212 ++ .../jesseduffield/lazycore/pkg/utils/utils.go | 16 + vendor/github.com/samber/lo/.gitignore | 2 + vendor/github.com/samber/lo/CHANGELOG.md | 276 +++ vendor/github.com/samber/lo/Dockerfile | 4 +- vendor/github.com/samber/lo/Makefile | 15 +- vendor/github.com/samber/lo/README.md | 2259 +++++++++++++++++--- vendor/github.com/samber/lo/channel.go | 228 ++ vendor/github.com/samber/lo/concurrency.go | 95 + vendor/github.com/samber/lo/condition.go | 51 + vendor/github.com/samber/lo/docker-compose.yml | 4 +- vendor/github.com/samber/lo/drop.go | 65 - vendor/github.com/samber/lo/errors.go | 354 +++ vendor/github.com/samber/lo/find.go | 247 ++- vendor/github.com/samber/lo/func.go | 8 + vendor/github.com/samber/lo/intersect.go | 73 +- vendor/github.com/samber/lo/map.go | 134 +- vendor/github.com/samber/lo/math.go | 74 + vendor/github.com/samber/lo/pointers.go | 19 - vendor/github.com/samber/lo/retry.go | 89 +- vendor/github.com/samber/lo/slice.go | 317 ++- vendor/github.com/samber/lo/string.go | 65 + vendor/github.com/samber/lo/test.go | 32 + vendor/github.com/samber/lo/tuples.go | 220 +- vendor/github.com/samber/lo/type_manipulation.go | 88 + vendor/github.com/samber/lo/util.go | 50 - .../stretchr/testify/assert/assertion_compare.go | 76 +- .../assert/assertion_compare_can_convert.go | 16 + .../testify/assert/assertion_compare_legacy.go | 16 + .../stretchr/testify/assert/assertion_format.go | 22 + .../stretchr/testify/assert/assertion_forward.go | 44 + .../stretchr/testify/assert/assertion_order.go | 8 +- .../stretchr/testify/assert/assertions.go | 190 +- vendor/gopkg.in/yaml.v3/decode.go | 78 +- vendor/gopkg.in/yaml.v3/parserc.go | 11 +- vendor/modules.txt | 10 +- 37 files changed, 4814 insertions(+), 675 deletions(-) create mode 100644 vendor/github.com/jesseduffield/lazycore/LICENSE create mode 100644 vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go create mode 100644 vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go create mode 100644 vendor/github.com/samber/lo/channel.go create mode 100644 vendor/github.com/samber/lo/concurrency.go delete mode 100644 vendor/github.com/samber/lo/drop.go create mode 100644 vendor/github.com/samber/lo/errors.go create mode 100644 vendor/github.com/samber/lo/func.go create mode 100644 vendor/github.com/samber/lo/math.go delete mode 100644 vendor/github.com/samber/lo/pointers.go create mode 100644 vendor/github.com/samber/lo/string.go create mode 100644 vendor/github.com/samber/lo/test.go create mode 100644 vendor/github.com/samber/lo/type_manipulation.go delete mode 100644 vendor/github.com/samber/lo/util.go create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_compare_can_convert.go create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_compare_legacy.go (limited to 'vendor') diff --git a/vendor/github.com/jesseduffield/lazycore/LICENSE b/vendor/github.com/jesseduffield/lazycore/LICENSE new file mode 100644 index 000000000..2a7175dcc --- /dev/null +++ b/vendor/github.com/jesseduffield/lazycore/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Jesse Duffield + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go b/vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go new file mode 100644 index 000000000..7cacfff98 --- /dev/null +++ b/vendor/github.com/jesseduffield/lazycore/pkg/boxlayout/boxlayout.go @@ -0,0 +1,212 @@ +package boxlayout + +import ( + "github.com/jesseduffield/generics/slices" + "github.com/jesseduffield/lazycore/pkg/utils" + "github.com/samber/lo" +) + +type Dimensions struct { + X0 int + X1 int + Y0 int + Y1 int +} + +type Direction int + +const ( + ROW Direction = iota + COLUMN +) + +// to give a high-level explanation of what's going on here. We layout our windows by arranging a bunch of boxes in the available space. +// If a box has children, it needs to specify how it wants to arrange those children: ROW or COLUMN. +// If a box represents a window, you can put the window name in the Window field. +// When determining how to divvy-up the available height (for row children) or width (for column children), we first +// give the boxes with a static `size` the space that they want. Then we apportion +// the remaining space based on the weights of the dynamic boxes (you can't define +// both size and weight at the same time: you gotta pick one). If there are two +// boxes, one with weight 1 and the other with weight 2, the first one gets 33% +// of the available space and the second one gets the remaining 66% + +type Box struct { + // Direction decides how the children boxes are laid out. ROW means the children will each form a row i.e. that they will be stacked on top of eachother. + Direction Direction + + // function which takes the width and height assigned to the box and decides which orientation it will have + ConditionalDirection func(width int, height int) Direction + + Children []*Box + + // function which takes the width and height assigned to the box and decides the layout of the children. + ConditionalChildren func(width int, height int) []*Box + + // Window refers to the name of the window this box represents, if there is one + Window string + + // static Size. If parent box's direction is ROW this refers to height, otherwise width + Size int + + // dynamic size. Once all statically sized children have been considered, Weight decides how much of the remaining space will be taken up by the box + // TODO: consider making there be one int and a type enum so we can't have size and Weight simultaneously defined + Weight int +} + +func ArrangeWindows(root *Box, x0, y0, width, height int) map[string]Dimensions { + children := root.getChildren(width, height) + if len(children) == 0 { + // leaf node + if root.Window != "" { + dimensionsForWindow := Dimensions{X0: x0, Y0: y0, X1: x0 + width - 1, Y1: y0 + height - 1} + return map[string]Dimensions{root.Window: dimensionsForWindow} + } + return map[string]Dimensions{} + } + + direction := root.getDirection(width, height) + + var availableSize int + if direction == COLUMN { + availableSize = width + } else { + availableSize = height + } + + sizes := calcSizes(children, availableSize) + + result := map[string]Dimensions{} + offset := 0 + for i, child := range children { + boxSize := sizes[i] + + var resultForChild map[string]Dimensions + if direction == COLUMN { + resultForChild = ArrangeWindows(child, x0+offset, y0, boxSize, height) + } else { + resultForChild = ArrangeWindows(child, x0, y0+offset, width, boxSize) + } + + result = mergeDimensionMaps(result, resultForChild) + offset += boxSize + } + + return result +} + +func calcSizes(boxes []*Box, availableSpace int) []int { + normalizedWeights := normalizeWeights(slices.Map(boxes, func(box *Box) int { return box.Weight })) + + totalWeight := 0 + reservedSpace := 0 + for i, box := range boxes { + if box.isStatic() { + reservedSpace += box.Size + } else { + totalWeight += normalizedWeights[i] + } + } + + dynamicSpace := utils.Max(0, availableSpace-reservedSpace) + + unitSize := 0 + extraSpace := 0 + if totalWeight > 0 { + unitSize = dynamicSpace / totalWeight + extraSpace = dynamicSpace % totalWeight + } + + result := make([]int, len(boxes)) + for i, box := range boxes { + if box.isStatic() { + // assuming that only one static child can have a size greater than the + // available space. In that case we just crop the size to what's available + result[i] = utils.Min(availableSpace, box.Size) + } else { + result[i] = unitSize * normalizedWeights[i] + } + } + + // distribute the remainder across dynamic boxes. + for extraSpace > 0 { + for i, weight := range normalizedWeights { + if weight > 0 { + result[i]++ + extraSpace-- + normalizedWeights[i]-- + + if extraSpace == 0 { + break + } + } + } + } + + return result +} + +// removes common multiple from weights e.g. if we get 2, 4, 4 we return 1, 2, 2. +func normalizeWeights(weights []int) []int { + if len(weights) == 0 { + return []int{} + } + + // to spare us some computation we'll exit early if any of our weights is 1 + if slices.Some(weights, func(weight int) bool { return weight == 1 }) { + return weights + } + + // map weights to factorSlices and find the lowest common factor + positiveWeights := slices.Filter(weights, func(weight int) bool { return weight > 0 }) + factorSlices := slices.Map(positiveWeights, func(weight int) []int { return calcFactors(weight) }) + commonFactors := factorSlices[0] + for _, factors := range factorSlices { + commonFactors = lo.Intersect(commonFactors, factors) + } + + if len(commonFactors) == 0 { + return weights + } + + newWeights := slices.Map(weights, func(weight int) int { return weight / commonFactors[0] }) + + return normalizeWeights(newWeights) +} + +func calcFactors(n int) []int { + factors := []int{} + for i := 2; i <= n; i++ { + if n%i == 0 { + factors = append(factors, i) + } + } + return factors +} + +func (b *Box) isStatic() bool { + return b.Size > 0 +} + +func (b *Box) getDirection(width int, height int) Direction { + if b.ConditionalDirection != nil { + return b.ConditionalDirection(width, height) + } + return b.Direction +} + +func (b *Box) getChildren(width int, height int) []*Box { + if b.ConditionalChildren != nil { + return b.ConditionalChildren(width, height) + } + return b.Children +} + +func mergeDimensionMaps(a map[string]Dimensions, b map[string]Dimensions) map[string]Dimensions { + result := map[string]Dimensions{} + for _, dimensionMap := range []map[string]Dimensions{a, b} { + for k, v := range dimensionMap { + result[k] = v + } + } + return result +} diff --git a/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go b/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go new file mode 100644 index 000000000..9fa933762 --- /dev/null +++ b/vendor/github.com/jesseduffield/lazycore/pkg/utils/utils.go @@ -0,0 +1,16 @@ +package utils + +// Min returns the minimum of two integers +func Min(x, y int) int { + if x < y { + return x + } + return y +} + +func Max(x, y int) int { + if x > y { + return x + } + return y +} diff --git a/vendor/github.com/samber/lo/.gitignore b/vendor/github.com/samber/lo/.gitignore index 3aa3a0ad4..e5ecc5c40 100644 --- a/vendor/github.com/samber/lo/.gitignore +++ b/vendor/github.com/samber/lo/.gitignore @@ -34,3 +34,5 @@ go.work cover.out cover.html .vscode + +.idea/ diff --git a/vendor/github.com/samber/lo/CHANGELOG.md b/vendor/github.com/samber/lo/CHANGELOG.md index aabeed120..9ba32f1b6 100644 --- a/vendor/github.com/samber/lo/CHANGELOG.md +++ b/vendor/github.com/samber/lo/CHANGELOG.md @@ -1,5 +1,281 @@ # Changelog +@samber: I sometimes forget to update this file. Ping me on [Twitter](https://twitter.com/samuelberthe) or open an issue in case of error. We need to keep a clear changelog for easier lib upgrade. + +## 1.31.0 (2022-10-06) + +Adding: + +- lo.SliceToChannel +- lo.Generator +- lo.Batch +- lo.BatchWithTimeout + +## 1.30.1 (2022-10-06) + +Fix: + +- lo.Try1: remove generic type +- lo.Validate: format error properly + +## 1.30.0 (2022-10-04) + +Adding: + +- lo.TernaryF +- lo.Validate + +## 1.29.0 (2022-10-02) + +Adding: + +- lo.ErrorAs +- lo.TryOr +- lo.TryOrX + +## 1.28.0 (2022-09-05) + +Adding: + +- lo.ChannelDispatcher with 6 dispatching strategies: + - lo.DispatchingStrategyRoundRobin + - lo.DispatchingStrategyRandom + - lo.DispatchingStrategyWeightedRandom + - lo.DispatchingStrategyFirst + - lo.DispatchingStrategyLeast + - lo.DispatchingStrategyMost + +## 1.27.1 (2022-08-15) + +Bugfix: + +- Removed comparable constraint for lo.FindKeyBy + +## 1.27.0 (2022-07-29) + +Breaking: + +- Change of MapToSlice prototype: `MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) []R` -> `MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(K, V) R) []R` + +Added: + +- lo.ChunkString +- lo.SliceToMap (alias to lo.Associate) + +## 1.26.0 (2022-07-24) + +Adding: + +- lo.Associate +- lo.ReduceRight +- lo.FromPtrOr +- lo.MapToSlice +- lo.IsSorted +- lo.IsSortedByKey + +## 1.25.0 (2022-07-04) + +Adding: + +- lo.FindUniques +- lo.FindUniquesBy +- lo.FindDuplicates +- lo.FindDuplicatesBy +- lo.IsNotEmpty + +## 1.24.0 (2022-07-04) + +Adding: + +- lo.Without +- lo.WithoutEmpty + +## 1.23.0 (2022-07-04) + +Adding: + +- lo.FindKey +- lo.FindKeyBy + +## 1.22.0 (2022-07-04) + +Adding: + +- lo.Slice +- lo.FromPtr +- lo.IsEmpty +- lo.Compact +- lo.ToPairs: alias to lo.Entries +- lo.FromPairs: alias to lo.FromEntries +- lo.Partial + +Change: + +- lo.Must + lo.MustX: add context to panic message + +Fix: + +- lo.Nth: out of bound exception (#137) + +## 1.21.0 (2022-05-10) + +Adding: + +- lo.ToAnySlice +- lo.FromAnySlice + +## 1.20.0 (2022-05-02) + +Adding: + +- lo.Synchronize +- lo.SumBy + +Change: +- Removed generic type definition for lo.Try0: `lo.Try0[T]()` -> `lo.Try0()` + +## 1.19.0 (2022-04-30) + +Adding: + +- lo.RepeatBy +- lo.Subset +- lo.Replace +- lo.ReplaceAll +- lo.Substring +- lo.RuneLength + +## 1.18.0 (2022-04-28) + +Adding: + +- lo.SomeBy +- lo.EveryBy +- lo.None +- lo.NoneBy + +## 1.17.0 (2022-04-27) + +Adding: + +- lo.Unpack2 -> lo.Unpack3 +- lo.Async0 -> lo.Async6 + +## 1.16.0 (2022-04-26) + +Adding: + +- lo.AttemptWithDelay + +## 1.15.0 (2022-04-22) + +Improvement: + +- lo.Must: error or boolean value + +## 1.14.0 (2022-04-21) + +Adding: + +- lo.Coalesce + +## 1.13.0 (2022-04-14) + +Adding: + +- PickBy +- PickByKeys +- PickByValues +- OmitBy +- OmitByKeys +- OmitByValues +- Clamp +- MapKeys +- Invert +- IfF + ElseIfF + ElseF +- T0() + T1() + T2() + T3() + ... + +## 1.12.0 (2022-04-12) + +Adding: + +- Must +- Must{0-6} +- FindOrElse +- Async +- MinBy +- MaxBy +- Count +- CountBy +- FindIndexOf +- FindLastIndexOf +- FilterMap + +## 1.11.0 (2022-03-11) + +Adding: + +- Try +- Try{0-6} +- TryWitchValue +- TryCatch +- TryCatchWitchValue +- Debounce +- Reject + +## 1.10.0 (2022-03-11) + +Adding: + +- Range +- RangeFrom +- RangeWithSteps + +## 1.9.0 (2022-03-10) + +Added + +- Drop +- DropRight +- DropWhile +- DropRightWhile + +## 1.8.0 (2022-03-10) + +Adding Union. + +## 1.7.0 (2022-03-09) + +Adding ContainBy + +Adding MapValues + +Adding FlatMap + +## 1.6.0 (2022-03-07) + +Fixed PartitionBy. + +Adding Sample + +Adding Samples + +## 1.5.0 (2022-03-07) + +Adding Times + +Adding Attempt + +Adding Repeat + +## 1.4.0 (2022-03-07) + +- adding tuple types (2->9) +- adding Zip + Unzip +- adding lo.PartitionBy + lop.PartitionBy +- adding lop.GroupBy +- fixing Nth + ## 1.3.0 (2022-03-03) Last and Nth return errors diff --git a/vendor/github.com/samber/lo/Dockerfile b/vendor/github.com/samber/lo/Dockerfile index 9f9f87192..bd01bbbb4 100644 --- a/vendor/github.com/samber/lo/Dockerfile +++ b/vendor/github.com/samber/lo/Dockerfile @@ -1,8 +1,8 @@ -FROM golang:1.18rc1-bullseye +FROM golang:1.18 WORKDIR /go/src/github.com/samber/lo -COPY Makefile go.* /go/src/github.com/samber/lo/ +COPY Makefile go.* ./ RUN make tools diff --git a/vendor/github.com/samber/lo/Makefile b/vendor/github.com/samber/lo/Makefile index 11b09cd25..57bb49159 100644 --- a/vendor/github.com/samber/lo/Makefile +++ b/vendor/github.com/samber/lo/Makefile @@ -1,10 +1,5 @@ BIN=go -# BIN=go1.18beta1 - -go1.18beta1: - go install golang.org/dl/go1.18beta1@latest - go1.18beta1 download build: ${BIN} build -v ./... @@ -12,15 +7,15 @@ build: test: go test -race -v ./... watch-test: - reflex -R assets.go -t 50ms -s -- sh -c 'gotest -race -v ./...' + reflex -t 50ms -s -- sh -c 'gotest -race -v ./...' bench: go test -benchmem -count 3 -bench ./... watch-bench: - reflex -R assets.go -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...' + reflex -t 50ms -s -- sh -c 'go test -benchmem -count 3 -bench ./...' coverage: - ${BIN} test -v -coverprofile cover.out . + ${BIN} test -v -coverprofile=cover.out -covermode=atomic . ${BIN} tool cover -html=cover.out -o cover.html # tools @@ -31,7 +26,7 @@ tools: ${BIN} install github.com/jondot/goweight@latest ${BIN} install github.com/golangci/golangci-lint/cmd/golangci-lint@latest ${BIN} get -t -u golang.org/x/tools/cmd/cover - ${BIN} get -t -u github.com/sonatype-nexus-community/nancy@latest + ${BIN} install github.com/sonatype-nexus-community/nancy@latest go mod tidy lint: @@ -40,11 +35,9 @@ lint-fix: golangci-lint run --timeout 60s --max-same-issues 50 --fix ./... audit: tools - ${BIN} mod tidy ${BIN} list -json -m all | nancy sleuth outdated: tools - ${BIN} mod tidy ${BIN} list -u -m -json all | go-mod-outdated -update -direct weight: tools diff --git a/vendor/github.com/samber/lo/README.md b/vendor/github.com/samber/lo/README.md index 48dfdc004..49cbe49da 100644 --- a/vendor/github.com/samber/lo/README.md +++ b/vendor/github.com/samber/lo/README.md @@ -1,10 +1,12 @@ # lo -![Build Status](https://github.com/samber/lo/actions/workflows/go.yml/badge.svg) +[![tag](https://img.shields.io/github/tag/samber/lo.svg)](https://github.com/samber/lo/releases) [![GoDoc](https://godoc.org/github.com/samber/lo?status.svg)](https://pkg.go.dev/github.com/samber/lo) +![Build Status](https://github.com/samber/lo/actions/workflows/go.yml/badge.svg) [![Go report](https://goreportcard.com/badge/github.com/samber/lo)](https://goreportcard.com/report/github.com/samber/lo) +[![codecov](https://codecov.io/gh/samber/lo/branch/master/graph/badge.svg)](https://codecov.io/gh/samber/lo) -✨ **`lo` is a Lodash-style Go library based on Go 1.18+ Generics.** +✨ **`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](https://github.com/lodash/lodash) in some aspects. I used to code with the fantastic ["go-funk"](https://github.com/thoas/go-funk) package, but "go-funk" uses reflection and therefore is not typesafe. @@ -12,16 +14,25 @@ As expected, benchmarks demonstrate that generics will be much faster than imple 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. -### Why this name? +**See also:** + +- [samber/do](https://github.com/samber/do): A dependency injection toolkit based on Go 1.18+ Generics +- [samber/mo](https://github.com/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 ```sh -go get github.com/samber/lo +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: @@ -48,78 +59,187 @@ GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/sam Supported helpers for slices: -- Filter -- Map -- FlatMap -- Reduce -- ForEach -- Times -- Uniq -- UniqBy -- GroupBy -- Chunk -- PartitionBy -- Flatten -- Shuffle -- Reverse -- Fill -- Repeat -- KeyBy -- Drop -- DropRight -- DropWhile -- DropRightWhile +- [Filter](#filter) +- [Map](#map) +- [FilterMap](#filtermap) +- [FlatMap](#flatmap) +- [Reduce](#reduce) +- [ReduceRight](#reduceright) +- [ForEach](#foreach) +- [Times](#times) +- [Uniq](#uniq) +- [UniqBy](#uniqby) +- [GroupBy](#groupby) +- [Chunk](#chunk) +- [PartitionBy](#partitionby) +- [Flatten](#flatten) +- [Shuffle](#shuffle) +- [Reverse](#reverse) +- [Fill](#fill) +- [Repeat](#repeat) +- [RepeatBy](#repeatby) +- [KeyBy](#keyby) +- [Associate / SliceToMap](#associate-alias-slicetomap) +- [Drop](#drop) +- [DropRight](#dropright) +- [DropWhile](#dropwhile) +- [DropRightWhile](#droprightwhile) +- [Reject](#reject) +- [Count](#count) +- [CountBy](#countby) +- [Subset](#subset) +- [Slice](#slice) +- [Replace](#replace) +- [ReplaceAll](#replaceall) +- [Compact](#compact) +- [IsSorted](#issorted) +- [IsSortedByKey](#issortedbykey) Supported helpers for maps: -- Keys -- Values -- Entries -- FromEntries -- Assign (merge of maps) -- MapValues +- [Keys](#keys) +- [Values](#values) +- [PickBy](#pickby) +- [PickByKeys](#pickbykeys) +- [PickByValues](#pickbyvalues) +- [OmitBy](#omitby) +- [OmitByKeys](#omitbykeys) +- [OmitByValues](#omitbyvalues) +- [Entries / ToPairs](#entries-alias-topairs) +- [FromEntries / FromPairs](#fromentries-alias-frompairs) +- [Invert](#invert) +- [Assign (merge of maps)](#assign) +- [MapKeys](#mapkeys) +- [MapValues](#mapvalues) +- [MapToSlice](#maptoslice) + +Supported math helpers: + +- [Range / RangeFrom / RangeWithSteps](#range--rangefrom--rangewithsteps) +- [Clamp](#clamp) +- [SumBy](#sumby) + +Supported helpers for strings: + +- [Substring](#substring) +- [ChunkString](#chunkstring) +- [RuneLength](#runelength) Supported helpers for tuples: -- Zip2 -> Zip9 -- Unzip2 -> Unzip9 +- [T2 -> T9](#t2---t9) +- [Unpack2 -> Unpack9](#unpack2---unpack9) +- [Zip2 -> Zip9](#zip2---zip9) +- [Unzip2 -> Unzip9](#unzip2---unzip9) + +Supported helpers for channels: + +- [ChannelDispatcher](#channeldispatcher) +- [SliceToChannel](#slicetochannel) +- [Generator](#generator) +- [Batch](#batch) +- [BatchWithTimeout](#batchwithtimeout) Supported intersection helpers: -- Contains -- ContainsBy -- Every -- Some -- Intersect -- Difference -- Union +- [Contains](#contains) +- [ContainsBy](#containsby) +- [Every](#every) +- [EveryBy](#everyby) +- [Some](#some) +- [SomeBy](#someby) +- [None](#none) +- [NoneBy](#noneby) +- [Intersect](#intersect) +- [Difference](#difference) +- [Union](#union) +- [Without](#without) +- [WithoutEmpty](#withoutempty) Supported search helpers: -- IndexOf -- LastIndexOf -- Find -- Min -- Max -- Last -- Nth -- Sample -- Samples - -Other functional programming helpers: - -- Ternary (1 line if/else statement) -- If / ElseIf / Else -- Switch / Case / Default -- ToPtr -- ToSlicePtr -- Attempt -- Range / RangeFrom / RangeWithSteps +- [IndexOf](#indexof) +- [LastIndexOf](#lastindexof) +- [Find](#find) +- [FindIndexOf](#findindexof) +- [FindLastIndexOf](#findlastindexof) +- [FindKey](#findkey) +- [FindKeyBy](#findkeyby) +- [FindUniques](#finduniques) +- [FindUniquesBy](#finduniquesby) +- [FindDuplicates](#findduplicates) +- [FindDuplicatesBy](#findduplicatesby) +- [Min](#min) +- [MinBy](#minby) +- [Max](#max) +- [MaxBy](#maxby) +- [Last](#last) +- [Nth](#nth) +- [Sample](#sample) +- [Samples](#samples) + +Conditional helpers: + +- [Ternary](#ternary) +- [TernaryF](#ternaryf) +- [If / ElseIf / Else](#if--elseif--else) +- [Switch / Case / Default](#switch--case--default) + +Type manipulation helpers: + +- [ToPtr](#toptr) +- [FromPtr](#fromptr) +- [FromPtrOr](#fromptror) +- [ToSlicePtr](#tosliceptr) +- [ToAnySlice](#toanyslice) +- [FromAnySlice](#fromanyslice) +- [Empty](#empty) +- [IsEmpty](#isempty) +- [IsNotEmpty](#isnotempty) +- [Coalesce](#coalesce) + +Function helpers: + +- [Partial](#partial) + +Concurrency helpers: + +- [Attempt](#attempt) +- [AttemptWithDelay](#attemptwithdelay) +- [Debounce](#debounce) +- [Synchronize](#synchronize) +- [Async](#async) + +Error handling: + +- [Validate](#validate) +- [Must](#must) +- [Try](#try) +- [Try1 -> Try6](#try0-6) +- [TryOr](#tryor) +- [TryOr1 -> TryOr6](#tryor0-6) +- [TryCatch](#trycatch) +- [TryWithErrorValue](#trywitherrorvalue) +- [TryCatchWithErrorValue](#trycatchwitherrorvalue) +- [ErrorsAs](#errorsas) Constraints: - Clonable +### Filter + +Iterates over a collection and returns an array of all the elements the predicate function returns `true` for. + +```go +even := lo.Filter[int]([]int{1, 2, 3, 4}, func(x int, index int) bool { + return x%2 == 0 +}) +// []int{2, 4} +``` + +[[play](https://go.dev/play/p/Apjg3WeSi7K)] + ### Map Manipulates a slice of one type and transforms it into a slice of another type: @@ -127,12 +247,14 @@ Manipulates a slice of one type and transforms it into a slice of another type: ```go import "github.com/samber/lo" -lo.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string { +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](https://go.dev/play/p/OkPcYAhBo0D)] + Parallel processing: like `lo.Map()`, but the mapper function is called in a goroutine. Results are returned in the same order. ```go @@ -144,6 +266,24 @@ lop.Map[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) string { // []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. + +```go +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](https://go.dev/play/p/-AuYXfy7opz)] + ### FlatMap Manipulates a slice and transforms and flattens it to a slice of another type. @@ -158,36 +298,7 @@ lo.FlatMap[int, string]([]int{0, 1, 2}, func(x int, _ int) []string { // []string{"0", "0", "1", "1", "2", "2"} ``` -### Filter - -Iterates over a collection and returns an array of all the elements the predicate function returns `true` for. - -```go -even := lo.Filter[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool { - return x%2 == 0 -}) -// []int{2, 4} -``` - -### Contains - -Returns true if an element is present in a collection. - -```go -present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5) -// true -``` - -### Contains - -Returns true if the predicate function returns `true`. - -```go -present := lo.ContainsBy[int]([]int{0, 1, 2, 3, 4, 5}, func(x int) bool { - return x == 3 -}) -// true -``` +[[play](https://go.dev/play/p/YSoYmQTA8-U)] ### Reduce @@ -200,6 +311,21 @@ sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int // 10 ``` +[[play](https://go.dev/play/p/R4UHXZNaaUG)] + +### ReduceRight + +Like `lo.Reduce` except that it iterates over elements of collection from right to left. + +```go +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](https://go.dev/play/p/Fq3W70l7wXF)] + ### ForEach Iterates over elements of a collection and invokes the function over each element. @@ -213,6 +339,8 @@ lo.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) { // prints "hello\nworld\n" ``` +[[play](https://go.dev/play/p/oofyiUPRf8t)] + Parallel processing: like `lo.ForEach()`, but the callback is called as a goroutine. ```go @@ -237,6 +365,8 @@ lo.Times[string](3, func(i int) string { // []string{"0", "1", "2"} ``` +[[play](https://go.dev/play/p/vgQj3Glr6lT)] + Parallel processing: like `lo.Times()`, but callback is called in goroutine. ```go @@ -257,6 +387,8 @@ uniqValues := lo.Uniq[int]([]int{1, 2, 2, 1}) // []int{1, 2} ``` +[[play](https://go.dev/play/p/DTzbeXZ6iEN)] + ### 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. @@ -268,6 +400,8 @@ uniqValues := lo.UniqBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int { // []int{0, 1, 2} ``` +[[play](https://go.dev/play/p/g42Z3QSb53u)] + ### GroupBy Returns an object composed of keys generated from the results of running each element of collection through iteratee. @@ -281,6 +415,8 @@ groups := lo.GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int { // map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}} ``` +[[play](https://go.dev/play/p/XnQBd_v6brd)] + Parallel processing: like `lo.GroupBy()`, but callback is called in goroutine. ```go @@ -310,6 +446,8 @@ lo.Chunk[int]([]int{0}, 2) // [][]int{{0}} ``` +[[play](https://go.dev/play/p/EeKl0AuTehH)] + ### 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. @@ -328,12 +466,14 @@ partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func( // [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}} ``` +[[play](https://go.dev/play/p/NfQ_nGjkgXW)] + Parallel processing: like `lo.PartitionBy()`, but callback is called in goroutine. Results are returned in the same order. ```go import lop "github.com/samber/lo/parallel" -partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string { +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 { @@ -353,24 +493,30 @@ flat := lo.Flatten[int]([][]int{{0, 1}, {2, 3, 4, 5}}) // []int{0, 1, 2, 3, 4, 5} ``` +[[play](https://go.dev/play/p/rbp9ORaMpjw)] + ### Shuffle Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm. ```go randomOrder := lo.Shuffle[int]([]int{0, 1, 2, 3, 4, 5}) -// []int{0, 1, 2, 3, 4, 5} +// []int{1, 4, 0, 3, 5, 2} ``` +[[play](https://go.dev/play/p/Qp73bnTDnc7)] + ### Reverse Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on. ```go -reverseOder := lo.Reverse[int]([]int{0, 1, 2, 3, 4, 5}) +reverseOrder := lo.Reverse[int]([]int{0, 1, 2, 3, 4, 5}) // []int{5, 4, 3, 2, 1, 0} ``` +[[play](https://go.dev/play/p/fhUMLvZ7vS6)] + ### Fill Fills elements of array with `initial` value. @@ -388,6 +534,8 @@ initializedSlice := lo.Fill[foo]([]foo{foo{"a"}, foo{"a"}}, foo{"b"}) // []foo{foo{"b"}, foo{"b"}} ``` +[[play](https://go.dev/play/p/VwR34GzqEub)] + ### Repeat Builds a slice with N copies of initial value. @@ -401,10 +549,30 @@ func (f foo) Clone() foo { return foo{f.bar} } -initializedSlice := lo.Repeat[foo](2, foo{"a"}) +slice := lo.Repeat[foo](2, foo{"a"}) // []foo{foo{"a"}, foo{"a"}} ``` +[[play](https://go.dev/play/p/g3uHXbmc3b6)] + +### RepeatBy + +Builds a slice with values returned by N calls of callback. + +```go +slice := lo.RepeatBy[string](0, func (i int) string { + return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10) +}) +// []int{} + +slice := lo.RepeatBy[string](5, func(i int) string { + return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10) +}) +// []int{0, 1, 4, 9, 16} +``` + +[[play](https://go.dev/play/p/ozZLCtX_hNU)] + ### KeyBy Transforms a slice or an array of structs to a map based on a pivot callback. @@ -423,12 +591,32 @@ characters := []Character{ {dir: "left", code: 97}, {dir: "right", code: 100}, } -result := KeyBy[Character, string](characters, func(char Character) string { +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](https://go.dev/play/p/mdaClUAT-zZ)] + +### 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. + +```go +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](https://go.dev/play/p/WHa2CfMO3Lr)] + ### Drop Drops n elements from the beginning of a slice or array. @@ -438,6 +626,8 @@ l := lo.Drop[int]([]int{0, 1, 2, 3, 4, 5}, 2) // []int{2, 3, 4, 5} ``` +[[play](https://go.dev/play/p/JswS7vXRJP2)] + ### DropRight Drops n elements from the end of a slice or array. @@ -447,6 +637,8 @@ l := lo.DropRight[int]([]int{0, 1, 2, 3, 4, 5}, 2) // []int{0, 1, 2, 3} ``` +[[play](https://go.dev/play/p/GG0nXkSJJa3)] + ### DropWhile Drop elements from the beginning of a slice or array while the predicate returns true. @@ -455,9 +647,11 @@ Drop elements from the beginning of a slice or array while the predicate returns l := lo.DropWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool { return len(val) <= 2 }) -// []string{"aaa", "aa", "a"} +// []string{"aaa", "aa", "aa"} ``` +[[play](https://go.dev/play/p/7gBPYw2IK16)] + ### DropRightWhile Drop elements from the end of a slice or array while the predicate returns true. @@ -469,429 +663,1804 @@ l := lo.DropRightWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val // []string{"a", "aa", "aaa"} ``` -### Keys +[[play](https://go.dev/play/p/3-n71oEC0Hz)] -Creates an array of the map keys. +### Reject + +The opposite of Filter, this method returns the elements of collection that predicate does not return truthy for. ```go -keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2}) -// []string{"bar", "foo"} +odd := lo.Reject[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool { + return x%2 == 0 +}) +// []int{1, 3} ``` -### Values +[[play](https://go.dev/play/p/YkLMODy1WEL)] -Creates an array of the map values. +### Count + +Counts the number of elements in the collection that compare equal to value. ```go -values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2}) -// []int{1, 2} +count := lo.Count[int]([]int{1, 5, 1}, 1) +// 2 ``` -### Entries +[[play](https://go.dev/play/p/Y3FlK54yveC)] -Transforms a map into array of key/value pairs. +### CountBy + +Counts the number of elements in the collection for which predicate is true. ```go -entries := lo.Entries[string, int](map[string]int{"foo": 1, "bar": 2}) -// []lo.Entry[string, int]{ -// { -// Key: "foo", -// Value: 1, -// }, -// { -// Key: "bar", -// Value: 2, -// }, -// } +count := lo.CountBy[int]([]int{1, 5, 1}, func(i int) bool { + return i < 4 +}) +// 2 ``` -### FromEntries +[[play](https://go.dev/play/p/ByQbNYQQi4X)] -Transforms an array of key/value pairs into a map. +### Subset + +Returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow. ```go -m := lo.FromEntries[string, int]([]lo.Entry[string, int]{ - { - Key: "foo", - Value: 1, - }, - { - Key: "bar", - Value: 2, - }, -}) -// map[string]int{"foo": 1, "bar": 2} -``` +in := []int{0, 1, 2, 3, 4} -### Assign +sub := lo.Subset(in, 2, 3) +// []int{2, 3, 4} -Merges multiple maps from left to right. +sub := lo.Subset(in, -4, 3) +// []int{1, 2, 3} -```go -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} +sub := lo.Subset(in, -2, math.MaxUint) +// []int{3, 4} ``` -### MapValues +[[play](https://go.dev/play/p/tOQu1GhFcog)] -Manipulates a map values and transforms it to a map of another type. +### 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. ```go -m1 := map[int]int64{1: 1, 2: 2, 3: 3} +in := []int{0, 1, 2, 3, 4} -m2 := lo.MapValues[int, int64, string](m, func(x int64, _ int) string { - return strconv.FormatInt(x, 10) -}) -// map[int]string{1: "1", 2: "2", 3: "3"} +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{} ``` -### Zip2 -> Zip9 +[[play](https://go.dev/play/p/8XWYhfMMA1h)] -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. +### Replace -When collections have different size, the Tuple attributes are filled with zero value. +Returns a copy of the slice with the first n non-overlapping instances of old replaced by new. ```go -tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2}) -// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}} +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} ``` -### Unzip2 -> Unzip9 +[[play](https://go.dev/play/p/XfPzmf9gql6)] -Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration. +### ReplaceAll + +Returns a copy of the slice with all non-overlapping instances of old replaced by new. ```go -a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}) -// []string{"a", "b"} -// []int{1, 2} +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} ``` -### Every +[[play](https://go.dev/play/p/a9xZFUHfYcV)] -Returns true if all elements of a subset are contained into a collection. +### Compact + +Returns a slice of all non-zero elements. ```go -ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2}) -// true +in := []string{"", "foo", "", "bar", ""} -ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6}) -// false +slice := lo.Compact[string](in) +// []string{"foo", "bar"} ``` -### Some +[[play](https://go.dev/play/p/tXiy-iK6PAc)] -Returns true if at least 1 element of a subset is contained into a collection. +### IsSorted + +Checks if a slice is sorted. ```go -ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2}) +slice := lo.IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) // true - -ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6}) -// false ``` -### Intersect +[[play](https://go.dev/play/p/mc3qR-t4mcx)] -Returns the intersection between two collections. +### IsSortedByKey + +Checks if a slice is sorted by iteratee. ```go -result1 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2}) -// []int{0, 2} +slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int { + return len(s) +}) +// true +``` -result2 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6} -// []int{0} +[[play](https://go.dev/play/p/wiG6XyBBu49)] -result3 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6}) -// []int{} +### Keys + +Creates an array of the map keys. + +```go +keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2}) +// []string{"foo", "bar"} ``` -### Difference +[[play](https://go.dev/play/p/Uu11fHASqrU)] -Returns the difference between two collections. +### Values -- The first value is the collection of element absent of list2. -- The second value is the collection of element absent of list1. +Creates an array of the map values. ```go -left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6}) -// []int{1, 3, 4, 5}, []int{6} - -left, right := Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5}) -// []int{}, []int{} +values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2}) +// []int{1, 2} ``` -### Union +[[play](https://go.dev/play/p/nnRTQkzQfF6)] -Returns all distinct elements from both collections. Result will not change the order of elements relatively. +### PickBy + +Returns same map type filtered by given predicate. ```go -union := lo.Union[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10}) -// []int{0, 1, 2, 3, 4, 5, 10} +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} ``` -### IndexOf +[[play](https://go.dev/play/p/kdg8GR_QMmf)] -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. +### PickByKeys -```go -found := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2) -// 2 +Returns same map type filtered by given keys. -notFound := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6) -// -1 +```go +m := lo.PickByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"}) +// map[string]int{"foo": 1, "baz": 3} ``` -### LastIndex +[[play](https://go.dev/play/p/R1imbuci9qU)] -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. +### PickByValues -```go -found := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2) -// 4 +Returns same map type filtered by given values. -notFound := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6) -// -1 +```go +m := lo.PickByValues[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3}) +// map[string]int{"foo": 1, "baz": 3} ``` -### Find +[[play](https://go.dev/play/p/1zdzSvbfsJc)] -Search an element in a slice based on a predicate. It returns element and true if element was found. +### OmitBy -```go -str, ok := lo.Find[string]([]string{"a", "b", "c", "d"}, func(i string) bool { - return i == "b" -}) -// "b", true +Returns same map type filtered by given predicate. -str, ok := lo.Find[string]([]string{"foobar"}, func(i string) bool { - return i == "b" +```go +m := lo.OmitBy[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool { + return value%2 == 1 }) -// "", false +// map[string]int{"bar": 2} ``` -### Min +[[play](https://go.dev/play/p/EtBsR43bdsd)] -Search the minimum value of a collection. +### OmitByKeys + +Returns same map type filtered by given keys. + +```go +m := lo.OmitByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"}) +// map[string]int{"bar": 2} +``` + +[[play](https://go.dev/play/p/t1QjCrs-ysk)] + +### OmitByValues + +Returns same map type filtered by given values. + +```go +m := lo.OmitByValues[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3}) +// map[string]int{"bar": 2} +``` + +[[play](https://go.dev/play/p/9UYZi-hrs8j)] + +### Entries (alias: ToPairs) + +Transforms a map into array of key/value pairs. + +```go +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](https://go.dev/play/p/3Dhgx46gawJ)] + +### FromEntries (alias: FromPairs) + +Transforms an array of key/value pairs into a map. + +```go +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](https://go.dev/play/p/oIr5KHFGCEN)] + +### Invert + +Creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values. + +```go +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](https://go.dev/play/p/rFQ4rak6iA1)] + +### Assign + +Merges multiple maps from left to right. + +```go +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](https://go.dev/play/p/VhwfJOyxf5o)] + +### MapKeys + +Manipulates a map keys and transforms it to a map of another type. + +```go +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](https://go.dev/play/p/9_4WPIqOetJ)] + +### MapValues + +Manipulates a map values and transforms it to a map of another type. + +```go +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](https://go.dev/play/p/T_8xAfvcf0W)] + +### MapToSlice + +Transforms a map into a slice based on specific iteratee. + +```go +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](https://go.dev/play/p/ZuiCZpDt6LD)] + +### Range / RangeFrom / RangeWithSteps + +Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end. + +```go +result := Range(4) +// [0, 1, 2, 3] + +result := Range(-4) +// [0, -1, -2, -3] + +result := RangeFrom(1, 5) +// [1, 2, 3, 4, 5] + +result := RangeFrom[float64](1.0, 5) +// [1.0, 2.0, 3.0, 4.0, 5.0] + +result := RangeWithSteps(0, 20, 5) +// [0, 5, 10, 15] + +result := RangeWithSteps[float32](-1.0, -4.0, -1.0) +// [-1.0, -2.0, -3.0] + +result := RangeWithSteps(1, 4, -1) +// [] + +result := Range(0) +// [] +``` + +[[play](https://go.dev/play/p/0r6VimXAi9H)] + +### Clamp + +Clamps number within the inclusive lower and upper bounds. + +```go +r1 := lo.Clamp(0, -10, 10) +// 0 + +r2 := lo.Clamp(-42, -10, 10) +// -10 + +r3 := lo.Clamp(42, -10, 10) +// 10 +``` + +[[play](https://go.dev/play/p/RU4lJNC2hlI)] + +### SumBy + +Summarizes the values in a collection using the given return value from the iteration function. +If collection is empty 0 is returned. + +```go +strings := []string{"foo", "bar"} +sum := lo.SumBy(strings, func(item string) int { + return len(item) +}) +// 6 +``` + +[[play](https://go.dev/play/p/Dz_a_7jN_ca)] + +### Substring + +Return part of a string. + +```go +sub := lo.Substring("hello", 2, 3) +// "llo" + +sub := lo.Substring("hello", -4, 3) +// "ell" + +sub := lo.Substring("hello", -2, math.MaxUint) +// "lo" +``` + +[[play](https://go.dev/play/p/TQlxQi82Lu1)] + +### 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. + +```go +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](https://go.dev/play/p/__FLTuJVz54)] + +### RuneLength + +An alias to utf8.RuneCountInString which returns the number of runes in string. + +```go +sub := lo.RuneLength("hellô") +// 5 + +sub := len("hellô") +// 6 +``` + +[[play](https://go.dev/play/p/tuhgW_lWY8l)] + +### T2 -> T9 + +Creates a tuple from a list of values. + +```go +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](https://go.dev/play/p/IllL3ZO4BQm)] + +### Unpack2 -> Unpack9 + +Returns values contained in tuple. + +```go +r1, r2 := lo.Unpack2[string, int](lo.Tuple2[string, int]{"a", 1}) +// "a", 1 +``` + +[[play](https://go.dev/play/p/xVP_k0kJ96W)] + +### 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. + +```go +tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2}) +// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}} +``` + +[[play](https://go.dev/play/p/jujaA6GaJTp)] + +### Unzip2 -> Unzip9 + +Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration. + +```go +a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}) +// []string{"a", "b"} +// []int{1, 2} +``` + +[[play](https://go.dev/play/p/ciHugugvaAW)] + +### 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. + +```go +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: + +- [lo.DispatchingStrategyRoundRobin](./channel.go): Distributes messages in a rotating sequential manner. +- [lo.DispatchingStrategyRandom](./channel.go): Distributes messages in a random manner. +- [lo.DispatchingStrategyWeightedRandom](./channel.go): Distributes messages in a weighted manner. +- [lo.DispatchingStrategyFirst](./channel.go): Distributes messages in the first non-full channel. +- [lo.DispatchingStrategyLeast](./channel.go): Distributes messages in the emptiest channel. +- [lo.DispatchingStrategyMost](./channel.go): Distributes to the fulliest channel. + +Some strategies bring fallback, in order to favor non-blocking behaviors. See implementations. + +For custom strategies, just implement the `lo.DispatchingStrategy` prototype: + +```go +type DispatchingStrategy[T any] func(message T, messageIndex uint64, channels []<-chan T) int +``` + +Eg: + +```go +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. + +```go +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 +``` + +### Generator + +Implements the generator design pattern. Channel is closed after last element. Channel capacity can be customized. + +```go +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). + +```go +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 👇 + +```go +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). + +```go +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 👇 + +```go +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 👇 + +```go +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]) +} +``` + +### Contains + +Returns true if an element is present in a collection. + +```go +present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5) +// true +``` + +### ContainsBy + +Returns true if the predicate function returns `true`. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +b := NoneBy[int]([]int{1, 2, 3, 4}, func(x int) bool { + return x < 0 +}) +// true +``` + +### Intersect + +Returns the intersection between two collections. + +```go +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. + +```go +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 both collections. Result will not change the order of elements relatively. + +```go +union := lo.Union[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10}) +// []int{0, 1, 2, 3, 4, 5, 10} +``` + +### Without + +Returns slice excluding all given values. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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. + +```go +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 occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array. + +```go +duplicatedValues := lo.FindDuplicates[int]([]int{1, 2, 2, 1, 2, 3}) +// []int{1, 2} +``` + +### FindDuplicatesBy + +Returns a slice with the first occurence 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. + +```go +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. + +```go +min := lo.Min[int]([]int{1, 2, 3}) +// 1 + +min := lo.Min[int]([]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. ```go -min := lo.Min[int]([]int{1, 2, 3}) +min := lo.MinBy[string]([]string{"s1", "string2", "s3"}, func(item string, min string) bool { + return len(item) < len(min) +}) +// "s1" + +min := lo.MinBy[string]([]string{}, func(item string, min string) bool { + return len(item) < len(min) +}) +// "" +``` + +### Max + +Search the maximum value of a collection. + +```go +max := lo.Max[int]([]int{1, 2, 3}) +// 3 + +max := lo.Max[int]([]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. + +```go +max := lo.MaxBy[string]([]string{"string1", "s2", "string3"}, func(item string, max string) bool { + return len(item) > len(max) +}) +// "string1" + +max := lo.MaxBy[string]([]string{}, func(item string, max string) bool { + return len(item) > len(max) +}) +// "" +``` + +### Last + +Returns the last element of a collection or error if empty. + +```go +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. + +```go +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. + +```go +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. + +```go +lo.Samples[string]([]string{"a", "b", "c"}, 3) +// []string{"a", "b", "c"} in random order +``` + +### Ternary + +A 1 line if/else statement. + +```go +result := lo.Ternary[stri