langz

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package langz contains miscellaneous functionality supplemental to core golang stuff like slices and channels.

Index

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 Cond

func Cond[T any](cond bool, a, b T) T

Cond returns a if cond is true, else b. It's basically the ternary operator.

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 Make

func Make[T any](count int, val T) []T

Make works like the runtime make func, but it also fills the slice with val.

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

func Take[C any](ch <-chan C) bool

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

func TypedSlice[T, S any](in ...S) (out []T, ok bool)

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.

Jump to

Keyboard shortcuts

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