From c7a629c4401ae0d4aad06767c88ce1e9e418dbf3 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 19 Mar 2022 12:26:30 +1100 Subject: [PATCH] make more use of generics --- go.mod | 2 + go.sum | 5 + pkg/commands/git_commands/rebase_test.go | 4 +- pkg/commands/loaders/branches.go | 7 +- pkg/commands/loaders/files.go | 10 +- pkg/commands/patch/hunk.go | 3 +- pkg/commands/patch/patch_manager.go | 6 +- pkg/commands/patch/patch_parser.go | 3 +- pkg/gui/controllers/global_controller.go | 8 +- .../controllers/helpers/cherry_pick_helper.go | 34 +- pkg/gui/filetree/collapsed_paths.go | 32 +- pkg/gui/filetree/commit_file_node.go | 8 +- pkg/gui/filetree/commit_file_tree.go | 6 +- pkg/gui/filetree/file_node.go | 8 +- pkg/gui/filetree/file_tree.go | 8 +- pkg/gui/filetree/inode.go | 18 +- pkg/gui/list_context_config.go | 6 +- pkg/gui/options_menu_panel.go | 18 +- pkg/gui/patch_building_panel.go | 6 +- pkg/gui/presentation/commits.go | 13 +- pkg/gui/presentation/commits_test.go | 5 +- pkg/gui/presentation/files.go | 2 +- pkg/gui/presentation/reflog_commits.go | 5 +- pkg/gui/refresh.go | 27 +- pkg/utils/slice.go | 83 -- pkg/utils/slice_test.go | 76 -- .../github.com/jesseduffield/generics/LICENSE | 21 + .../generics/hashmap/functions.go | 35 + .../generics/list/comparable_list.go | 49 + .../jesseduffield/generics/list/functions.go | 72 ++ .../jesseduffield/generics/list/list.go | 117 +++ .../jesseduffield/generics/set/set.go | 49 + vendor/github.com/samber/lo/.gitignore | 36 + vendor/github.com/samber/lo/CHANGELOG.md | 71 ++ vendor/github.com/samber/lo/Dockerfile | 8 + vendor/github.com/samber/lo/LICENSE | 21 + vendor/github.com/samber/lo/Makefile | 51 + vendor/github.com/samber/lo/README.md | 981 ++++++++++++++++++ vendor/github.com/samber/lo/condition.go | 99 ++ vendor/github.com/samber/lo/constraints.go | 6 + .../github.com/samber/lo/docker-compose.yml | 9 + vendor/github.com/samber/lo/drop.go | 65 ++ vendor/github.com/samber/lo/find.go | 157 +++ vendor/github.com/samber/lo/intersect.go | 131 +++ vendor/github.com/samber/lo/map.go | 72 ++ vendor/github.com/samber/lo/pointers.go | 19 + vendor/github.com/samber/lo/retry.go | 19 + vendor/github.com/samber/lo/slice.go | 242 +++++ vendor/github.com/samber/lo/tuples.go | 413 ++++++++ vendor/github.com/samber/lo/types.go | 83 ++ vendor/github.com/samber/lo/util.go | 50 + vendor/modules.txt | 8 + 52 files changed, 3013 insertions(+), 274 deletions(-) create mode 100644 vendor/github.com/jesseduffield/generics/LICENSE create mode 100644 vendor/github.com/jesseduffield/generics/hashmap/functions.go create mode 100644 vendor/github.com/jesseduffield/generics/list/comparable_list.go create mode 100644 vendor/github.com/jesseduffield/generics/list/functions.go create mode 100644 vendor/github.com/jesseduffield/generics/list/list.go create mode 100644 vendor/github.com/jesseduffield/generics/set/set.go create mode 100644 vendor/github.com/samber/lo/.gitignore create mode 100644 vendor/github.com/samber/lo/CHANGELOG.md create mode 100644 vendor/github.com/samber/lo/Dockerfile create mode 100644 vendor/github.com/samber/lo/LICENSE create mode 100644 vendor/github.com/samber/lo/Makefile create mode 100644 vendor/github.com/samber/lo/README.md create mode 100644 vendor/github.com/samber/lo/condition.go create mode 100644 vendor/github.com/samber/lo/constraints.go create mode 100644 vendor/github.com/samber/lo/docker-compose.yml create mode 100644 vendor/github.com/samber/lo/drop.go create mode 100644 vendor/github.com/samber/lo/find.go create mode 100644 vendor/github.com/samber/lo/intersect.go create mode 100644 vendor/github.com/samber/lo/map.go create mode 100644 vendor/github.com/samber/lo/pointers.go create mode 100644 vendor/github.com/samber/lo/retry.go create mode 100644 vendor/github.com/samber/lo/slice.go create mode 100644 vendor/github.com/samber/lo/tuples.go create mode 100644 vendor/github.com/samber/lo/types.go create mode 100644 vendor/github.com/samber/lo/util.go diff --git a/go.mod b/go.mod index 3e7598384..afdf377ed 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/gookit/color v1.4.2 github.com/imdario/mergo v0.3.11 github.com/integrii/flaggy v1.4.0 + github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 github.com/jesseduffield/gocui v0.3.1-0.20220227022729-69f0c798eec8 github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e @@ -25,6 +26,7 @@ require ( github.com/mgutz/str v1.2.0 github.com/pmezard/go-difflib v1.0.0 github.com/sahilm/fuzzy v0.1.0 + github.com/samber/lo v1.10.1 github.com/sanity-io/litter v1.5.2 github.com/sirupsen/logrus v1.4.2 github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad diff --git a/go.sum b/go.sum index fc2d2400d..c06b0aef1 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/integrii/flaggy v1.4.0 h1:A1x7SYx4jqu5NSrY14z8Z+0UyX2S5ygfJJrfolWR3zM github.com/integrii/flaggy v1.4.0/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f h1:9USuZttmg5ioHsjFyXboiGSbncpAqcKkq9qb4ga5PD0= +github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk= github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 h1:GOQrmaE8i+KEdB8NzAegKYd4tPn/inM0I1uo0NXFerg= github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o= github.com/jesseduffield/gocui v0.3.1-0.20220227022729-69f0c798eec8 h1:9N08i5kjvOfkzMj6THmIM110wPTQLdVYEOHMHT2DFiI= @@ -129,6 +131,8 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI= github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= +github.com/samber/lo v1.10.1 h1:0D3h7i0U3hRAbaCeQ82DLe67n0A7Bbl0/cEoWqFGp+U= +github.com/samber/lo v1.10.1/go.mod h1:2I7tgIv8Q1SG2xEIkRq0F2i2zgxVpnyPOP0d3Gj2r+A= github.com/sanity-io/litter v1.5.2 h1:AnC8s9BMORWH5a4atZ4D6FPVvKGzHcnc5/IVTa87myw= github.com/sanity-io/litter v1.5.2/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= @@ -146,6 +150,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/urfave/cli v1.20.1-0.20180226030253-8e01ec4cd3e2/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= diff --git a/pkg/commands/git_commands/rebase_test.go b/pkg/commands/git_commands/rebase_test.go index 56df77a86..4e7b5c2c6 100644 --- a/pkg/commands/git_commands/rebase_test.go +++ b/pkg/commands/git_commands/rebase_test.go @@ -8,7 +8,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/stretchr/testify/assert" ) @@ -64,7 +64,7 @@ func TestRebaseSkipEditorCommand(t *testing.T) { "^LAZYGIT_CLIENT_COMMAND=EXIT_IMMEDIATELY$", } { regexStr := regexStr - foundMatch := utils.IncludesStringFunc(envVars, func(envVar string) bool { + foundMatch := lo.ContainsBy(envVars, func(envVar string) bool { return regexp.MustCompile(regexStr).MatchString(envVar) }) if !foundMatch { diff --git a/pkg/commands/loaders/branches.go b/pkg/commands/loaders/branches.go index 1f78908a8..9fa1e80f4 100644 --- a/pkg/commands/loaders/branches.go +++ b/pkg/commands/loaders/branches.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/go-git/v5/config" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/common" @@ -181,15 +182,15 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { // TODO: only look at the new reflog commits, and otherwise store the recencies in // int form against the branch to recalculate the time ago func (self *BranchLoader) obtainReflogBranches(reflogCommits []*models.Commit) []*models.Branch { - foundBranchesMap := map[string]bool{} + foundBranches := set.New[string]() re := regexp.MustCompile(`checkout: moving from ([\S]+) to ([\S]+)`) reflogBranches := make([]*models.Branch, 0, len(reflogCommits)) for _, commit := range reflogCommits { if match := re.FindStringSubmatch(commit.Name); len(match) == 3 { recency := utils.UnixToTimeAgo(commit.UnixTimestamp) for _, branchName := range match[1:] { - if !foundBranchesMap[branchName] { - foundBranchesMap[branchName] = true + if !foundBranches.Includes(branchName) { + foundBranches.Add(branchName) reflogBranches = append(reflogBranches, &models.Branch{ Recency: recency, Name: branchName, diff --git a/pkg/commands/loaders/files.go b/pkg/commands/loaders/files.go index f5becdb92..8ab727453 100644 --- a/pkg/commands/loaders/files.go +++ b/pkg/commands/loaders/files.go @@ -7,7 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/common" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type FileLoaderConfig interface { @@ -57,10 +57,10 @@ func (self *FileLoader) GetStatusFiles(opts GetStatusFileOptions) []*models.File change := status.Change stagedChange := change[0:1] unstagedChange := change[1:2] - untracked := utils.IncludesString([]string{"??", "A ", "AM"}, change) - hasNoStagedChanges := utils.IncludesString([]string{" ", "U", "?"}, stagedChange) - hasInlineMergeConflicts := utils.IncludesString([]string{"UU", "AA"}, change) - hasMergeConflicts := hasInlineMergeConflicts || utils.IncludesString([]string{"DD", "AU", "UA", "UD", "DU"}, change) + untracked := lo.Contains([]string{"??", "A ", "AM"}, change) + hasNoStagedChanges := lo.Contains([]string{" ", "U", "?"}, stagedChange) + hasInlineMergeConflicts := lo.Contains([]string{"UU", "AA"}, change) + hasMergeConflicts := hasInlineMergeConflicts || lo.Contains([]string{"DD", "AU", "UA", "UD", "DU"}, change) file := &models.File{ Name: status.Name, diff --git a/pkg/commands/patch/hunk.go b/pkg/commands/patch/hunk.go index bbb2d54ff..98d932126 100644 --- a/pkg/commands/patch/hunk.go +++ b/pkg/commands/patch/hunk.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type PatchHunk struct { @@ -54,7 +55,7 @@ func (hunk *PatchHunk) updatedLines(lineIndices []int, reverse bool) []string { if line == "" { break } - isLineSelected := utils.IncludesInt(lineIndices, lineIdx) + isLineSelected := lo.Contains(lineIndices, lineIdx) firstChar, content := line[:1], line[1:] transformedFirstChar := transformedFirstChar(firstChar, reverse, isLineSelected) diff --git a/pkg/commands/patch/patch_manager.go b/pkg/commands/patch/patch_manager.go index cbdf7b2d4..1282356f8 100644 --- a/pkg/commands/patch/patch_manager.go +++ b/pkg/commands/patch/patch_manager.go @@ -4,7 +4,7 @@ import ( "sort" "strings" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -140,7 +140,7 @@ func (p *PatchManager) AddFileLineRange(filename string, firstLineIdx, lastLineI return err } info.mode = PART - info.includedLineIndices = utils.UnionInt(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) + info.includedLineIndices = lo.Union(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) return nil } @@ -151,7 +151,7 @@ func (p *PatchManager) RemoveFileLineRange(filename string, firstLineIdx, lastLi return err } info.mode = PART - info.includedLineIndices = utils.DifferenceInt(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) + info.includedLineIndices, _ = lo.Difference(info.includedLineIndices, getIndicesForRange(firstLineIdx, lastLineIdx)) if len(info.includedLineIndices) == 0 { p.removeFile(info) } diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index c2be120c9..3810d8a29 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "github.com/sirupsen/logrus" ) @@ -186,7 +187,7 @@ func (p *PatchParser) Render(firstLineIndex int, lastLineIndex int, incLineIndic renderedLines := make([]string, len(p.PatchLines)) for index, patchLine := range p.PatchLines { selected := index >= firstLineIndex && index <= lastLineIndex - included := utils.IncludesInt(incLineIndices, index) + included := lo.Contains(incLineIndices, index) renderedLines[index] = patchLine.render(selected, included) } result := strings.Join(renderedLines, "\n") diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 487c1e10b..c45e98d85 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -1,9 +1,11 @@ package controllers import ( + "github.com/jesseduffield/generics/list" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) type GlobalController struct { @@ -36,9 +38,7 @@ func (self *GlobalController) customCommand() error { FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(), HandleConfirm: func(command string) error { self.c.GetAppState().CustomCommandsHistory = utils.Limit( - utils.Uniq( - append(self.c.GetAppState().CustomCommandsHistory, command), - ), + lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)), 1000, ) @@ -57,7 +57,7 @@ func (self *GlobalController) customCommand() error { func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { // reversing so that we display the latest command first - history := utils.Reverse(self.c.GetAppState().CustomCommandsHistory) + history := list.Reverse(self.c.GetAppState().CustomCommandsHistory) return helpers.FuzzySearchFunc(history) } diff --git a/pkg/gui/controllers/helpers/cherry_pick_helper.go b/pkg/gui/controllers/helpers/cherry_pick_helper.go index badbf0dfe..e4a9b3e81 100644 --- a/pkg/gui/controllers/helpers/cherry_pick_helper.go +++ b/pkg/gui/controllers/helpers/cherry_pick_helper.go @@ -1,11 +1,13 @@ package helpers import ( + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/modes/cherrypicking" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) type CherryPickHelper struct { @@ -63,13 +65,13 @@ func (self *CherryPickHelper) CopyRange(selectedIndex int, commitsList []*models return err } - commitShaMap := self.CherryPickedCommitShaMap() + commitSet := self.CherryPickedCommitShaSet() // find the last commit that is copied that's above our position // if there are none, startIndex = 0 startIndex := 0 for index, commit := range commitsList[0:selectedIndex] { - if commitShaMap[commit.Sha] { + if commitSet.Includes(commit.Sha) { startIndex = index } } @@ -105,25 +107,23 @@ func (self *CherryPickHelper) Reset() error { return self.rerender() } -func (self *CherryPickHelper) CherryPickedCommitShaMap() map[string]bool { - commitShaMap := map[string]bool{} - for _, commit := range self.getData().CherryPickedCommits { - commitShaMap[commit.Sha] = true - } - return commitShaMap +func (self *CherryPickHelper) CherryPickedCommitShaSet() *set.Set[string] { + shas := lo.Map(self.getData().CherryPickedCommits, func(commit *models.Commit, _ int) string { + return commit.Sha + }) + return set.NewFromSlice(shas) } func (self *CherryPickHelper) add(selectedCommit *models.Commit, commitsList []*models.Commit) { - commitShaMap := self.CherryPickedCommitShaMap() - commitShaMap[selectedCommit.Sha] = true + commitSet := self.CherryPickedCommitShaSet() + commitSet.Add(selectedCommit.Sha) - newCommits := []*models.Commit{} - for _, commit := range commitsList { - if commitShaMap[commit.Sha] { - // duplicating just the things we need to put in the rebase TODO list - newCommits = append(newCommits, &models.Commit{Name: commit.Name, Sha: commit.Sha}) - } - } + commitsInSet := lo.Filter(commitsList, func(commit *models.Commit, _ int) bool { + return commitSet.Includes(commit.Sha) + }) + newCommits := lo.Map(commitsInSet, func(commit *models.Commit, _ int) *models.Commit { + return &models.Commit{Name: commit.Name, Sha: commit.Sha} + }) self.getData().CherryPickedCommits = newCommits } diff --git a/pkg/gui/filetree/collapsed_paths.go b/pkg/gui/filetree/collapsed_paths.go index 02c0b4303..903999b37 100644 --- a/pkg/gui/filetree/collapsed_paths.go +++ b/pkg/gui/filetree/collapsed_paths.go @@ -1,20 +1,38 @@ package filetree -type CollapsedPaths map[string]bool +import "github.com/jesseduffield/generics/set" -func (cp CollapsedPaths) ExpandToPath(path string) { +type CollapsedPaths struct { + collapsedPaths *set.Set[string] +} + +func NewCollapsedPaths() *CollapsedPaths { + return &CollapsedPaths{ + collapsedPaths: set.New[string](), + } +} + +func (self *CollapsedPaths) ExpandToPath(path string) { // need every directory along the way splitPath := split(path) for i := range splitPath { dir := join(splitPath[0 : i+1]) - cp[dir] = false + self.collapsedPaths.Remove(dir) } } -func (cp CollapsedPaths) IsCollapsed(path string) bool { - return cp[path] +func (self *CollapsedPaths) IsCollapsed(path string) bool { + return self.collapsedPaths.Includes(path) } -func (cp CollapsedPaths) ToggleCollapsed(path string) { - cp[path] = !cp[path] +func (self *CollapsedPaths) Collapse(path string) { + self.collapsedPaths.Add(path) +} + +func (self *CollapsedPaths) ToggleCollapsed(path string) { + if self.collapsedPaths.Includes(path) { + self.collapsedPaths.Remove(path) + } else { + self.collapsedPaths.Add(path) + } } diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go index a8f7d0a95..ac2057da5 100644 --- a/pkg/gui/filetree/commit_file_node.go +++ b/pkg/gui/filetree/commit_file_node.go @@ -100,7 +100,7 @@ func (s *CommitFileNode) EveryFile(test func(file *models.CommitFile) bool) bool }) } -func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNode { +func (n *CommitFileNode) Flatten(collapsedPaths *CollapsedPaths) []*CommitFileNode { results := flatten(n, collapsedPaths) nodes := make([]*CommitFileNode, len(results)) for i, result := range results { @@ -110,7 +110,7 @@ func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNo return nodes } -func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileNode { +func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *CommitFileNode { if node == nil { return nil } @@ -124,11 +124,11 @@ func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string] return result.(*CommitFileNode) } -func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) { return getIndexForPath(node, path, collapsedPaths) } -func (node *CommitFileNode) Size(collapsedPaths map[string]bool) int { +func (node *CommitFileNode) Size(collapsedPaths *CollapsedPaths) int { if node == nil { return 0 } diff --git a/pkg/gui/filetree/commit_file_tree.go b/pkg/gui/filetree/commit_file_tree.go index 055e273f3..e539c9dea 100644 --- a/pkg/gui/filetree/commit_file_tree.go +++ b/pkg/gui/filetree/commit_file_tree.go @@ -19,7 +19,7 @@ type CommitFileTree struct { tree *CommitFileNode showTree bool log *logrus.Entry - collapsedPaths CollapsedPaths + collapsedPaths *CollapsedPaths } var _ ICommitFileTree = &CommitFileTree{} @@ -29,7 +29,7 @@ func NewCommitFileTree(getFiles func() []*models.CommitFile, log *logrus.Entry, getFiles: getFiles, log: log, showTree: showTree, - collapsedPaths: CollapsedPaths{}, + collapsedPaths: NewCollapsedPaths(), } } @@ -88,7 +88,7 @@ func (self *CommitFileTree) Tree() INode { return self.tree } -func (self *CommitFileTree) CollapsedPaths() CollapsedPaths { +func (self *CommitFileTree) CollapsedPaths() *CollapsedPaths { return self.collapsedPaths } diff --git a/pkg/gui/filetree/file_node.go b/pkg/gui/filetree/file_node.go index 841f723fc..e73504321 100644 --- a/pkg/gui/filetree/file_node.go +++ b/pkg/gui/filetree/file_node.go @@ -87,7 +87,7 @@ func (s *FileNode) Any(test func(node *FileNode) bool) bool { }) } -func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { +func (n *FileNode) Flatten(collapsedPaths *CollapsedPaths) []*FileNode { results := flatten(n, collapsedPaths) nodes := make([]*FileNode, len(results)) for i, result := range results { @@ -97,7 +97,7 @@ func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { return nodes } -func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileNode { +func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *FileNode { if node == nil { return nil } @@ -111,11 +111,11 @@ func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) return result.(*FileNode) } -func (node *FileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *FileNode) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) { return getIndexForPath(node, path, collapsedPaths) } -func (node *FileNode) Size(collapsedPaths map[string]bool) int { +func (node *FileNode) Size(collapsedPaths *CollapsedPaths) int { if node == nil { return 0 } diff --git a/pkg/gui/filetree/file_tree.go b/pkg/gui/filetree/file_tree.go index 0d0524470..47d7f32f2 100644 --- a/pkg/gui/filetree/file_tree.go +++ b/pkg/gui/filetree/file_tree.go @@ -27,7 +27,7 @@ type ITree interface { IsCollapsed(path string) bool ToggleCollapsed(path string) Tree() INode - CollapsedPaths() CollapsedPaths + CollapsedPaths() *CollapsedPaths } type IFileTree interface { @@ -48,7 +48,7 @@ type FileTree struct { showTree bool log *logrus.Entry filter FileTreeDisplayFilter - collapsedPaths CollapsedPaths + collapsedPaths *CollapsedPaths } func NewFileTree(getFiles func() []*models.File, log *logrus.Entry, showTree bool) *FileTree { @@ -57,7 +57,7 @@ func NewFileTree(getFiles func() []*models.File, log *logrus.Entry, showTree boo log: log, showTree: showTree, filter: DisplayAll, - collapsedPaths: CollapsedPaths{}, + collapsedPaths: NewCollapsedPaths(), } } @@ -164,7 +164,7 @@ func (self *FileTree) Tree() INode { return self.tree } -func (self *FileTree) CollapsedPaths() CollapsedPaths { +func (self *FileTree) CollapsedPaths() *CollapsedPaths { return self.collapsedPaths } diff --git a/pkg/gui/filetree/inode.go b/pkg/gui/filetree/inode.go index 7d9035fe3..7c8b9fb75 100644 --- a/pkg/gui/filetree/inode.go +++ b/pkg/gui/filetree/inode.go @@ -90,11 +90,11 @@ func every(node INode, test func(INode) bool) bool { return true } -func flatten(node INode, collapsedPaths map[string]bool) []INode { +func flatten(node INode, collapsedPaths *CollapsedPaths) []INode { result := []INode{} result = append(result, node) - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { result = append(result, flatten(child, collapsedPaths)...) } @@ -103,20 +103,20 @@ func flatten(node INode, collapsedPaths map[string]bool) []INode { return result } -func getNodeAtIndex(node INode, index int, collapsedPaths map[string]bool) INode { +func getNodeAtIndex(node INode, index int, collapsedPaths *CollapsedPaths) INode { foundNode, _ := getNodeAtIndexAux(node, index, collapsedPaths) return foundNode } -func getNodeAtIndexAux(node INode, index int, collapsedPaths map[string]bool) (INode, int) { +func getNodeAtIndexAux(node INode, index int, collapsedPaths *CollapsedPaths) (INode, int) { offset := 1 if index == 0 { return node, offset } - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { foundNode, offsetChange := getNodeAtIndexAux(child, index-offset, collapsedPaths) offset += offsetChange @@ -129,14 +129,14 @@ func getNodeAtIndexAux(node INode, index int, collapsedPaths map[string]bool) (I return nil, offset } -func getIndexForPath(node INode, path string, collapsedPaths map[string]bool) (int, bool) { +func getIndexForPath(node INode, path string, collapsedPaths *CollapsedPaths) (int, bool) { offset := 0 if node.GetPath() == path { return offset, true } - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { offsetChange, found := getIndexForPath(child, path, collapsedPaths) offset += offsetChange + 1 @@ -149,10 +149,10 @@ func getIndexForPath(node INode, path string, collapsedPaths map[string]bool) (i return offset, false } -func size(node INode, collapsedPaths map[string]bool) int { +func size(node INode, collapsedPaths *CollapsedPaths) int { output := 1 - if !collapsedPaths[node.GetPath()] { + if !collapsedPaths.IsCollapsed(node.GetPath()) { for _, child := range node.GetChildren() { output += size(child, collapsedPaths) } diff --git a/pkg/gui/list_context_config.go b/pkg/gui/list_context_config.go index 010a2f0b1..dcea5a936 100644 --- a/pkg/gui/list_context_config.go +++ b/pkg/gui/list_context_config.go @@ -123,7 +123,7 @@ func (gui *Gui) branchCommitsListContext() *context.LocalCommitsContext { return presentation.GetCommitListDisplayStrings( gui.State.Model.Commits, gui.State.ScreenMode != SCREEN_NORMAL, - gui.helpers.CherryPick.CherryPickedCommitShaMap(), + gui.helpers.CherryPick.CherryPickedCommitShaSet(), gui.State.Modes.Diffing.Ref, gui.c.UserConfig.Git.ParseEmoji, selectedCommitSha, @@ -155,7 +155,7 @@ func (gui *Gui) subCommitsListContext() *context.SubCommitsContext { return presentation.GetCommitListDisplayStrings( gui.State.Model.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, - gui.helpers.CherryPick.CherryPickedCommitShaMap(), + gui.helpers.CherryPick.CherryPickedCommitShaSet(), gui.State.Modes.Diffing.Ref, gui.c.UserConfig.Git.ParseEmoji, selectedCommitSha, @@ -199,7 +199,7 @@ func (gui *Gui) reflogCommitsListContext() *context.ReflogCommitsContext { return presentation.GetReflogCommitListDisplayStrings( gui.State.Model.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, - gui.helpers.CherryPick.CherryPickedCommitShaMap(), + gui.helpers.CherryPick.CherryPickedCommitShaSet(), gui.State.Modes.Diffing.Ref, gui.c.UserConfig.Git.ParseEmoji, ) diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go index 0073bb041..54f08ed50 100644 --- a/pkg/gui/options_menu_panel.go +++ b/pkg/gui/options_menu_panel.go @@ -7,7 +7,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" ) func (gui *Gui) getBindings(context types.Context) []*types.Binding { @@ -26,7 +26,7 @@ func (gui *Gui) getBindings(context types.Context) []*types.Binding { bindingsGlobal = append(bindingsGlobal, binding) } else if binding.Tag == "navigation" { bindingsNavigation = append(bindingsNavigation, binding) - } else if utils.IncludesString(binding.Contexts, string(context.GetKey())) { + } else if lo.Contains(binding.Contexts, string(context.GetKey())) { bindingsPanel = append(bindingsPanel, binding) } } @@ -45,17 +45,9 @@ func (gui *Gui) getBindings(context types.Context) []*types.Binding { // We shouldn't really need to do this. We should define alternative keys for the same // handler in the keybinding struct. func uniqueBindings(bindings []*types.Binding) []*types.Binding { - keys := make(map[string]bool) - result := make([]*types.Binding, 0) - - for _, binding := range bindings { - if _, ok := keys[binding.Description]; !ok { - keys[binding.Description] = true - result = append(result, binding) - } - } - - return result + return lo.UniqBy(bindings, func(binding *types.Binding) string { + return binding.Description + }) } func (gui *Gui) displayDescription(binding *types.Binding) string { diff --git a/pkg/gui/patch_building_panel.go b/pkg/gui/patch_building_panel.go index e734433c4..cae6167a4 100644 --- a/pkg/gui/patch_building_panel.go +++ b/pkg/gui/patch_building_panel.go @@ -1,8 +1,6 @@ package gui -import ( - "github.com/jesseduffield/lazygit/pkg/utils" -) +import "github.com/samber/lo" func (gui *Gui) refreshPatchBuildingPanel(selectedLineIdx int) error { if !gui.git.Patch.PatchManager.Active() { @@ -68,7 +66,7 @@ func (gui *Gui) handleToggleSelectionForPatch() error { if err != nil { return err } - currentLineIsStaged := utils.IncludesInt(includedLineIndices, state.GetSelectedLineIdx()) + currentLineIsStaged := lo.Contains(includedLineIndices, state.GetSelectedLineIdx()) if currentLineIsStaged { toggleFunc = gui.git.Patch.PatchManager.RemoveFileLineRange } diff --git a/pkg/gui/presentation/commits.go b/pkg/gui/presentation/commits.go index 2d5262e89..7ec4e78af 100644 --- a/pkg/gui/presentation/commits.go +++ b/pkg/gui/presentation/commits.go @@ -4,6 +4,7 @@ import ( "strings" "sync" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/presentation/authors" @@ -32,7 +33,7 @@ type bisectBounds struct { func GetCommitListDisplayStrings( commits []*models.Commit, fullDescription bool, - cherryPickedCommitShaMap map[string]bool, + cherryPickedCommitShaSet *set.Set[string], diffName string, parseEmoji bool, selectedCommitSha string, @@ -94,7 +95,7 @@ func GetCommitListDisplayStrings( bisectStatus = getBisectStatus(unfilteredIdx, commit.Sha, bisectInfo, bisectBounds) lines = append(lines, displayCommit( commit, - cherryPickedCommitShaMap, + cherryPickedCommitShaSet, diffName, parseEmoji, getGraphLine(unfilteredIdx), @@ -237,7 +238,7 @@ func getBisectStatusText(bisectStatus BisectStatus, bisectInfo *git_commands.Bis func displayCommit( commit *models.Commit, - cherryPickedCommitShaMap map[string]bool, + cherryPickedCommitShaSet *set.Set[string], diffName string, parseEmoji bool, graphLine string, @@ -245,7 +246,7 @@ func displayCommit( bisectStatus BisectStatus, bisectInfo *git_commands.BisectInfo, ) []string { - shaColor := getShaColor(commit, diffName, cherryPickedCommitShaMap, bisectStatus, bisectInfo) + shaColor := getShaColor(commit, diffName, cherryPickedCommitShaSet, bisectStatus, bisectInfo) bisectString := getBisectStatusText(bisectStatus, bisectInfo) actionString := "" @@ -313,7 +314,7 @@ func getBisectStatusColor(status BisectStatus) style.TextStyle { func getShaColor( commit *models.Commit, diffName string, - cherryPickedCommitShaMap map[string]bool, + cherryPickedCommitShaSet *set.Set[string], bisectStatus BisectStatus, bisectInfo *git_commands.BisectInfo, ) style.TextStyle { @@ -338,7 +339,7 @@ func getShaColor( if diffed { shaColor = theme.DiffTerminalColor - } else if cherryPickedCommitShaMap[commit.Sha] { + } else if cherryPickedCommitShaSet.Includes(commit.Sha) { shaColor = theme.CherryPickedCommitTextStyle } diff --git a/pkg/gui/presentation/commits_test.go b/pkg/gui/presentation/commits_test.go index b7fd23468..846d50d19 100644 --- a/pkg/gui/presentation/commits_test.go +++ b/pkg/gui/presentation/commits_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/gookit/color" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/utils" @@ -25,7 +26,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) { testName string commits []*models.Commit fullDescription bool - cherryPickedCommitShaMap map[string]bool + cherryPickedCommitShaSet *set.Set[string] diffName string parseEmoji bool selectedCommitSha string @@ -209,7 +210,7 @@ func TestGetCommitListDisplayStrings(t *testing.T) { result := GetCommitListDisplayStrings( s.commits, s.fullDescription, - s.cherryPickedCommitShaMap, + s.cherryPickedCommitShaSet, s.diffName, s.parseEmoji, s.selectedCommitSha, diff --git a/pkg/gui/presentation/files.go b/pkg/gui/presentation/files.go index be57a3510..13b2a64b0 100644 --- a/pkg/gui/presentation/files.go +++ b/pkg/gui/presentation/files.go @@ -66,7 +66,7 @@ func RenderCommitFileTree( func renderAux( s filetree.INode, - collapsedPaths filetree.CollapsedPaths, + collapsedPaths *filetree.CollapsedPaths, prefix string, depth int, renderLine func(filetree.INode, int) string, diff --git a/pkg/gui/presentation/reflog_commits.go b/pkg/gui/presentation/reflog_commits.go index 5437af8b5..72bb80ef6 100644 --- a/pkg/gui/presentation/reflog_commits.go +++ b/pkg/gui/presentation/reflog_commits.go @@ -1,6 +1,7 @@ package presentation import ( + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/theme" @@ -8,7 +9,7 @@ import ( "github.com/kyokomi/emoji/v2" ) -func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaMap map[string]bool, diffName string, parseEmoji bool) [][]string { +func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription bool, cherryPickedCommitShaSet *set.Set[string], diffName string, parseEmoji bool) [][]string { lines := make([][]string, len(commits)) var displayFunc func(*models.Commit, bool, bool, bool) []string @@ -20,7 +21,7 @@ func GetReflogCommitListDisplayStrings(commits []*models.Commit, fullDescription for i := range commits { diffed := commits[i].Sha == diffName - cherryPicked := cherryPickedCommitShaMap[commits[i].Sha] + cherryPicked := cherryPickedCommitShaSet.Includes(commits[i].Sha) lines[i] = displayFunc(commits[i], cherryPicked, diffed, parseEmoji) } diff --git a/pkg/gui/refresh.go b/pkg/gui/refresh.go index 2a47efc7a..5252d7ec9 100644 --- a/pkg/gui/refresh.go +++ b/pkg/gui/refresh.go @@ -5,6 +5,7 @@ import ( "strings" "sync" + "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/loaders" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/types/enums" @@ -52,14 +53,6 @@ func getModeName(mode types.RefreshMode) string { } } -func arrToMap(arr []types.RefreshableView) map[types.RefreshableView]bool { - output := map[types.RefreshableView]bool{} - for _, el := range arr { - output[el] = true - } - return output -} - func (gui *Gui) Refresh(options types.RefreshOptions) error { if options.Scope == nil { gui.c.Log.Infof( @@ -77,9 +70,9 @@ func (gui *Gui) Refresh(options types.RefreshOptions) error { wg := sync.WaitGroup{} f := func() { - var scopeMap map[types.RefreshableView]bool + var scopeSet *set.Set[types.RefreshableView] if len(options.Scope) == 0 { - scopeMap = arrToMap([]types.RefreshableView{ + scopeSet = set.NewFromSlice([]types.RefreshableView{ types.COMMITS, types.BRANCHES, types.FILES, @@ -91,7 +84,7 @@ func (gui *Gui) Refresh(options types.RefreshOptions) error { types.BISECT_INFO, }) } else { - scopeMap = arrToMap(options.Scope) + scopeSet = set.NewFromSlice(options.Scope) } refresh := func(f func()) { @@ -106,27 +99,27 @@ func (gui *Gui) Refresh(options types.RefreshOptions) error { }() } - if scopeMap[types.COMMITS] || scopeMap[types.BRANCHES] || scopeMap[types.REFLOG] || scopeMap[types.BISECT_INFO] { + if scopeSet.Includes(types.COMMITS) || scopeSet.Includes(types.BRANCHES) || scopeSet.Includes(types.REFLOG) || scopeSet.Includes(types.BISECT_INFO) { refresh(gui.refreshCommits) - } else if scopeMap[types.REBASE_COMMITS] { + } else if scopeSet.Includes(types.REBASE_COMMITS) { // the above block handles rebase commits so we only need to call this one // if we've asked specifically for rebase commits and not those other things refresh(func() { _ = gui.refreshRebaseCommits() }) } - if scopeMap[types.FILES] || scopeMap[types.SUBMODULES] { + if scopeSet.Includes(types.FILES) || scopeSet.Includes(types.SUBMODULES) { refresh(func() { _ = gui.refreshFilesAndSubmodules() }) } - if scopeMap[types.STASH] { + if scopeSet.Includes(types.STASH) { refresh(func() { _ = gui.refreshStashEntries() }) } - if scopeMap[types.TAGS] { + if scopeSet.Includes(types.TAGS) { refresh(func() { _ = gui.refreshTags() }) } - if scopeMap[types.REMOTES] { + if scopeSet.Includes(types.REMOTES) { refresh(func() { _ = gui.refreshRemotes() }) } diff --git a/pkg/utils/slice.go b/pkg/utils/slice.go index 123fc7df9..6971c9367 100644 --- a/pkg/utils/slice.go +++ b/pkg/utils/slice.go @@ -1,29 +1,5 @@ package utils -// IncludesString if the list contains the string -func IncludesString(list []string, a string) bool { - return IncludesStringFunc(list, func(b string) bool { return b == a }) -} - -func IncludesStringFunc(list []string, fn func(string) bool) bool { - for _, b := range list { - if fn(b) { - return true - } - } - return false -} - -// IncludesInt if the list contains the Int -func IncludesInt(list []int, a int) bool { - for _, b := range list { - if b == a { - return true - } - } - return false -} - // NextIndex returns the index of the element that comes after the given number func NextIndex(numbers []int, currentNumber int) int { for index, number := range numbers { @@ -45,44 +21,6 @@ func PrevIndex(numbers []int, currentNumber int) int { return 0 } -// UnionInt returns the union of two int arrays -func UnionInt(a, b []int) []int { - m := make(map[int]bool) - - for _, item := range a { - m[item] = true - } - - for _, item := range b { - if _, ok := m[item]; !ok { - // this does not mutate the original a slice - // though it does mutate the backing array I believe - // but that doesn't matter because if you later want to append to the - // original a it must see that the backing array has been changed - // and create a new one - a = append(a, item) - } - } - return a -} - -// DifferenceInt returns the difference of two int arrays -func DifferenceInt(a, b []int) []int { - result := []int{} - m := make(map[int]bool) - - for _, item := range b { - m[item] = true - } - - for _, item := range a { - if _, ok := m[item]; !ok { - result = append(result, item) - } - } - return result -} - // NextIntInCycle returns the next int in a slice, returning to the first index if we've reached the end func NextIntInCycle(sl []int, current int) int { for i, val := range sl { @@ -121,19 +59,6 @@ func StringArraysOverlap(strArrA []string, strArrB []string) bool { return false } -func Uniq(values []string) []string { - added := make(map[string]bool) - result := make([]string, 0, len(values)) - for _, value := range values { - if added[value] { - continue - } - added[value] = true - result = append(result, value) - } - return result -} - func Limit(values []string, limit int) []string { if len(values) > limit { return values[:limit] @@ -141,14 +66,6 @@ func Limit(values []string, limit int) []string { return values } -func Reverse(values []string) []string { - result := make([]string, len(values)) - for i, val := range values { - result[len(values)-i-1] = val - } - return result -} - func LimitStr(value string, limit int) string { n := 0 for i := range value { diff --git a/pkg/utils/slice_test.go b/pkg/utils/slice_test.go index fc80a46d7..e66edcd61 100644 --- a/pkg/utils/slice_test.go +++ b/pkg/utils/slice_test.go @@ -6,42 +6,6 @@ import ( "github.com/stretchr/testify/assert" ) -// TestIncludesString is a function. -func TestIncludesString(t *testing.T) { - type scenario struct { - list []string - element string - expected bool - } - - scenarios := []scenario{ - { - []string{"a", "b"}, - "a", - true, - }, - { - []string{"a", "b"}, - "c", - false, - }, - { - []string{"a", "b"}, - "", - false, - }, - { - []string{""}, - "", - true, - }, - } - - for _, s := range scenarios { - assert.EqualValues(t, s.expected, IncludesString(s.list, s.element)) - } -} - func TestNextIndex(t *testing.T) { type scenario struct { testName string @@ -169,26 +133,6 @@ func TestEscapeSpecialChars(t *testing.T) { } } -func TestUniq(t *testing.T) { - for _, test := range []struct { - values []string - want []string - }{ - { - values: []string{"a", "b", "c"}, - want: []string{"a", "b", "c"}, - }, - { - values: []string{"a", "b", "a", "b", "c"}, - want: []string{"a", "b", "c"}, - }, - } { - if got := Uniq(test.values); !assert.EqualValues(t, got, test.want) { - t.Errorf("Uniq(%v) = %v; want %v", test.values, got, test.want) - } - } -} - func TestLimit(t *testing.T) { for _, test := range []struct { values []string @@ -232,26 +176,6 @@ func TestLimit(t *testing.T) { } } -func TestReverse(t *testing.T) { - for _, test := range []struct { - values []string - want []string - }{ - { - values: []string{"a", "b", "c"}, - want: []string{"c", "b", "a"}, - }, - { - values: []string{}, - want: []string{}, - }, - } { - if got := Reverse(test.values); !assert.EqualValues(t, got, test.want) { - t.Errorf("Reverse(%v) = %v; want %v", test.values, got, test.want) - } - } -} - func TestLimitStr(t *testing.T) { for _, test := range []struct { values string diff --git a/vendor/github.com/jesseduffield/generics/LICENSE b/vendor/github.com/jesseduffield/generics/LICENSE new file mode 100644 index 000000000..2a7175dcc --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/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/generics/hashmap/functions.go b/vendor/github.com/jesseduffield/generics/hashmap/functions.go new file mode 100644 index 000000000..526222b1f --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/hashmap/functions.go @@ -0,0 +1,35 @@ +package hashmap + +func Keys[Key comparable, Value any](m map[Key]Value) []Key { + keys := make([]Key, 0, len(m)) + for key := range m { + keys = append(keys, key) + } + return keys +} + +func Values[Key comparable, Value any](m map[Key]Value) []Value { + values := make([]Value, 0, len(m)) + for _, value := range m { + values = append(values, value) + } + return values +} + +func TransformValues[Key comparable, Value any, NewValue any]( + m map[Key]Value, fn func(Value) NewValue, +) map[Key]NewValue { + output := make(map[Key]NewValue) + for key, value := range m { + output[key] = fn(value) + } + return output +} + +func TransformKeys[Key comparable, Value any, NewKey comparable](m map[Key]Value, fn func(Key) NewKey) map[NewKey]Value { + output := make(map[NewKey]Value) + for key, value := range m { + output[fn(key)] = value + } + return output +} diff --git a/vendor/github.com/jesseduffield/generics/list/comparable_list.go b/vendor/github.com/jesseduffield/generics/list/comparable_list.go new file mode 100644 index 000000000..21d56a80b --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/comparable_list.go @@ -0,0 +1,49 @@ +package list + +import ( + "golang.org/x/exp/slices" +) + +type ComparableList[T comparable] struct { + *List[T] +} + +func NewComparable[T comparable]() *ComparableList[T] { + return &ComparableList[T]{List: New[T]()} +} + +func NewComparableFromSlice[T comparable](slice []T) *ComparableList[T] { + return &ComparableList[T]{List: NewFromSlice(slice)} +} + +func (l *ComparableList[T]) Equal(other *ComparableList[T]) bool { + return l.EqualSlice(other.ToSlice()) +} + +func (l *ComparableList[T]) EqualSlice(other []T) bool { + return slices.Equal(l.ToSlice(), other) +} + +func (l *ComparableList[T]) Compact() { + l.slice = slices.Compact(l.slice) +} + +func (l *ComparableList[T]) Index(needle T) int { + return slices.Index(l.slice, needle) +} + +func (l *ComparableList[T]) Contains(needle T) bool { + return slices.Contains(l.slice, needle) +} + +func (l *ComparableList[T]) SortFuncInPlace(test func(a T, b T) bool) { + slices.SortFunc(l.slice, test) +} + +func (l *ComparableList[T]) SortFunc(test func(a T, b T) bool) *ComparableList[T] { + newSlice := slices.Clone(l.slice) + + slices.SortFunc(newSlice, test) + + return NewComparableFromSlice(newSlice) +} diff --git a/vendor/github.com/jesseduffield/generics/list/functions.go b/vendor/github.com/jesseduffield/generics/list/functions.go new file mode 100644 index 000000000..21578b82f --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/functions.go @@ -0,0 +1,72 @@ +package list + +func Some[T any](slice []T, test func(T) bool) bool { + for _, value := range slice { + if test(value) { + return true + } + } + + return false +} + +func Every[T any](slice []T, test func(T) bool) bool { + for _, value := range slice { + if !test(value) { + return false + } + } + + return true +} + +func Map[T any, V any](slice []T, f func(T) V) []V { + result := make([]V, len(slice)) + for i, value := range slice { + result[i] = f(value) + } + + return result +} + +func MapInPlace[T any](slice []T, f func(T) T) { + for i, value := range slice { + slice[i] = f(value) + } +} + +func Filter[T any](slice []T, test func(T) bool) []T { + result := make([]T, 0) + for _, element := range slice { + if test(element) { + result = append(result, element) + } + } + return result +} + +func FilterInPlace[T any](slice []T, test func(T) bool) []T { + newLength := 0 + for _, element := range slice { + if test(element) { + slice[newLength] = element + newLength++ + } + } + + return slice[:newLength] +} + +func Reverse[T any](slice []T) []T { + result := make([]T, len(slice)) + for i := range slice { + result[i] = slice[len(slice)-1-i] + } + return result +} + +func ReverseInPlace[T any](slice []T) { + for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 { + slice[i], slice[j] = slice[j], slice[i] + } +} diff --git a/vendor/github.com/jesseduffield/generics/list/list.go b/vendor/github.com/jesseduffield/generics/list/list.go new file mode 100644 index 000000000..2b0f43010 --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/list/list.go @@ -0,0 +1,117 @@ +package list + +import ( + "golang.org/x/exp/slices" +) + +type List[T any] struct { + slice []T +} + +func New[T any]() *List[T] { + return &List[T]{} +} + +func NewFromSlice[T any](slice []T) *List[T] { + return &List[T]{slice: slice} +} + +func (l *List[T]) ToSlice() []T { + return l.slice +} + +// Mutative methods + +func (l *List[T]) Push(v T) { + l.slice = append(l.slice, v) +} + +func (l *List[T]) Pop() { + l.slice = l.slice[0 : len(l.slice)-1] +} + +func (l *List[T]) Insert(index int, values ...T) { + l.slice = slices.Insert(l.slice, index, values...) +} + +func (l *List[T]) Append(values ...T) { + l.slice = append(l.slice, values...) +} + +func (l *List[T]) Prepend(values ...T) { + l.slice = append(values, l.slice...) +} + +func (l *List[T]) Remove(index int) { + l.Delete(index, index+1) +} + +func (l *List[T]) Delete(from int, to int) { + l.slice = slices.Delete(l.slice, from, to) +} + +func (l *List[T]) FilterInPlace(test func(value T) bool) { + l.slice = FilterInPlace(l.slice, test) +} + +func (l *List[T]) MapInPlace(f func(value T) T) { + MapInPlace(l.slice, f) +} + +func (l *List[T]) ReverseInPlace() { + ReverseInPlace(l.slice) +} + +// Non-mutative methods + +// Similar to Append but we leave the original slice untouched and return a new list +func (l *List[T]) Concat(values ...T) *List[T] { + newSlice := make([]T, 0, len(l.slice)+len(values)) + newSlice = append(newSlice, l.slice...) + newSlice = append(newSlice, values...) + return &List[T]{slice: newSlice} +} + +func (l *List[T]) Filter(test func(value T) bool) *List[T] { + return NewFromSlice(Filter(l.slice, test)) +} + +// Unfortunately this does not support mapping from one type to another +// because Go does not yet (and may never) support methods defining their own +// type parameters. For that functionality you'll need to use the standalone +// Map function instead +func (l *List[T]) Map(f func(value T) T) *List[T] { + return NewFromSlice(Map(l.slice, f)) +} + +func (l *List[T]) Clone() *List[T] { + return NewFromSlice(slices.Clone(l.slice)) +} + +func (l *List[T]) Some(test func(value T) bool) bool { + return Some(l.slice, test) +} + +func (l *List[T]) Every(test func(value T) bool) bool { + return Every(l.slice, test) +} + +func (l *List[T]) IndexFunc(f func(T) bool) int { + return slices.IndexFunc(l.slice, f) +} + +func (l *List[T]) ContainsFunc(f func(T) bool) bool { + return l.IndexFunc(f) != -1 +} + +func (l *List[T]) Reverse() *List[T] { + return NewFromSlice(Reverse(l.slice)) +} + +func (l *List[T]) IsEmpty() bool { + return len(l.slice) == 0 +} + +func (l *List[T]) Len() int { + return len(l.slice) +} diff --git a/vendor/github.com/jesseduffield/generics/set/set.go b/vendor/github.com/jesseduffield/generics/set/set.go new file mode 100644 index 000000000..3e9b9d9bf --- /dev/null +++ b/vendor/github.com/jesseduffield/generics/set/set.go @@ -0,0 +1,49 @@ +package set + +import "github.com/jesseduffield/generics/hashmap" + +type Set[T comparable] struct { + hashMap map[T]bool +} + +func New[T comparable]() *Set[T] { + return &Set[T]{hashMap: make(map[T]bool)} +} + +func NewFromSlice[T comparable](slice []T) *Set[T] { + hashMap := make(map[T]bool) + for _, value := range slice { + hashMap[value] = true + } + + return &Set[T]{hashMap: hashMap} +} + +func (s *Set[T]) Add(value T) { + s.hashMap[value] = true +} + +func (s *Set[T]) AddSlice(slice []T) { + for _, value := range slice { + s.Add(value) + } +} + +func (s *Set[T]) Remove(value T) { + delete(s.hashMap, value) +} + +func (s *Set[T]) RemoveSlice(slice []T) { + for _, value := range slice { + s.Remove(value) + } +} + +func (s *Set[T]) Includes(value T) bool { + return s.hashMap[value] +} + +// output slice is not necessarily in the same order that items were added +func (s *Set[T]) ToSlice() []T { + return hashmap.Keys(s.hashMap) +} diff --git a/vendor/github.com/samber/lo/.gitignore b/vendor/github.com/samber/lo/.gitignore new file mode 100644 index 000000000..3aa3a0ad4 --- /dev/null +++ b/vendor/github.com/samber/lo/.gitignore @@ -0,0 +1,36 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/go +# Edit at https://www.toptal.com/developers/gitignore?templates=go + +### Go ### +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +### Go Patch ### +/vendor/ +/Godeps/ + +# End of https://www.toptal.com/developers/gitignore/api/go + +cover.out +cover.html +.vscode diff --git a/vendor/github.com/samber/lo/CHANGELOG.md b/vendor/github.com/samber/lo/CHANGELOG.md new file mode 100644 index 000000000..aabeed120 --- /dev/null +++ b/vendor/github.com/samber/lo/CHANGELOG.md @@ -0,0 +1,71 @@ +# Changelog + +## 1.3.0 (2022-03-03) + +Last and Nth return errors + +## 1.2.0 (2022-03-03) + +Adding `lop.Map` and `lop.ForEach`. + +## 1.1.0 (2022-03-03) + +Adding `i int` param to `lo.Map()`, `lo.Filter()`, `lo.ForEach()` and `lo.Reduce()` predicates. + +## 1.0.0 (2022-03-02) + +*Initial release* + +Supported helpers for slices: + +- Filter +- Map +- Reduce +- ForEach +- Uniq +- UniqBy +- GroupBy +- Chunk +- Flatten +- Shuffle +- Reverse +- Fill +- ToMap + +Supported helpers for maps: + +- Keys +- Values +- Entries +- FromEntries +- Assign (maps merge) + +Supported intersection helpers: + +- Contains +- Every +- Some +- Intersect +- Difference + +Supported search helpers: + +- IndexOf +- LastIndexOf +- Find +- Min +- Max +- Last +- Nth + +Other functional programming helpers: + +- Ternary (1 line if/else statement) +- If / ElseIf / Else +- Switch / Case / Default +- ToPtr +- ToSlicePtr + +Constraints: + +- Clonable diff --git a/vendor/github.com/samber/lo/Dockerfile b/vendor/github.com/samber/lo/Dockerfile new file mode 100644 index 000000000..9f9f87192 --- /dev/null +++ b/vendor/github.com/samber/lo/Dockerfile @@ -0,0 +1,8 @@ + +FROM golang:1.18rc1-bullseye + +WORKDIR /go/src/github.com/samber/lo + +COPY Makefile go.* /go/src/github.com/samber/lo/ + +RUN make tools diff --git a/vendor/github.com/samber/lo/LICENSE b/vendor/github.com/samber/lo/LICENSE new file mode 100644 index 000000000..c3dc72d9a --- /dev/null +++ b/vendor/github.com/samber/lo/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Samuel Berthe + +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/samber/lo/Makefile b/vendor/github.com/samber/lo/Makefile new file mode 100644 index 000000000..11b09cd25 --- /dev/null +++ b/vendor/github.com/samber/lo/Makefile @@ -0,0 +1,51 @@ + +BIN=go +# BIN=go1.18beta1 + +go1.18beta1: + go install golang.org/dl/go1.18beta1@latest + go1.18beta1 download + +build: + ${BIN} build -v ./... + +test: + go test -race -v ./... +watch-test: + reflex -R assets.go -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 ./...' + +coverage: + ${BIN} test -v -coverprofile cover.out . + ${BIN} tool cover -html=cover.out -o cover.html + +# tools +tools: + ${BIN} install github.com/cespare/reflex@latest + ${BIN} install github.com/rakyll/gotest@latest + ${BIN} install github.com/psampaz/go-mod-outdated@latest + ${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 + go mod tidy + +lint: + golangci-lint run --timeout 60s --max-same-issues 50 ./... +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 + goweight diff --git a/vendor/github.com/samber/lo/README.md b/vendor/github.com/samber/lo/README.md new file mode 100644 index 000000000..48dfdc004 --- /dev/null +++ b/vendor/github.com/samber/lo/README.md @@ -0,0 +1,981 @@ +# lo + +![Build Status](https://github.com/samber/lo/actions/workflows/go.yml/badge.svg) +[![GoDoc](https://godoc.org/github.com/samber/lo?status.svg)](https://pkg.go.dev/github.com/samber/lo) +[![Go report](https://goreportcard.com/badge/github.com/samber/lo)](https://goreportcard.com/report/github.com/samber/lo) + +✨ **`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. + +As expected, benchmarks demonstrate that generics will be much faster than implementations based on the "reflect" package. Benchmarks also show similar performance gains compared to pure `for` loops. [See below](#-benchmark). + +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? + +I wanted a **short name**, similar to "Lodash" and no Go package currently uses this name. + +## 🚀 Install + +```sh +go get github.com/samber/lo +``` + +## 💡 Usage + +You can import `lo` using: + +```go +import ( + "github.com/samber/lo" + lop "github.com/samber/lo/parallel" +) +``` + +Then use one of the helpers below: + +```go +names := lo.Uniq[string]([]string{"Samuel", "Marc", "Samuel"}) +// []string{"Samuel", "Marc"} +``` + +Most of the time, the compiler will be able to infer the type so that you can call: `lo.Uniq([]string{...})`. + +## 🤠 Spec + +GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/samber/lo) + +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 + +Supported helpers for maps: + +- Keys +- Values +- Entries +- FromEntries +- Assign (merge of maps) +- MapValues + +Supported helpers for tuples: + +- Zip2 -> Zip9 +- Unzip2 -> Unzip9 + +Supported intersection helpers: + +- Contains +- ContainsBy +- Every +- Some +- Intersect +- Difference +- Union + +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 + +Constraints: + +- Clonable + +### Map + +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 { + return strconv.FormatInt(x, 10) +}) +// []string{"1", "2", "3", "4"} +``` + +Parallel processing: like `lo.Map()`, but the mapper function is called in a goroutine. Results are returned in the same order. + +```go +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"} +``` + +### FlatMap + +Manipulates a slice and transforms and flattens it to a slice of another type. + +```go +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"} +``` + +### 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 +``` + +### 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. + +```go +sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int { + return agg + item +}, 0) +// 10 +``` + +### ForEach + +Iterates over elements of a collection and invokes the function over each element. + +```go +import "github.com/samber/lo" + +lo.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) { + println(x) +}) +// prints "hello\nworld\n" +``` + +Parallel processing: like `lo.ForEach()`, but the callback is called as a goroutine. + +```go +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. + +```go +import "github.com/samber/lo" + +lo.Times[string](3, func(i int) string { + return strconv.FormatInt(int64(i), 10) +}) +// []string{"0", "1", "2"} +``` + +Parallel processing: like `lo.Times()`, but callback is called in goroutine. + +```go +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. + +```go +uniqValues := lo.Uniq[int]([]int{1, 2, 2, 1}) +// []int{1, 2} +``` + +### UniqBy + +Returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed. + +```go +uniqValues := lo.UniqBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int { + return i%3 +}) +// []int{0, 1, 2} +``` + +### GroupBy + +Returns an object composed of keys generated from the results of running each element of collection through iteratee. + +```go +import lo "github.com/samber/lo" + +groups := lo.GroupBy[int, int]([]int{0, 1, 2, 3, 4, 5}, func(i int) int { + return i%3 +}) +// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}} +``` + +Parallel processing: like `lo.GroupBy()`, but callback is called in goroutine. + +```go +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. + +```go +lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5}, 2) +// [][]int{{0, 1}, {2, 3}, {4, 5}} + +lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5, 6}, 2) +// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}} + +lo.Chunk[int]([]int{}, 2) +// [][]int{} + +lo.Chunk[int]([]int{0}, 2) +// [][]int{{0}} +``` + +### PartitionBy + +Returns an array of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee. + +```go +import lo "github.com/samber/lo" + +partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string { + if x < 0 { + return "negative" + } else if x%2 == 0 { + return "even" + } + return "odd" +}) +// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}} +``` + +Parallel processing: like `lo.PartitionBy()`, but callback is called in goroutine. Results are returned in the same order. + +```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 { + 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. + +```go +flat := lo.Flatten[int]([][]int{{0, 1}, {2, 3, 4, 5}}) +// []int{0, 1, 2, 3, 4, 5} +``` + +### Shuffle + +Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm. + +```go +randomOrder := lo.Shuffle[int]([]int{0, 1, 2, 3, 4, 5}) +// []int{0, 1, 2, 3, 4, 5} +``` + +### Reverse + +Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on. + +```go +reverseOder := lo.Reverse[int]([]int{0, 1, 2, 3, 4, 5}) +// []int{5, 4, 3, 2, 1, 0} +``` + +### Fill + +Fills elements of array with `initial` value. + +```go +type foo struct { + bar string +} + +func (f foo) Clone() foo { + return foo{f.bar} +} + +initializedSlice := lo.Fill[foo]([]foo{foo{"a"}, foo{"a"}}, foo{"b"}) +// []foo{foo{"b"}, foo{"b"}} +``` + +### Repeat + +Builds a slice with N copies of initial value. + +```go +type foo struct { + bar string +} + +func (f foo) Clone() foo { + return foo{f.bar} +} + +initializedSlice := lo.Repeat[foo](2, foo{"a"}) +// []foo{foo{"a"}, foo{"a"}} +``` + +### KeyBy + +Transforms a slice or an array of structs to a map based on a pivot callback. + +```go +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 := KeyBy[Character, string](characters, func(char Character) string { + return string(rune(char.code)) +}) +//map[a:{dir:left code:97} d:{dir:right code:100}] +``` + +### Drop + +Drops n elements from the beginning of a slice or array. + +```go +l := lo.Drop[int]([]int{0, 1, 2, 3, 4, 5}, 2) +// []int{2, 3, 4, 5} +``` + +### DropRight + +Drops n elements from the end of a slice or array. + +```go +l := lo.DropRight[int]([]int{0, 1, 2, 3, 4, 5}, 2) +// []int{0, 1, 2, 3} +``` + +### DropWhile + +Drop elements from the beginning of a slice or array while the predicate returns true. + +```go +l := lo.DropWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool { + return len(val) <= 2 +}) +// []string{"aaa", "aa", "a"} +``` + +### DropRightWhile + +Drop elements from the end of a slice or array while the predicate returns true. + +```go +l := lo.DropRightWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool { + return len(val) <= 2 +}) +// []string{"a", "aa", "aaa"} +``` + +### Keys + +Creates an array of the map keys. + +```go +keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2}) +// []string{"bar", "foo"} +``` + +### Values + +Creates an array of the map values. + +```go +values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2}) +// []int{1, 2} +``` + +### Entries + +Transforms a map into array of key/value pairs. + +```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, +// }, +// } +``` + +### FromEntries + +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} +``` + +### 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} +``` + +### 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](m, func(x int64, _ int) string { + return strconv.FormatInt(x, 10) +}) +// map[int]string{1: "1", 2: "2", 3: "3"} +``` + +### 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}} +``` + +### 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} +``` + +### Every + +Returns true if all elements of a subset are contained into a collection. + +```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 +``` + +### Some + +Returns true if at least 1 element of a subset is contained into a collection. + +```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 +``` + +### 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 := 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} +``` + +### 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 +``` + +### LastIndex + +Returns the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found. + +```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 +``` + +### Min + +Search the minimum value of a collection. + +```go +min := lo.Min[int]([]int{1, 2, 3}) +// 1 + +min := lo.Min[int]([]int{}) +// 0 +``` + +### Max + +Search the maximum value of a collection. + +```go +max := lo.Max[int]([]int{1, 2, 3}) +// 3 + +max := lo.Max[int]([]int{}) +// 0 +``` + +### Last + +Returns the last element of a collection or error if empty. + +```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[string](true, "a", "b") +// "a" + +result := lo.Ternary[string](false, "a", "b") +// "b" +``` + +### If / ElseIf / Else + +```go +result := lo.If[int](true, 1). + ElseIf(false, 2). + Else(3) +// 1 + +result := lo.If[int](false, 1). + ElseIf(true, 2). + Else(3) +// 2 + +result := lo.If[int](false, 1). + ElseIf(false, 2). + Else(3) +// 3 +``` + +### Switch / Case / Default + +```go +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: + +```go +result := lo.Switch[int, string](1). + CaseF(1, func() string { + return "1" + }). + CaseF(2, func() string { + return "2" + }). + DefaultF(func() string { + return "3" + }) +// "1" +``` + +### ToPtr + +Returns a pointer copy of value. + +```go +ptr := lo.ToPtr[string]("hello world") +// *string{"hello world"} +``` + +### ToSlicePtr + +Returns a slice of pointer copy of value. + +```go +ptr := lo.ToSlicePtr[string]([]string{"hello", "world"}) +// []*string{"hello", "world"} +``` + +### Attempt + +Invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned. + +```go +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 +``` + +### 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] + +result := RangeFrom[float64](1.0, 5); +// [1.0, 2.0, 3.0, 4.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); +// [] +``` + +For more advanced retry strategies (delay, exponential backoff...), please take a look on [cenkalti/backoff](https://github.com/cenkalti/backoff). + +## 🛩 Benchmark + +We executed a simple benchmark with the a dead-simple `lo.Map` loop: + +See the full implementation [here](./benchmark_test.go). + +```go +_ = lo.Map[int64](arr, func(x int64, i int) string { + return strconv.FormatInt(x, 10) +}) +``` + +**Result:** + +Here is a comparison between `lo.Map`, `lop.Map`, `go-funk` library and a simple Go `for` loop. + +``` +$ go test -benchmem -bench ./... +goos: linux +goarch: amd64 +pkg: github.com/samber/lo +cpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz +cpu: Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz +BenchmarkMap/lo.Map-8 8 132728237 ns/op 39998945 B/op 1000002 allocs/op +BenchmarkMap/lop.Map-8 2 503947830 ns/op 119999956 B/op 3000007 allocs/op +BenchmarkMap/reflect-8 2 826400560 ns/op 170326512 B/op 4000042 allocs/op +BenchmarkMap/for-8 9 126252954 ns/op 39998674 B/op 1000001 allocs/op +PASS +ok github.com/samber/lo 6.657s +``` + +- `lo.Map` is way faster (x7) than `go-funk`, a relection-based Map implementation. +- `lo.Map` have the same allocation profile than `for`. +- `lo.Map` is 4% slower than `for`. +- `lop.Map` is slower than `lo.Map` because it implies more memory allocation and locks. `lop.Map` will be usefull for long-running callbacks, such as i/o bound processing. +- `for` beats other implementations for memory and CPU. + +## 🤝 Contributing + +- Ping me on twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :)) +- Fork the [project](https://github.com/samber/lo) +- Fix [open issues](https://github.com/samber/lo/issues) or request new features + +Don't hesitate ;) + +### Install go 1.18 + +```bash +make go1.18beta1 +``` + +If your OS currently not default to Go 1.18, replace `BIN=go` by `BIN=go1.18beta1` in the Makefile. + +### With Docker + +```bash +docker-compose run --rm dev +``` + +### Without Docker + +```bash +# Install some dev dependencies +make tools + +# Run tests +make test +# or +make watch-test +``` + +## 👤 Authors + +- Samuel Berthe + +## 💫 Show your support + +Give a ⭐️ if this project helped you! + +[![support us](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/samber) + +## 📝 License + +Copyright © 2022 [Samuel Berthe](https://github.com/samber). + +This project is [MIT](./LICENSE) licensed. diff --git a/vendor/github.com/samber/lo/condition.go b/vendor/github.com/samber/lo/condition.go new file mode 100644 index 000000000..2d9862cc5 --- /dev/null +++ b/vendor/github.com/samber/lo/condition.go @@ -0,0 +1,99 @@ +package lo + +// Ternary is a 1 line if/else statement. +func Ternary[T any](condition bool, ifOutput T, elseOutput T) T { + if condition { + return ifOutput + } + + return elseOutput +} + +type ifElse[T any] struct { + result T + done bool +} + +// If. +func If[T any](condition bool, result T) *ifElse[T] { + if condition { + return &ifElse[T]{result, true} + } + + var t T + return &ifElse[T]{t, false} +} + +// ElseIf. +func (i *ifElse[T]) ElseIf(condition bool, result T) *ifElse[T] { + if !i.done && condition { + i.result = result + i.done = true + } + + return i +} + +// Else. +func (i *ifElse[T]) Else(result T) T { + if i.done { + return i.result + } + + return result +} + +type switchCase[T comparable, R any] struct { + predicate T + result R + done bool +} + +// Switch is a pure functional switch/case/default statement. +func Switch[T comparable, R any](predicate T) *switchCase[T, R] { + var result R + + return &switchCase[T, R]{ + predicate, + result, + false, + } +} + +// Case. +func (s *switchCase[T, R]) Case(val T, result R) *switchCase[T, R] { + if !s.done && s.predicate == val { + s.result = result + s.done = true + } + + return s +} + +// CaseF. +func (s *switchCase[T, R]) CaseF(val T, cb func() R) *switchCase[T, R] { + if !s.done && s.predicate == val { + s.result = cb() + s.done = true + } + + return s +} + +// Default. +func (s *switchCase[T, R]) Default(result R) R { + if !s.done { + s.result = result + } + + return s.result +} + +// DefaultF. +func (s *switchCase[T, R]) DefaultF(cb func() R) R { + if !s.done { + s.result = cb() + } + + return s.result +} diff --git a/vendor/github.com/samber/lo/constraints.go b/vendor/github.com/samber/lo/constraints.go new file mode 100644 index 000000000..c1f352968 --- /dev/null +++ b/vendor/github.com/samber/lo/constraints.go @@ -0,0 +1,6 @@ +package lo + +// Clonable defines a constraint of types having Clone() T method. +type Clonable[T any] interface { + Clone() T +} diff --git a/vendor/github.com/samber/lo/docker-compose.yml b/vendor/github.com/samber/lo/docker-compose.yml new file mode 100644 index 000000000..c6f3f652b --- /dev/null +++ b/vendor/github.com/samber/lo/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3' + +services: + dev: + build: . + volumes: + - ./:/go/src/github.com/samber/lo + working_dir: /go/src/github.com/samber/lo + command: bash -c 'make tools ; make watch-test' diff --git a/vendor/github.com/samber/lo/drop.go b/vendor/github.com/samber/lo/drop.go new file mode 100644 index 000000000..870b04aa3 --- /dev/null +++ b/vendor/github.com/samber/lo/drop.go @@ -0,0 +1,65 @@ +package lo + +//Drop drops n elements from the beginning of a slice or array. +func Drop[T any](collection []T, n int) []T { + if len(collection) <= n { + return make([]T, 0) + } + + result := make([]T, len(collection)-n) + for i := n; i < len(collection); i++ { + result[i-n] = collection[i] + } + + return result +} + +//DropWhile drops elements from the beginning of a slice or array while the predicate returns true. +func DropWhile[T any](collection []T, predicate func(T) bool) []T { + i := 0 + for ; i < len(collection); i++ { + if !predicate(collection[i]) { + break + } + } + + result := make([]T, len(collection)-i) + + for j := 0; i < len(collection); i, j = i+1, j+1 { + result[j] = collection[i] + } + + return result +} + +//DropRight drops n elements from the end of a slice or array. +func DropRight[T any](collection []T, n int) []T { + if len(collection) <= n { + return make([]T, 0) + } + + result := make([]T, len(collection)-n) + for i := len(collection) - 1 - n; i != 0; i-- { + result[i] = collection[i] + } + + return result +} + +//DropRightWhile drops elements from the end of a slice or array while the predicate returns true. +func DropRightWhile[T any](collection []T, predicate func(T) bool) []T { + i := len(collection) - 1 + for ; i >= 0; i-- { + if !predicate(collection[i]) { + break + } + } + + result := make([]T, i+1) + + for ; i >= 0; i-- { + result[i] = collection[i] + } + + return result +} diff --git a/vendor/github.com/samber/lo/find.go b/vendor/github.com/samber/lo/find.go new file mode 100644 index 000000000..e6d22ca05 --- /dev/null +++ b/vendor/github.com/samber/lo/find.go @@ -0,0 +1,157 @@ +package lo + +import ( + "fmt" + "math/rand" + "math" + "golang.org/x/exp/constraints" +) + +// import "golang.org/x/exp/constraints" + +// 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 IndexOf[T comparable](collection []T, element T) int { + for i, item := range collection { + if item == element { + return i + } + } + + return -1 +} + +// IndexOf returns the index at which the last occurrence of a value is found in an array or return -1 +// if the value cannot be found. +func LastIndexOf[T comparable](collection []T, element T) int { + length := len(collection) + + for i := length - 1; i >= 0; i-- { + if collection[i] == element { + return i + } + } + + return -1 +} + +// Find search an element in a slice based on a predicate. It returns element and true if element was found. +func Find[T any](collection []T, predicate func(T) bool) (T, bool) { + for _, item := range collection { + if predicate(item) { + return item, true + } + } + + var result T + return result, false +} + +// Min search the minimum value of a collection. +func Min[T constraints.Ordered](collection []T) T { + var min T + + if len(collection) == 0 { + return min + } + + min = collection[0] + + for i := 1; i < len(collection); i++ { + item := collection[i] + + // if item.Less(min) { + if item < min { + min = item + } + } + + return min +} + +// Max search the maximum value of a collection. +func Max[T constraints.Ordered](collection []T) T { + var max T + + if len(collection) == 0 { + return max + } + + max = collection[0] + + for i := 1; i < len(collection); i++ { + item := collection[i] + + if item > max { + max = item + } + } + + return max +} + +// Last returns the last element of a collection or error if empty. +func Last[T any](collection []T) (T, error) { + length := len(collection) + + if length == 0 { + var t T + return t, fmt.Errorf("last: cannot extract the last element of an empty slice") + } + + return collection[length-1], nil +} + +// 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 Nth[T any](collection []T, nth int) (T, error) { + if int(math.Abs(float64(nth))) >= len(collection) { + var t T + return t, fmt.Errorf("nth: %d out of slice bounds", nth) + } + + length := len(collection) + + if nth >= 0 { + return collection[nth], nil + } + + return collection[length+nth], nil +} + +// Sample returns a random item from collection. +func Sample[T any](collection []T) T { + size := len(collection) + if size == 0 { + return Empty[T]() + } + + return collection[rand.Intn(size)] +} + +// Samples returns N random unique items from collection. +func Samples[T any](collection []T, count int) []T { + size := len(collection) + + // put values into a map, for faster deletion + cOpy := make([]T, 0, size) + for _, v := range collection { + cOpy = append(cOpy, v) + } + + results := []T{} + + for i := 0; i < size && i < count; i++ { + copyLength := size - i + + index := rand.Intn(size - i) + results = append(results, cOpy[index]) + + // Removes element. + // It is faster to swap with last element and remove it. + cOpy[index] = cOpy[copyLength-1] + cOpy = cOpy[:copyLength-1] + } + + return results +} diff --git a/vendor/github.com/samber/lo/intersect.go b/vendor/github.com/samber/lo/intersect.go new file mode 100644 index 000000000..f720d1a2f --- /dev/null +++ b/vendor/github.com/samber/lo/intersect.go @@ -0,0 +1,131 @@ +package lo + +// Contains returns true if an element is present in a collection. +func Contains[T comparable](collection []T, element T) bool { + for _, item := range collection { + if item == element { + return true + } + } + + return false +} + +// ContainsBy returns true if predicate function return true. +func ContainsBy[T any](collection []T, predicate func(T) bool) bool { + for _, item := range collection { + if predicate(item) { + return true + } + } + + return false +} + +// Every returns true if all elements of a subset are contained into a collection. +func Every[T comparable](collection []T, subset []T) bool { + for _, elem := range subset { + if !Contains(collection, elem) { + return false + } + } + + return true +} + +// Some returns true if at least 1 element of a subset is contained into a collection. +func Some[T comparable](collection []T, subset []T) bool { + for _, elem := range subset { + if Contains(collection, elem) { + return true + } + } + + return false +} + +// Intersect returns the intersection between two collections. +func Intersect[T comparable](list1 []T, list2 []T) []T { + result := []T{} + seen := map[T]struct{}{} + + for _, elem := range list1 { + seen[elem] = struct{}{} + } + + for _, elem := range list2 { + if _, ok := seen[elem]; ok { + result = append(result, elem) + } + } + + return result +} + +// 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 Difference[T comparable](list1 []T, list2 []T) ([]T, []T) { + left := []T{} + right := []T{} + + seenLeft := map[T]struct{}{} + seenRight := map[T]struct{}{} + + for _, elem := range list1 { + seenLeft[elem] = struct{}{} + } + + for _, elem := range list2 { + seenRight[elem] = struct{}{} + } + + for _, elem := range list1 { + if _, ok := seenRight[elem]; !ok { + left = append(left, elem) + } + } + + for _, elem := range list2 { + if _, ok := seenLeft[elem]; !ok { + right = append(right, elem) + } + } + + return left, right +} + +// Union returns all distinct elements from both collections. +// result returns will not change the order of elements relatively. +func Union[T comparable](list1 []T, list2 []T) []T { + result := []T{} + + seen := map[T]struct{}{} + hasAdd := map[T]struct{}{} + + for _, e := range list1 { + seen[e] = struct{}{} + } + + for _, e := range list2 { + seen[e] = struct{}{} + } + + for _, e := range list1 { + if _, ok := seen[e]; ok { + result = append(result, e) + hasAdd[e] = struct{}{} + } + } + + for _, e := range list2 { + if _, ok := hasAdd[e]; ok { + continue + } + if _, ok := seen[e]; ok { + result = append(result, e) + } + } + + return result +} diff --git a/vendor/github.com/samber/lo/map.go b/vendor/github.com/samber/lo/map.go new file mode 100644 index 000000000..92c77d8ba --- /dev/null +++ b/vendor/github.com/samber/lo/map.go @@ -0,0 +1,72 @@ +package lo + +// Keys creates an array of the map keys. +func Keys[K comparable, V any](in map[K]V) []K { + result := make([]K, 0, len(in)) + + for k, _ := range in { + result = append(result, k) + } + + return result +} + +// Values creates an array of the map values. +func Values[K comparable, V any](in map[K]V) []V { + result := make([]V, 0, len(in)) + + for _, v := range in { + result = append(result, v) + } + + return result +} + +// Entries transforms a map into array of key/value pairs. +func Entries[K comparable, V any](in map[K]V) []Entry[K, V] { + entries := make([]Entry[K, V], 0, len(in)) + + for k, v := range in { + entries = append(entries, Entry[K, V]{ + Key: k, + Value: v, + }) + } + + return entries +} + +// FromEntries transforms an array of key/value pairs into a map. +func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V { + out := map[K]V{} + + for _, v := range entries { + out[v.Key] = v.Value + } + + return out +} + +// Assign merges multiple maps from left to right. +func Assign[K comparable, V any](maps ...map[K]V) map[K]V { + out := map[K]V{} + + for _, m := range maps { + for k, v := range m { + out[k] = v + } + } + + return out +} + +// MapValues manipulates a map values and transforms it to a map of another type. +func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) map[K]R { + result := map[K]R{} + + for k, v := range in { + result[k] = iteratee(v, k) + } + + return result +} \ No newline at end of file diff --git a/vendor/github.com/samber/lo/pointers.go b/vendor/github.com/samber/lo/pointers.go new file mode 100644 index 000000000..9c6f7fe33 --- /dev/null +++ b/vendor/github.com/samber/lo/pointers.go @@ -0,0 +1,19 @@ +package lo + +// ToPtr returns a pointer copy of value. +func ToPtr[T any](x T) *T { + return &x +} + +// ToPtr returns a slice of pointer copy of value. +func ToSlicePtr[T any](collection []T) []*T { + return Map(collection, func (x T, _ int) *T { + return &x + }) +} + +// Empty returns an empty value. +func Empty[T any]() T { + var t T + return t +} diff --git a/vendor/github.com/samber/lo/retry.go b/vendor/github.com/samber/lo/retry.go new file mode 100644 index 000000000..ebdb31d29 --- /dev/null +++ b/vendor/github.com/samber/lo/retry.go @@ -0,0 +1,19 @@ +package lo + +// Attempt invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned. +func Attempt(maxIteration int, f func(int) error) (int, error) { + var err error + + for i := 0; maxIteration <= 0 || i < maxIteration; i++ { + // for retries >= 0 { + err = f(i) + if err == nil { + return i + 1, nil + } + } + + return maxIteration, err +} + +// throttle ? +// debounce ? diff --git a/vendor/github.com/samber/lo/slice.go b/vendor/github.com/samber/lo/slice.go new file mode 100644 index 000000000..f5e989358 --- /dev/null +++ b/vendor/github.com/samber/lo/slice.go @@ -0,0 +1,242 @@ +package lo + +import ( + "math/rand" +) + +// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for. +func Filter[V any](collection []V, predicate func(V, int) bool) []V { + result := []V{} + + for i, item := range collection { + if predicate(item, i) { + result = append(result, item) + } + } + + return result +} + +// Map manipulates a slice and transforms it to a slice of another type. +func Map[T any, R any](collection []T, iteratee func(T, int) R) []R { + result := make([]R, len(collection)) + + for i, item := range collection { + result[i] = iteratee(item, i) + } + + return result +} + +// FlatMap manipulates a slice and transforms and flattens it to a slice of another type. +func FlatMap[T any, R any](collection []T, iteratee func(T, int) []R) []R { + result := []R{} + + for i, item := range collection { + result = append(result, iteratee(item, i)...) + } + + return result +} + +// Reduce reduces collection to a value which is the accumulated result of running each element in collection +// through accumulator, where each successive invocation is supplied the return value of the previous. +func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R { + for i, item := range collection { + initial = accumulator(initial, item, i) + } + + return initial +} + +// ForEach iterates over elements of collection and invokes iteratee for each element. +func ForEach[T any](collection []T, iteratee func(T, int)) { + for i, item := range collection { + iteratee(item, i) + } +} + +// Times invokes the iteratee n times, returning an array of the results of each invocation. +// The iteratee is invoked with index as argument. +func Times[T any](count int, iteratee func(int) T) []T { + result := make([]T, count) + + for i := 0; i < count; i++ { + result[i] = iteratee(i) + } + + return result +} + +// Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. +// The order of result values is determined by the order they occur in the array. +func Uniq[T comparable](collection []T) []T { + result := make([]T, 0, len(collection)) + seen := make(map[T]struct{}, len(collection)) + + for _, item := range collection { + if _, ok := seen[item]; ok { + continue + } + + seen[item] = struct{}{} + result = append(result, item) + } + + return result +} + +// Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. +// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is +// invoked for each element in array to generate the criterion by which uniqueness is computed. +func UniqBy[T any, U comparable](collection []T, iteratee func(T) U) []T { + result := make([]T, 0, len(collection)) + seen := make(map[U]struct{}, len(collection)) + + for _, item := range collection { + key := iteratee(item) + + if _, ok := seen[key]; ok { + continue + } + + seen[key] = struct{}{} + result = append(result, item) + } + + return result +} + +// GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee. +func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T { + result := map[U][]T{} + + for _, item := range collection { + key := iteratee(item) + + if _, ok := result[key]; !ok { + result[key] = []T{} + } + + result[key] = append(result[key], item) + } + + return result +} + +// Chunk returns an array of elements split into groups the length of size. If array can't be split evenly, +// the final chunk will be the remaining elements. +func Chunk[T any](collection []T, size int) [][]T { + if size <= 0 { + panic("Second parameter must be greater than 0") + } + + result := make([][]T, 0, len(collection)/2+1) + length := len(collection) + + for i := 0; i < length; i++ { + chunk := i / size + + if i%size == 0 { + result = append(result, make([]T, 0, size)) + } + + result[chunk] = append(result[chunk], collection[i]) + } + + return result +} + +// PartitionBy returns an array of elements split into groups. The order of grouped values is +// determined by the order they occur in collection. The grouping is generated from the results +// of running each element of collection through iteratee. +func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T { + result := [][]T{} + seen := map[K]int{} + + for _, item := range collection { + key := iteratee(item) + + resultIndex, ok := seen[key] + if !ok { + resultIndex = len(result) + seen[key] = resultIndex + result = append(result, []T{}) + } + + result[resultIndex] = append(result[resultIndex], item) + } + + return result + + // unordered: + // groups := GroupBy[T, K](collection, iteratee) + // return Values[K, []T](groups) +} + +// Flattens returns an array a single level deep. +func Flatten[T any](collection [][]T) []T { + result := []T{} + + for _, item := range collection { + result = append(result, item...) + } + + return result +} + +// Shuffle returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm. +func Shuffle[T any](collection []T) []T { + rand.Shuffle(len(collection), func(i, j int) { + collection[i], collection[j] = collection[j], collection[i] + }) + + return collection +} + +// Reverse reverses array so that the first element becomes the last, the second element becomes the second to last, and so on. +func Reverse[T any](collection []T) []T { + length := len(collection) + half := length / 2 + + for i := 0; i < half; i = i + 1 { + j := length - 1 - i + collection[i], collection[j] = collection[j], collection[i] + } + + return collection +} + +// Fill fills elements of array with `initial` value. +func Fill[T Clonable[T]](collection []T, initial T) []T { + result := make([]T, 0, len(collection)) + + for _ = range collection { + result = append(result, initial.Clone()) + } + + return result +} + +// Repeat builds a slice with N copies of initial value. +func Repeat[T Clonable[T]](count int, initial T) []T { + result := make([]T, 0, count) + + for i := 0; i < count; i++ { + result = append(result, initial.Clone()) + } + + return result +} + +// KeyBy transforms a slice or an array of structs to a map based on a pivot callback. +func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V { + result := make(map[K]V, len(collection)) + + for _, v := range collection { + k := iteratee(v) + result[k] = v + } + + return result +} diff --git a/vendor/github.com/samber/lo/tuples.go b/vendor/github.com/samber/lo/tuples.go new file mode 100644 index 000000000..6439952b8 --- /dev/null +++ b/vendor/github.com/samber/lo/tuples.go @@ -0,0 +1,413 @@ +package lo + +func longestCollection(collections ...[]interface{}) int { + max := 0 + + for _, collection := range collections { + if len(collection) > max { + max = len(collection) + } + } + + return max +} + +// 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. +func Zip2[A any, B any](a []A, b []B) []Tuple2[A, B] { + size := Max[int]([]int{len(a), len(b)}) + + result := make([]Tuple2[A, B], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + + result = append(result, Tuple2[A, B]{ + A: _a, + B: _b, + }) + } + + return result +} + +// 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. +func Zip3[A any, B any, C any](a []A, b []B, c []C) []Tuple3[A, B, C] { + size := Max[int]([]int{len(a), len(b), len(c)}) + + result := make([]Tuple3[A, B, C], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + + result = append(result, Tuple3[A, B, C]{ + A: _a, + B: _b, + C: _c, + }) + } + + return result +} + +// 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. +func Zip4[A any, B any, C any, D any](a []A, b []B, c []C, d []D) []Tuple4[A, B, C, D] { + size := Max[int]([]int{len(a), len(b), len(c), len(d)}) + + result := make([]Tuple4[A, B, C, D], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + _d, _ := Nth[D](d, index) + + result = append(result, Tuple4[A, B, C, D]{ + A: _a, + B: _b, + C: _c, + D: _d, + }) + } + + return result +} + +// 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. +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] { + size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e)}) + + result := make([]Tuple5[A, B, C, D, E], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + _d, _ := Nth[D](d, index) + _e, _ := Nth[E](e, index) + + result = append(result, Tuple5[A, B, C, D, E]{ + A: _a, + B: _b, + C: _c, + D: _d, + E: _e, + }) + } + + return result +} + +// 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. +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] { + size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f)}) + + result := make([]Tuple6[A, B, C, D, E, F], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + _d, _ := Nth[D](d, index) + _e, _ := Nth[E](e, index) + _f, _ := Nth[F](f, index) + + result = append(result, Tuple6[A, B, C, D, E, F]{ + A: _a, + B: _b, + C: _c, + D: _d, + E: _e, + F: _f, + }) + } + + return result +} + +// 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. +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] { + size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g)}) + + result := make([]Tuple7[A, B, C, D, E, F, G], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + _d, _ := Nth[D](d, index) + _e, _ := Nth[E](e, index) + _f, _ := Nth[F](f, index) + _g, _ := Nth[G](g, index) + + result = append(result, Tuple7[A, B, C, D, E, F, G]{ + A: _a, + B: _b, + C: _c, + D: _d, + E: _e, + F: _f, + G: _g, + }) + } + + return result +} + +// 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. +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] { + size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h)}) + + result := make([]Tuple8[A, B, C, D, E, F, G, H], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + _d, _ := Nth[D](d, index) + _e, _ := Nth[E](e, index) + _f, _ := Nth[F](f, index) + _g, _ := Nth[G](g, index) + _h, _ := Nth[H](h, index) + + result = append(result, Tuple8[A, B, C, D, E, F, G, H]{ + A: _a, + B: _b, + C: _c, + D: _d, + E: _e, + F: _f, + G: _g, + H: _h, + }) + } + + return result +} + +// 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. +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] { + size := Max[int]([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h), len(i)}) + + result := make([]Tuple9[A, B, C, D, E, F, G, H, I], 0, size) + + for index := 0; index < size; index++ { + _a, _ := Nth[A](a, index) + _b, _ := Nth[B](b, index) + _c, _ := Nth[C](c, index) + _d, _ := Nth[D](d, index) + _e, _ := Nth[E](e, index) + _f, _ := Nth[F](f, index) + _g, _ := Nth[G](g, index) + _h, _ := Nth[H](h, index) + _i, _ := Nth[I](i, index) + + result = append(result, Tuple9[A, B, C, D, E, F, G, H, I]{ + A: _a, + B: _b, + C: _c, + D: _d, + E: _e, + F: _f, + G: _g, + H: _h, + I: _i, + }) + } + + return result +} + +// Unzip2 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +func Unzip2[A any, B any](tuples []Tuple2[A, B]) ([]A, []B) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + } + + return r1, r2 +} + +// Unzip3 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +func Unzip3[A any, B any, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + } + + return r1, r2, r3 +} + +// Unzip4 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +func Unzip4[A any, B any, C any, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + r4 := make([]D, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + r4 = append(r4, tuple.D) + } + + return r1, r2, r3, r4 +} + +// Unzip5 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +func Unzip5[A any, B any, C any, D any, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + r4 := make([]D, 0, size) + r5 := make([]E, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + r4 = append(r4, tuple.D) + r5 = append(r5, tuple.E) + } + + return r1, r2, r3, r4, r5 +} + +// Unzip6 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +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) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + r4 := make([]D, 0, size) + r5 := make([]E, 0, size) + r6 := make([]F, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + r4 = append(r4, tuple.D) + r5 = append(r5, tuple.E) + r6 = append(r6, tuple.F) + } + + return r1, r2, r3, r4, r5, r6 +} + +// Unzip7 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +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) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + r4 := make([]D, 0, size) + r5 := make([]E, 0, size) + r6 := make([]F, 0, size) + r7 := make([]G, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + r4 = append(r4, tuple.D) + r5 = append(r5, tuple.E) + r6 = append(r6, tuple.F) + r7 = append(r7, tuple.G) + } + + return r1, r2, r3, r4, r5, r6, r7 +} + +// Unzip8 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +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) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + r4 := make([]D, 0, size) + r5 := make([]E, 0, size) + r6 := make([]F, 0, size) + r7 := make([]G, 0, size) + r8 := make([]H, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + r4 = append(r4, tuple.D) + r5 = append(r5, tuple.E) + r6 = append(r6, tuple.F) + r7 = append(r7, tuple.G) + r8 = append(r8, tuple.H) + } + + return r1, r2, r3, r4, r5, r6, r7, r8 +} + +// Unzip9 accepts an array of grouped elements and creates an array regrouping the elements +// to their pre-zip configuration. +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) { + size := len(tuples) + r1 := make([]A, 0, size) + r2 := make([]B, 0, size) + r3 := make([]C, 0, size) + r4 := make([]D, 0, size) + r5 := make([]E, 0, size) + r6 := make([]F, 0, size) + r7 := make([]G, 0, size) + r8 := make([]H, 0, size) + r9 := make([]I, 0, size) + + for _, tuple := range tuples { + r1 = append(r1, tuple.A) + r2 = append(r2, tuple.B) + r3 = append(r3, tuple.C) + r4 = append(r4, tuple.D) + r5 = append(r5, tuple.E) + r6 = append(r6, tuple.F) + r7 = append(r7, tuple.G) + r8 = append(r8, tuple.H) + r9 = append(r9, tuple.I) + } + + return r1, r2, r3, r4, r5, r6, r7, r8, r9 +} diff --git a/vendor/github.com/samber/lo/types.go b/vendor/github.com/samber/lo/types.go new file mode 100644 index 000000000..5361a02a1 --- /dev/null +++ b/vendor/github.com/samber/lo/types.go @@ -0,0 +1,83 @@ +package lo + +// Entry defines a key/value pairs. +type Entry[K comparable, V any] struct { + Key K + Value V +} + +// Tuple2 is a group of 2 elements (pair). +type Tuple2[A any, B any] struct { + A A + B B +} + +// Tuple3 is a group of 3 elements. +type Tuple3[A any, B any, C any] struct { + A A + B B + C C +} + +// Tuple4 is a group of 4 elements. +type Tuple4[A any, B any, C any, D any] struct { + A A + B B + C C + D D +} + +// Tuple5 is a group of 5 elements. +type Tuple5[A any, B any, C any, D any, E any] struct { + A A + B B + C C + D D + E E +} + +// Tuple6 is a group of 6 elements. +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 +} + +// Tuple7 is a group of 7 elements. +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 +} + +// Tuple8 is a group of 8 elements. +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 +} + +// Tuple9 is a group of 9 elements. +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 +} diff --git a/vendor/github.com/samber/lo/util.go b/vendor/github.com/samber/lo/util.go new file mode 100644 index 000000000..41ca9c80c --- /dev/null +++ b/vendor/github.com/samber/lo/util.go @@ -0,0 +1,50 @@ +package lo + +import "golang.org/x/exp/constraints" + +// Range creates an array of numbers (positive and/or negative) with given length. +func Range(elementNum int) []int { + length := If(elementNum < 0, -elementNum).Else(elementNum) + result := make([]int, length) + step := If(elementNum < 0, -1).Else(1) + for i, j := 0, 0; i < length; i, j = i+1, j+step { + result[i] = j + } + return result +} + +// RangeFrom creates an array of numbers from start with specified length. +func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []T { + length := If(elementNum < 0, -elementNum).Else(elementNum) + result := make([]T, length) + step := If(elementNum < 0, -1).Else(1) + for i, j := 0, start; i < length; i, j = i+1, j+T(step) { + result[i] = j + } + return result +} + +// 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. +func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T { + result := []T{} + if start == end || step == 0 { + return result + } + if start < end { + if step < 0 { + return result + } + for i := start; i < end; i += step { + result = append(result, i) + } + return result + } + if step > 0 { + return result + } + for i := start; i > end; i += step { + result = append(result, i) + } + return result +} diff --git a/vendor/modules.txt b/vendor/modules.txt index f24c14661..6dc01e5ab 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -120,6 +120,11 @@ github.com/integrii/flaggy # github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 ## explicit github.com/jbenet/go-context/io +# github.com/jesseduffield/generics v0.0.0-20220318214805-3397e5e19e9f +## explicit; go 1.18 +github.com/jesseduffield/generics/hashmap +github.com/jesseduffield/generics/list +github.com/jesseduffield/generics/set # github.com/jesseduffield/go-git/v5 v5.1.2-0.20201006095850-341962be15a4 ## explicit; go 1.13 github.com/jesseduffield/go-git/v5 @@ -221,6 +226,9 @@ github.com/rivo/uniseg # github.com/sahilm/fuzzy v0.1.0 ## explicit github.com/sahilm/fuzzy +# github.com/samber/lo v1.10.1 +## explicit; go 1.18 +github.com/samber/lo # github.com/sanity-io/litter v1.5.2 ## explicit; go 1.14 github.com/sanity-io/litter