From cdb1cf3e03aa5a208a30a3cf180fc272c32b1437 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Tue, 6 Sep 2022 01:58:19 +0200 Subject: [PATCH] more helper functions --- slices.go => contains.go | 18 ----------------- each.go | 11 +++++++++++ flat.go | 9 +++++++++ map.go | 9 +++++++++ of.go | 16 +++++++++++++++ to.go | 42 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 18 deletions(-) rename slices.go => contains.go (61%) create mode 100644 each.go create mode 100644 flat.go create mode 100644 map.go create mode 100644 of.go create mode 100644 to.go diff --git a/slices.go b/contains.go similarity index 61% rename from slices.go rename to contains.go index 0d413e0..8a4d4ab 100644 --- a/slices.go +++ b/contains.go @@ -20,21 +20,3 @@ func Contains[T comparable](slice []T, value T) bool { func ContainsCmp[T comparable](slice []T, value T, cmp EqualityComparator[T]) bool { return IndexOfCmp(slice, value, cmp) != -1 } - -func Map[I, O any](slice []I, mapper func(I) O) []O { - ret := make([]O, 0, len(slice)) - for _, v := range slice { - ret = append(ret, mapper(v)) - } - return ret -} - -func Each[T any](slice []T, f func(T)) { - EachIndex(slice, func(_ int, v T) { f(v) }) -} - -func EachIndex[T any](slice []T, f func(int, T)) { - for i, v := range slice { - f(i, v) - } -} diff --git a/each.go b/each.go new file mode 100644 index 0000000..fced363 --- /dev/null +++ b/each.go @@ -0,0 +1,11 @@ +package slices + +func Each[T any](slice []T, f func(T)) { + EachIndex(slice, func(_ int, v T) { f(v) }) +} + +func EachIndex[T any](slice []T, f func(int, T)) { + for i, v := range slice { + f(i, v) + } +} diff --git a/flat.go b/flat.go new file mode 100644 index 0000000..17a5fbd --- /dev/null +++ b/flat.go @@ -0,0 +1,9 @@ +package slices + +func Flat[T any](s ...[]T) []T { + out := make([]T, 0) + for _, v := range s { + out = append(out, v...) + } + return out +} diff --git a/map.go b/map.go new file mode 100644 index 0000000..a364ec1 --- /dev/null +++ b/map.go @@ -0,0 +1,9 @@ +package slices + +func Map[I, O any](slice []I, mapper func(I) O) []O { + ret := make([]O, 0, len(slice)) + for _, v := range slice { + ret = append(ret, mapper(v)) + } + return ret +} diff --git a/of.go b/of.go new file mode 100644 index 0000000..d8f12cf --- /dev/null +++ b/of.go @@ -0,0 +1,16 @@ +package slices + +func Of[T any](values ...T) []T { + return values +} + +// OfMap returns a slice containing the return values of the unmapper function +// applied to any key-value pair in m +// The order is random +func OfMap[K comparable, V, T any](m map[K]V, unmapper func(K, V) T) []T { + out := make([]T, 0, len(m)) + for k, v := range m { + out = append(out, unmapper(k, v)) + } + return out +} diff --git a/to.go b/to.go new file mode 100644 index 0000000..855e0d2 --- /dev/null +++ b/to.go @@ -0,0 +1,42 @@ +package slices + +import ( + "container/list" +) + +// Deref returns a slice containing all dereferenced values of s. +// The returned slice will be a dereferenced and continuous block of memory. +// Nil pointers are ignored. +func Deref[T any](s []*T) []T { + out := make([]T, 0, len(s)) + Each(s, func(v *T) { + if v != nil { + out = append(out, *v) + } + }) + return out +} + +// ToList returns a list.List containing all values of s +func ToList[T any](s []T) *list.List { + l := list.New() + Each(s, func(value T) { l.PushBack(value) }) + return l +} + +// ToMap returns a map containing all values of s. +// The map key-value pairs are determined by mapper +func ToMap[T any, K comparable, V any](s []T, mapper func(T) (K, V)) map[K]V { + m := map[K]V{} + Each(s, func(value T) { + k, v := mapper(value) + m[k] = v + }) + return m +} + +// ToStructMap returns a struct{} map containing all values of s as keys. +// It is a shorthand for ToMap(s, func(value T) (T, struct{}) { return value, struct{}{} }) +func ToStructMap[T comparable](s []T) map[T]struct{} { + return ToMap(s, func(value T) (T, struct{}) { return value, struct{}{} }) +}