From c3f294bd48c6002bc5e2c919a38080fb91f6fea7 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 16 Apr 2022 14:20:15 -0400 Subject: [PATCH] feat: generic array sampling --- array/array.go | 7 +++---- go.mod | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/array/array.go b/array/array.go index 365c261..aa51994 100644 --- a/array/array.go +++ b/array/array.go @@ -2,8 +2,7 @@ package array import "math/rand" -// Sample returns a random element from an array -// (can't wait for generics!) -func Sample(s []string) string { - return s[rand.Intn(len(s))] +// Sample returns a random element from a generic array +func Sample[T any](arr []T) T { + return arr[rand.Intn(len(arr))] } diff --git a/go.mod b/go.mod index e6d53a3..ba6d78f 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,22 @@ module github.com/maaslalani/confetty -go 1.16 +go 1.18 require ( github.com/charmbracelet/bubbletea v0.20.0 github.com/charmbracelet/harmonica v0.2.0 github.com/charmbracelet/lipgloss v0.5.0 ) + +require ( + github.com/containerd/console v1.0.3 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + golang.org/x/term v0.0.0-20210422114643-f5beecf764ed // indirect +)