Documentation
¶
Overview ¶
Package langz contains miscellaneous functionality supplemental to core golang stuff like slices and channels.
Index ¶
- func AlignMatrixWidth[T any](a [][]T, defaultVal T)
- func AlignSliceLengths[T any](a, b []T, defaultVal T) ([]T, []T)
- func All[T any](elems ...T) []T
- func Apply[T any](collection []T, fn func(item T) T) []T
- func Cond[T any](cond bool, a, b T) T
- func IsSliceZeroed[T comparable](a []T) bool
- func JoinSlices[T any](a []T, others ...[]T) []T
- func Make[T any](count int, val T) []T
- func MustTypedSlice[T, S any](in ...S) []T
- func NilIfZero[T comparable](t T) *T
- func NonEmptyOf[T comparable](a, b T) T
- func Remove[T any](a []*T, v *T) []*T
- func RemoveUnordered[T any](a []*T, v *T) []*T
- func Take[C any](ch <-chan C) bool
- func TypedSlice[T, S any](in ...S) (out []T, ok bool)
- func ZeroIfNil[T comparable](t *T) T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlignMatrixWidth ¶
func AlignMatrixWidth[T any](a [][]T, defaultVal T)
AlignMatrixWidth ensures that rows of matrix have the same length, that length being the length of the longest row of a.
func AlignSliceLengths ¶
func AlignSliceLengths[T any](a, b []T, defaultVal T) ([]T, []T)
AlignSliceLengths returns slices for a and b such that the returned slices have the same lengths and contents as a and b, with any additional elements filled with defaultVal. If a and b are already the same length, they are returned as-is. At most one new slice is allocated.
func All ¶
func All[T any](elems ...T) []T
All returns a new slice containing elems. If elems is nil, an empty slice is returned.
func Apply ¶
func Apply[T any](collection []T, fn func(item T) T) []T
Apply returns a new slice whose elements are the result of applying fn to each element of collection.
func IsSliceZeroed ¶
func IsSliceZeroed[T comparable](a []T) bool
IsSliceZeroed returns true if a is empty or if each element of a is the zero value.
func JoinSlices ¶
func JoinSlices[T any](a []T, others ...[]T) []T
JoinSlices joins the slices into a single slice. It always returns a new, non-nil slice, even if all the input slices are nil. The input slices are not modified. Order is preserved.
func MustTypedSlice ¶
func MustTypedSlice[T, S any](in ...S) []T
MustTypedSlice calls TypedSlice, but panics if the conversion fails.
func NilIfZero ¶
func NilIfZero[T comparable](t T) *T
NilIfZero returns nil if t is T's zero value; otherwise it returns a pointer to t.
func NonEmptyOf ¶
func NonEmptyOf[T comparable](a, b T) T
NonEmptyOf returns a if a is non-zero, else b.
func Remove ¶
func Remove[T any](a []*T, v *T) []*T
Remove returns a slice without v, preserving order. If order is not important, use RemoveUnordered instead, as it's faster.
func RemoveUnordered ¶
func RemoveUnordered[T any](a []*T, v *T) []*T
RemoveUnordered returns a slice without v, but order is not guaranteed to preserved. If order is important, use Remove instead.
func Take ¶
Take returns true if ch is non-nil and a value is available from ch, or false otherwise. This is useful for a succinct "if done" idiom, e.g.:
if someCondition && loz.Take(doneCh) { return }
Note that this function does read from the channel, so it's mostly intended for use with "done" channels, where the caller is not interested in the value sent on the channel, only the fact that a value was sent, e.g. by closing the channel.
func TypedSlice ¶
TypedSlice returns a new slice of type T, having performed type conversion on each element of in.
func ZeroIfNil ¶
func ZeroIfNil[T comparable](t *T) T
ZeroIfNil returns the zero value of T if t is nil, otherwise it returns the dereferenced value of t.
Types ¶
This section is empty.