tricks

package module
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: GPL-3.0 Imports: 1 Imported by: 6

README

Tricks Coverage Badge

Frequent magics every developer may need

Usage

go get github.com/janstoon/toolbox/tricks

Documentation

Index

Constants

View Source
const IndexUnavailable = -1

Variables

View Source
var ErrDuplicateEntry = errors.New("duplicate entry")

Functions

func ApplyOptions

func ApplyOptions[S any](s *S, oo ...Option[S]) *S

func Coalesce

func Coalesce[T comparable](tt ...T) T

Coalesce returns left-most non-zero value It's like cmp.Or

func Copy added in v0.11.0

func Copy[T any](tt []T) []T

func Filter

func Filter[T any](tt []T, match func(src T) bool) []T

func Find

func Find[T any](tt []T, match func(src T) bool) *T

func FindIndex

func FindIndex[T any](tt []T, match func(src T) bool) int

func Flat added in v0.6.3

func Flat[T any](slices ...[]T) []T

func FromAny added in v0.8.1

func FromAny[T any](src any) T

func IdentityMiddleware added in v0.9.0

func IdentityMiddleware[H any](h H) H

func IndexOf

func IndexOf[T comparable](expected T, tt []T) int

func InsertIfNotExist

func InsertIfNotExist[K comparable, V any](hm map[K]V, key K, value V)

func IteratorCast added in v0.11.0

func IteratorCast[T comparable](i Iterator[any]) iteratorExtension[T]

IteratorCast casts the elements of the iterator to the specified type. It is useful in cases where the type is lost in the transformation.

func Map

func Map[S, D any](ss []S, transformer Transformer[S, D]) []D

func MatchEqual

func MatchEqual[T comparable](expected T) func(actual T) bool

func PtrPtr

func PtrPtr[S, D any](src *S, transformer Transformer[S, D]) *D

PtrPtr transforms pointer of type S to pointer of type D. If src is nil it returns nil

func PtrVal

func PtrVal[T any](src *T) T

PtrVal dereferences a pointer of type T. If it's nil it returns the zero value of T

func Reduce

func Reduce[T, R any](tt []T, reducer func(r R, t T) R) R

func StringToRunes

func StringToRunes(src string) []rune

func ToAny added in v0.8.1

func ToAny[T any](src T) any

func ValPtr

func ValPtr[T any](src T) *T

ValPtr converts a value of type T to its pointer

Types

type ChainIterator added in v0.11.0

type ChainIterator[T comparable] struct {
	// contains filtered or unexported fields
}

func NewChainIterator added in v0.11.0

func NewChainIterator[T comparable](is ...Iterator[T]) *ChainIterator[T]

func (*ChainIterator[T]) Next added in v0.11.0

func (ci *ChainIterator[T]) Next() (T, bool)

type InPlaceOption

type InPlaceOption[S any] func(s *S)

func (InPlaceOption[S]) Apply

func (o InPlaceOption[S]) Apply(s *S) *S

type Iterator added in v0.11.0

type Iterator[T comparable] interface {
	Next() (T, bool)
}

type Middleware added in v0.9.0

type Middleware[H any] func(next H) H

type MiddlewareStack added in v0.9.0

type MiddlewareStack[H any] Middleware[H]

func (MiddlewareStack[H]) Push added in v0.9.0

func (stk MiddlewareStack[H]) Push(mw Middleware[H]) MiddlewareStack[H]

type Option

type Option[S any] interface {
	Apply(s *S) *S
}

type OutOfPlaceOption

type OutOfPlaceOption[S any] func(s S) S

func (OutOfPlaceOption[S]) Apply

func (o OutOfPlaceOption[S]) Apply(s *S) *S

type SliceIterator added in v0.11.0

type SliceIterator[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSliceIterator added in v0.11.0

func NewSliceIterator[T comparable](s []T) *SliceIterator[T]

func (*SliceIterator[T]) Iter added in v0.11.0

func (si *SliceIterator[T]) Iter() iteratorExtension[T]

Iter returns an iterator extension. The extension provides additional functionality for the iterator. The extension is not thread-safe. The extension is not reusable. If you need to iterate again, call Clone.

func (*SliceIterator[T]) Next added in v0.11.0

func (si *SliceIterator[T]) Next() (T, bool)

type Transformer

type Transformer[S, D any] func(src S) D

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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