slicesutil

package
v0.0.0-...-9e322e5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2024 License: MIT Imports: 3 Imported by: 2

README

go-utils

slices utils

A few examples on the "slices" package.

string

FilterByNonEmpty
slicesutil.FilterByNonEmpty([]string{"1", "", "3", ""})
// Result: [1 3]
FindNextEl
slicesutil.FindNextEl([]string{"1", "2", "3", "4"}, "2")
// Result: 3

T

users := []User{
	{Id: 1, Name: "Name 1", Age: 1},
	{Id, 2, Name: "Name 2", Age: 2},
	{Id: 3, Name: "Name 3", Age: 3}}
FilterT
var filteredUsers []User
for _, user := range users {
	if user.Age > 1 {
		filteredUsers = append(filteredUsers, user)
	}
}

// new way
filteredUsers = slicesutil.FilterT[User](users, func(u User) bool {
	return u.Age > 1
}) // Result: [{2 2 Name 2} {3 3 Name 3}]
FindT
var foundUser *User
for _, user := range users {
	if user.Age == 2 {
		foundUser = &user
		break
	}
}

// new way
foundUser = slicesutil.FindT[User](users, func(u User) bool {
	return u.Age == 2
}) // Result: &{2 2 Name 2}
TransformT
var names []string
for _, user := range users {
	names = append(names, user.Name)
}

// new way
names = slicesutil.TransformT[User, string](users, func(u User) (*string, error) {
	return &u.Name, nil
}) // Result: [Name 1 Name 2 Name 3]
SortT
sort.SliceStable(users, func(i, j int) bool {
	return users[i].Age > users[j].Age
})

// new way (clone and sort values)
users = slicesutil.SortT[User](getUsers(), func(u1, u2 User) bool {
	return u1.Age > u2.Age
}) // Result: [{3 3 Name 3} {2 2 Name 2} {1 1 Name 1}]
ToStringT
var names []string
for _, user := range users {
	names = append(names, user.Name)
}
str := strings.Join(names, ", ")

// new way
str = slicesutil.ToStringT[User](users, func(u User) *string {
	return &u.Name
}, ", ") // Result: Name 1, Name 2, Name 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddOrReplaceT

func AddOrReplaceT[T any](in []T, t T, is func(T) bool) []T

AddOrReplaceT adds or replaces {t} in the slice {in} using the provided {is} function.

func Append

func Append(from, to []string) []string

Append appends the slice {to} into the slice {from} without duplicated values (case-insensitive).

func AppendT

func AppendT[T any](from, to []T, exist func(T, T) bool) []T

Append appends the slice {to} into the slice {from} without duplicated values using the provided {exist} function.

func ContainAll

func ContainAll(s1, s2 []string) bool

ContainAll returns {true} if every value of the slice {s1} is in the slice {s2} (case-insensitive).

func Distinct

func Distinct(in []string) []string

Distinct removes duplicated values in the slice {in}.

func Equal

func Equal(s1, s2 []string) bool

Equal returns {true} if the two slices contain exactly the same values (case-insensitive).

func Exist

func Exist(s []string, v string) bool

Exist returns {true} if {v} value exists in the slice {s} (case-insensitive).

func ExistT

func ExistT[T any](s []T, equal func(T) bool) bool

ExistT returns bool using the provided equal function.

func FilterByNonEmpty

func FilterByNonEmpty(in []string) []string

FilterByNonEmpty removes all empty values in the slice {in}.

func FilterT

func FilterT[T any](in []T, is func(T) bool) []T

FilterT filters the slice in using the provided is function.

func FindLastOccurrenceIn

func FindLastOccurrenceIn(from, to []string) string

FindLastOccurrenceIn finds last occurrence in the slice {from} of the slice {to},

returns empty if no occurrence found.

func FindNextEl

func FindNextEl(s []string, v string) string

FindNextEl finds the next element after the value {v} in the slice {s}.

func FindT

func FindT[T any](in []T, is func(T) bool) *T

FindT finds the first occurrence in the slice in using the provided {is} function.

func FlatTransformT

func FlatTransformT[F any, T any](from []F, transform func(F) ([]T, error)) []T

FlatTransformT transforms and flattens the slice {from} []F to the slice []T using the provided {transform} function.

func Must

func Must(s1, s2 []string) bool

Must returns {true} if the two slices contain exactly in the same order the same values (case-insensitive).

func NewSliceS

func NewSliceS(in []string) sliceS

NewSliceS builds {sliceS} type from {in}.

func Sort

func Sort(s []string) []string

Sort sorts the slice {s} values.

func SortT

func SortT[T any](s []T, less func(T, T) bool) []T

SortT clones and sorts the slice {s} using the provided {less} function.

func ToMap

func ToMap(in []string) map[string]string

ToMap transforms the slice {in} to a map key/value.

func ToStringT

func ToStringT[T any](in []T, transform func(T) *string, separator string) string

ToString concatenates the slice {in} to a single string using the provided {transform} function.

func TransformT

func TransformT[F any, T any](from []F, transform func(F) (*T, error)) []T

TransformT transforms the slice {from} []F to the slice []T using the provided {transform} function.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL