hlp

package module
v0.0.0-...-1c63abb Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 3 Imported by: 0

README

hlp

Generic h(e)lp(ers)

This is a sort of half-fork of the absolutely awesome samber/lo. My only issue was the lack of "error" variants in the slices helpers, even though lots of issues/PRs seem to have been raised to introduce them.

For a complete list of included functions, see the godoc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign[K comparable, V any](maps ...map[K]V) map[K]V

Assign merges multiple maps from left to right.

func CopyFS

func CopyFS(dest wfs.WriteFileFS, src fs.FS, root string) error

func Filter

func Filter[T any](collection []T, callback func(item T, index int) bool) []T

Filter returns all elements of the input slice the supplied callback returns true for

func FilterErr

func FilterErr[T any](collection []T, callback func(item T, index int) (bool, error)) ([]T, error)

FilterErr is like Filter, but the callback function can fail. In such a case, a nil slice and the error from the callback is returned. This function fails fast; i.e it stops iteration at the first non-nil error

func FilterMap

func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R

FilterMap is the combination of Filter & Map, returning the elements from the input slice as transformed by the callback function, but only in cases where the callback function also returns true

func FilterMapErr

func FilterMapErr[T any, R any](collection []T, callback func(item T, index int) (R, bool, error)) ([]R, error)

FilterMapErr is like FilterMap, but the callback function can fail. In such a case, a nil slice and the error from the callback is returned. This function fails fast; i.e it stops iteration at the first non-nil error

func Flatten

func Flatten[T any](lists ...[]T) []T

Flatten consolidates multiple lists into a single conjoined list

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys creates an slice of the map keys.

func Map

func Map[T any, R any](collection []T, callback func(item T, index int) R) []R

Map returns all elements of the input slice as transformed by the supplied callback function

func MapErr

func MapErr[T any, R any](collection []T, callback func(item T, index int) (R, error)) ([]R, error)

MapErr is like Map, but the callback function can fail. In such a case, a nil slice and error from the callback is returned. This function fails fast; i.e it stops iteration at the first non-nil error

func MapFromSlice

func MapFromSlice[T any, K comparable, V any](collection []T, callback func(item T, index int) (K, V)) map[K]V

MapFromSlice returns a map, whose keys & values are the return values from applying the callback function to each element of the given slice

func MapFromSliceErr

func MapFromSliceErr[T any, K comparable, V any](collection []T, callback func(item T, index int) (K, V, error)) (map[K]V, error)

MapFromSliceErr is like MapFromSlice, but the callback function can fail. In such a case, a nil slice and the error from the callback is returned. This function fails fast; i.e it stops iteration at the first non-nil error

func Must

func Must[T any](out T, err error) T

Must wraps a function call that returns value and error, and panics if the error is non-nil

func Ptr

func Ptr[T any](x T) *T

Ptr returns a pointer copy of the given value

func ToAnySlice

func ToAnySlice[T any](collection []T) []any

ToAnySlice returns a new slice where all elements of `collection` are mapped to the `any` type

func Values

func Values[K comparable, V any](m map[K]V) []V

Values creates an slice of the map values.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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