Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
With some functions
  • Loading branch information
OZoneGuy committed Oct 14, 2023
0 parents commit 90e2a9a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/OZoneGuy/go-misc-lib

go 1.21.3
96 changes: 96 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 90e2a9a

Please sign in to comment.