From 90e2a9ad3a9f9be240921974c36bc97e12aacf80 Mon Sep 17 00:00:00 2001 From: OZoneGuy Date: Sat, 14 Oct 2023 18:03:44 -0400 Subject: [PATCH] Initial commit With some functions --- README.md | 6 ++++ go.mod | 3 ++ lib.go | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 lib.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..0140c52 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Common use go functions + +A collection of functions that are not in the standard library that I want to have. + +WIP + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..38ae5be --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/OZoneGuy/go-misc-lib + +go 1.21.3 diff --git a/lib.go b/lib.go new file mode 100644 index 0000000..0164c62 --- /dev/null +++ b/lib.go @@ -0,0 +1,96 @@ +package gomisclib + +// Map applies f to each element of a, returning a new slice containing the results. +func Map[T1, T2 any](f func(T1) T2, a []T1) []T2 { + b := make([]T2, len(a)) + for i, v := range a { + b[i] = f(v) + } + return b +} + +// MutMap applies f to each element of a mutating a +func MutMap[T any](f func(*T), a []T) { + for i := range a { + f(&a[i]) + } +} + +// Filter returns a slice containing all elements of a for which f returns true. +func Filter[T any](f func(T) bool, a []T) []T { + b := make([]T, 0, len(a)) + for _, v := range a { + if f(v) { + b = append(b, v) + } + } + return b +} + +// Contains returns true if v is in a. +func Contains[T comparable](a []T, v T) bool { + for _, x := range a { + if x == v { + return true + } + } + return false +} + +// All returns true if f returns true for all elements of a. +func All[T any](f func(T) bool, a []T) bool { + for _, v := range a { + if !f(v) { + return false + } + } + return true +} + +// Any returns true if f returns true for any element of a. +func Any[T any](f func(T) bool, a []T) bool { + for _, v := range a { + if f(v) { + return true + } + } + return false +} + +// Count returns the number of elements of a for which f returns true. +func Count[T any](f func(T) bool, a []T) int { + n := 0 + for _, v := range a { + if f(v) { + n++ + } + } + return n +} + +// Find returns the first element of a for which f returns true, or nil if there is no such element. +func Find[T any](f func(T) bool, a []T) *T { + for _, v := range a { + if f(v) { + return &v + } + } + return nil +} + +// FindIndex returns the index of the first element of a for which f returns true, or -1 if there is no such element. +func FindIndex[T any](f func(T) bool, a []T) int { + for i, v := range a { + if f(v) { + return i + } + } + return -1 +} + +// ForEeach calls f on each element of a. +func ForEach[T any](f func(T), a []T) { + for _, v := range a { + f(v) + } +}