Parcourir la source

perf: replace for-range funcs

https://github.com/lunemec/go-rule-of-thumb?tab=readme-ov-file#range-over-func
ayn2op il y a 1 an
Parent
commit
6dd60ed0e9
3 fichiers modifiés avec 12 ajouts et 38 suppressions
  1. 12 10
      cmd/guilds_tree.go
  2. 0 15
      pkg/itertools/filter.go
  3. 0 13
      pkg/itertools/map.go

+ 12 - 10
cmd/guilds_tree.go

@@ -3,11 +3,9 @@ package cmd
 import (
 	"fmt"
 	"log/slog"
-	"slices"
 	"sort"
 	"strings"
 
-	"github.com/ayn2op/discordo/pkg/itertools"
 	"github.com/diamondburned/arikawa/v3/discord"
 	"github.com/diamondburned/arikawa/v3/gateway"
 	"github.com/gdamore/tcell/v2"
@@ -91,11 +89,12 @@ func (gt *GuildsTree) channelToString(c discord.Channel) string {
 		s = c.Name
 		// If the name of the channel is empty, use the recipients' tags
 		if s == "" {
-			recipients := itertools.Map(slices.Values(c.DMRecipients), func(r discord.User) string {
-				return r.Tag()
-			})
+			var recipients []string
+			for _, r := range c.DMRecipients {
+				recipients = append(recipients, r.DisplayOrUsername())
+			}
 
-			s = strings.Join(slices.Collect(recipients), ", ")
+			s = strings.Join(recipients, ", ")
 		}
 	case discord.GuildAnnouncement:
 		s = "a-" + c.Name
@@ -131,11 +130,14 @@ func (gt *GuildsTree) createChannelNode(n *tview.TreeNode, c discord.Channel) *t
 }
 
 func (gt *GuildsTree) createChannelNodes(n *tview.TreeNode, cs []discord.Channel) {
-	orphanChannels := itertools.Filter(slices.Values(cs), func(c discord.Channel) bool {
-		return c.Type != discord.GuildCategory && !c.ParentID.IsValid()
-	})
+	var orphanChs []discord.Channel
+	for _, ch := range cs {
+		if ch.Type != discord.GuildCategory && !ch.ParentID.IsValid() {
+			orphanChs = append(orphanChs, ch)
+		}
+	}
 
-	for _, c := range slices.Collect(orphanChannels) {
+	for _, c := range orphanChs {
 		gt.createChannelNode(n, c)
 	}
 

+ 0 - 15
pkg/itertools/filter.go

@@ -1,15 +0,0 @@
-package itertools
-
-import "iter"
-
-func Filter[I any](in iter.Seq[I], predicate func(I) bool) iter.Seq[I] {
-	return func(yield func(I) bool) {
-		for i := range in {
-			if predicate(i) {
-				if !yield(i) {
-					return
-				}
-			}
-		}
-	}
-}

+ 0 - 13
pkg/itertools/map.go

@@ -1,13 +0,0 @@
-package itertools
-
-import "iter"
-
-func Map[I, O any](in iter.Seq[I], f func(I) O) iter.Seq[O] {
-	return func(yield func(O) bool) {
-		for i := range in {
-			if !yield(f(i)) {
-				return
-			}
-		}
-	}
-}