Skip to content

Commit 5d99735

Browse files
committed
core/slices: impl ChooseOne
1 parent 262b870 commit 5d99735

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

intra/core/slices.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ func ShuffleInPlace[T any](c []T) []T {
5454
return c
5555
}
5656

57+
func ChooseOne[T any](c []T) (zz T) {
58+
if len(c) <= 0 {
59+
return zz
60+
}
61+
return c[rand.Intn(len(c))]
62+
}
63+
5764
// sorts arr x in ascending order. less(a, b) < 0 when a < b, a > 0 when a > b
5865
// and 0 when a == b.
5966
func Sort[T any](arr []T, less func(a, b T) int) []T {
@@ -107,12 +114,7 @@ func WithoutNils[T any](arr []T) (out []T) {
107114
}
108115

109116
func IsAny[T any](arr []T, test TestFn[T]) bool {
110-
for _, x := range arr {
111-
if test(x) {
112-
return true
113-
}
114-
}
115-
return false
117+
return slices.ContainsFunc(arr, test)
116118
}
117119

118120
func IsAll[T any](arr []T, test TestFn[T]) bool {

0 commit comments

Comments
 (0)