Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
e61682b24c
|
|||
5dc9e98f6b
|
|||
4dd1c08e77
|
|||
c9e459edac
|
@@ -3,6 +3,6 @@ BFB goext library
|
||||
|
||||
A collection of general & useful library methods
|
||||
|
||||
Every subfolder is a seperate dependency and can be imported individually
|
||||
This should not have any heavy dependencies (gin, mongo, etc) and add missing basic language features...
|
||||
|
||||
Potentially needs `export GOPRIVATE="gogs.mikescher.com"`
|
@@ -4,6 +4,24 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func BoolCount(arr ...bool) int {
|
||||
c := 0
|
||||
for _, v := range arr {
|
||||
if v {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func Range[T IntegerConstraint](start T, end T) []T {
|
||||
r := make([]T, 0, end-start)
|
||||
for i := start; i < end; i++ {
|
||||
r = append(r, i)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func ForceArray[T any](v []T) []T {
|
||||
if v == nil {
|
||||
return make([]T, 0)
|
||||
@@ -124,6 +142,27 @@ func ArrAnyErr(arr interface{}, fn func(int) (bool, error)) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func ArrFirst[T comparable](arr []T, comp func(v T) bool) (T, bool) {
|
||||
for _, v := range arr {
|
||||
if comp(v) {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return *new(T), false
|
||||
}
|
||||
|
||||
func ArrLast[T comparable](arr []T, comp func(v T) bool) (T, bool) {
|
||||
found := false
|
||||
result := *new(T)
|
||||
for _, v := range arr {
|
||||
if comp(v) {
|
||||
found = true
|
||||
result = v
|
||||
}
|
||||
}
|
||||
return result, found
|
||||
}
|
||||
|
||||
func AddToSet[T comparable](set []T, add T) []T {
|
||||
for _, v := range set {
|
||||
if v == add {
|
||||
|
@@ -3,3 +3,31 @@ package langext
|
||||
type IntConstraint interface {
|
||||
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64
|
||||
}
|
||||
|
||||
type SignedConstraint interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64
|
||||
}
|
||||
|
||||
type UnsignedConstraint interface {
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
||||
}
|
||||
|
||||
type IntegerConstraint interface {
|
||||
SignedConstraint | UnsignedConstraint
|
||||
}
|
||||
|
||||
type FloatConstraint interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
type ComplexConstraint interface {
|
||||
~complex64 | ~complex128
|
||||
}
|
||||
|
||||
type OrderedConstraint interface {
|
||||
IntegerConstraint | FloatConstraint | ~string
|
||||
}
|
||||
|
||||
type NumberConstraint interface {
|
||||
IntegerConstraint | FloatConstraint
|
||||
}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package mathext
|
||||
|
||||
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
|
||||
func AvgFloat64(arr []float64) float64 {
|
||||
return SumFloat64(arr) / float64(len(arr))
|
||||
}
|
||||
@@ -11,3 +13,19 @@ func SumFloat64(arr []float64) float64 {
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func Max[T langext.OrderedConstraint](v1 T, v2 T) T {
|
||||
if v1 > v2 {
|
||||
return v1
|
||||
} else {
|
||||
return v2
|
||||
}
|
||||
}
|
||||
|
||||
func Min[T langext.OrderedConstraint](v1 T, v2 T) T {
|
||||
if v1 < v2 {
|
||||
return v1
|
||||
} else {
|
||||
return v2
|
||||
}
|
||||
}
|
||||
|
@@ -1,26 +1,28 @@
|
||||
package mathext
|
||||
|
||||
func Sum(v []float64) float64 {
|
||||
total := float64(0)
|
||||
import "gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
|
||||
func Sum[T langext.NumberConstraint](v []T) T {
|
||||
total := T(0)
|
||||
for _, v := range v {
|
||||
total += v
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func Mean(v []float64) float64 {
|
||||
return Sum(v) / float64(len(v))
|
||||
func Mean[T langext.FloatConstraint](v []T) T {
|
||||
return Sum(v) / T(len(v))
|
||||
}
|
||||
|
||||
func Median(v []float64) float64 {
|
||||
func Median[T langext.FloatConstraint](v []T) T {
|
||||
if len(v)%2 == 1 {
|
||||
return v[len(v)/2]
|
||||
} else {
|
||||
return (v[len(v)/2-1] + v[len(v)/2]) / float64(2)
|
||||
return (v[len(v)/2-1] + v[len(v)/2]) / T(2)
|
||||
}
|
||||
}
|
||||
|
||||
func Min(v []float64) float64 {
|
||||
func ArrMin[T langext.OrderedConstraint](v []T) T {
|
||||
r := v[0]
|
||||
for _, val := range v {
|
||||
if val < r {
|
||||
@@ -30,7 +32,7 @@ func Min(v []float64) float64 {
|
||||
return r
|
||||
}
|
||||
|
||||
func Max(v []float64) float64 {
|
||||
func ArrMax[T langext.OrderedConstraint](v []T) T {
|
||||
r := v[0]
|
||||
for _, val := range v {
|
||||
if val > r {
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package termext
|
||||
|
||||
import "strings"
|
||||
|
||||
const (
|
||||
colorReset = "\033[0m"
|
||||
colorRed = "\033[31m"
|
||||
@@ -43,3 +45,17 @@ func Gray(v string) string {
|
||||
func White(v string) string {
|
||||
return colorWhite + v + colorReset
|
||||
}
|
||||
|
||||
func CleanString(v string) string {
|
||||
v = strings.ReplaceAll(v, colorReset, "")
|
||||
v = strings.ReplaceAll(v, colorRed, "")
|
||||
v = strings.ReplaceAll(v, colorGreen, "")
|
||||
v = strings.ReplaceAll(v, colorYellow, "")
|
||||
v = strings.ReplaceAll(v, colorBlue, "")
|
||||
v = strings.ReplaceAll(v, colorPurple, "")
|
||||
v = strings.ReplaceAll(v, colorCyan, "")
|
||||
v = strings.ReplaceAll(v, colorGray, "")
|
||||
v = strings.ReplaceAll(v, colorWhite, "")
|
||||
|
||||
return v
|
||||
}
|
||||
|
Reference in New Issue
Block a user