Skip to content
Snippets Groups Projects
slice_utils.go 1.28 KiB
Newer Older
Cornel Rautenbach's avatar
Cornel Rautenbach committed
package slice_utils

import (
	"github.com/thoas/go-funk"
Cornel Rautenbach's avatar
Cornel Rautenbach committed
)

func MinimumFloat64(values []float64) (min float64) {
	for index, value := range values {
		if index == 0 || value < min {
			min = value
		}
	}
	return
}

// Intersection Finds the intersection between two integer arrays
func Intersection(a, b []int64) (c []int64) {
	m := make(map[int64]bool)

	for _, item := range a {
		m[item] = true
	}

	for _, item := range b {
		if _, ok := m[item]; ok {
			c = append(c, item)
		}
	}
	return
}

func RemoveStringAtIndex(s []string, index int) []string {
	return append(s[:index], s[index+1:]...)
}

func ElementExists(in interface{}, elem interface{}) (bool, int) {
	idx := funk.IndexOf(in, elem)
	return idx != -1, idx
}

func FilterNonZero(arr []int64) []int64 {
	// Filter out the zero numbers
	nonZeroNumbers := funk.Filter(arr, func(number int64) bool {
		return number != 0
	}).([]int64)
	return nonZeroNumbers
}

func FilterNonEmptyString(arr []string) []string {
	// Filter out empty strings
	nonEmptyStrings := funk.Filter(arr, func(value string) bool {
		return value != ""
	}).([]string)
	return nonEmptyStrings
}

func ArraySlice(s []any, offset, length int) []any {
	if offset > len(s) {
		return s
	}
	end := offset + length
	if end < len(s) {
		return s[offset:end]
	}
	return s[offset:]
}