Documentation ¶
Index ¶
- func Abs[T number](v T) T
- func Bool[T builtin](v T) bool
- func ChanAdapt[T, D any](in <-chan T, fn func(T) D) <-chan D
- func ChanPut[T any](ch chan T, elems []T)
- func ChanReadN[T any](ch chan T, n int) []T
- func MapApply[K comparable, V any](in map[K]V, fn func(k K, v V))
- func MapContainsKey[K comparable, V any](m map[K]V, key K) bool
- func MapContainsKeysAll[K comparable, V any](m map[K]V, keys []K) bool
- func MapContainsKeysAny[K comparable, V any](m map[K]V, keys []K) bool
- func MapCopy[K comparable, V any](m map[K]V) map[K]V
- func MapDefaults[K comparable, V any](m map[K]V, defaults map[K]V) map[K]V
- func MapFilter[K comparable, V any](in map[K]V, fn func(k K, v V) bool) map[K]V
- func MapFilterKeys[K comparable, V any](in map[K]V, fn func(k K) bool) map[K]V
- func MapFilterValues[K comparable, V any](in map[K]V, fn func(v V) bool) map[K]V
- func MapGetKeys[K comparable, V any](m map[K]V) []K
- func MapGetValues[K comparable, V any](m map[K]V) []V
- func MapMap[K, K1 comparable, V, V1 any](in map[K]V, fn func(K, V) (K1, V1)) map[K1]V1
- func MapMapErr[K, K1 comparable, V, V1 any](in map[K]V, fn func(K, V) (K1, V1, error)) (map[K1]V1, error)
- func MapMerge[K comparable, V any](m1, m2 map[K]V, fn func(k K, v1, v2 V) V) map[K]V
- func Max[T number](in ...T) T
- func MaxDefault[T number](in ...T) T
- func MaxOr[T number](defaultVal T, in ...T) T
- func Min[T number](in ...T) T
- func MinDefault[T number](in ...T) T
- func MinOr[T number](defaultVal T, in ...T) T
- func Must[T any](val T, err error) T
- func Pointer[T any](v T) *T
- func Slice2Chan[T any](in []T, capacity int) chan T
- func Slice2ChanFill[T any](in []T) chan T
- func Slice2Map[T comparable](in []T) map[T]struct{}
- func Slice2MapFn[T any, K comparable, V any](in []T, fn func(idx int, elem T) (K, V)) map[K]V
- func Slice2MapFnErr[T any, K comparable, V any](in []T, fn func(idx int, elem T) (K, V, error)) (map[K]V, error)
- func SliceAddNotExists[T comparable](in []T, elem T) []T
- func SliceAll[T any](in []T, fn func(T) bool) bool
- func SliceAny[T any](in []T, fn func(T) bool) bool
- func SliceApply[T any](in []T, fn func(int, T))
- func SliceChain[T any](in ...[]T) []T
- func SliceChunk[T any](in []T, fn func(i int, elem T) bool) [][]T
- func SliceChunkEvery[T any](in []T, every int) [][]T
- func SliceContainsElem[T comparable](in []T, elem T) bool
- func SliceDifference[T comparable](oldSlice, newSlice []T) []T
- func SliceEqualUnordered[T comparable](in1, in2 []T) bool
- func SliceFillElem[T any](l int, elem T) []T
- func SliceFilter[T any](in []T, fn func(T) bool) []T
- func SliceFindAllElements[T any](in []T, fn func(i int, elem T) bool) []T
- func SliceFindAllIndexes[T any](in []T, fn func(i int, elem T) bool) []int
- func SliceGroupBy[K comparable, V any](in []V, fn func(V) K) map[K][]V
- func SliceIntersection[T comparable](oldSlice, newSlice []T) []T
- func SliceMap[T any, V any](in []T, fn func(T) V) []V
- func SliceMapErr[T any, V any](in []T, fn func(T) (V, error)) ([]V, error)
- func SliceRange[T number](start, stop, step T) []T
- func SliceReverse[T any](in []T) []T
- func SliceSort[T any](in []T, less func(a, b T) bool)
- func SliceSortCopy[T any](in []T, less func(a, b T) bool) []T
- func SliceUnion[T comparable](in ...[]T) []T
- func SliceUniq[T comparable](in []T) []T
- func SliceWithout[T any](in []T, fn func(T) bool) []T
- func SliceWithoutElem[T comparable](in []T, elem T) []T
- func SliceZip[T any](in ...[]T) [][]T
- func Sum[T number](in ...T) T
- type KV
- type NullVal
- type SliceElem
- func SliceFindAll[T any](in []T, fn func(i int, elem T) bool) []SliceElem[T]
- func SliceFindFirst[T any](in []T, fn func(i int, elem T) bool) SliceElem[T]
- func SliceFindFirstElem[T comparable](in []T, elem T) SliceElem[T]
- func SliceFindLast[T any](in []T, fn func(i int, elem T) bool) SliceElem[T]
- func SliceFindLastElem[T comparable](in []T, elem T) SliceElem[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶ added in v0.4.0
func Bool[T builtin](v T) bool
Bool returns true if element not equal to default value for this type.
func ChanAdapt ¶ added in v0.37.0
func ChanAdapt[T, D any](in <-chan T, fn func(T) D) <-chan D
ChanAdapt returns a channel, which will contains adapted messages from source channel. Result channel will closed after source channel is closed.
func ChanPut ¶ added in v0.37.0
func ChanPut[T any](ch chan T, elems []T)
ChanPut will put all elements into the channel synchronously.
func ChanReadN ¶ added in v0.38.0
ChanReadN will read N messages from channel and return the result slice.
func MapApply ¶ added in v0.35.0
func MapApply[K comparable, V any](in map[K]V, fn func(k K, v V))
MapApply apply fn to each kv pair
func MapContainsKey ¶ added in v0.27.0
func MapContainsKey[K comparable, V any](m map[K]V, key K) bool
MapContainsKey returns true if key is exists in the map.
func MapContainsKeysAll ¶ added in v0.27.0
func MapContainsKeysAll[K comparable, V any](m map[K]V, keys []K) bool
MapContainsKeysAll returns true when at all keys exists in the map.
func MapContainsKeysAny ¶ added in v0.27.0
func MapContainsKeysAny[K comparable, V any](m map[K]V, keys []K) bool
MapContainsKeysAny returns true when at least one key exists in the map.
func MapCopy ¶ added in v0.18.0
func MapCopy[K comparable, V any](m map[K]V) map[K]V
MapCopy returns a shallow copy of map.
func MapDefaults ¶ added in v0.18.0
func MapDefaults[K comparable, V any](m map[K]V, defaults map[K]V) map[K]V
MapDefaults returns the map `m` after filling in its non-exists keys by `defaults`. Example: {1:1}, {1:0, 2:2} => {1:1, 2:2}
func MapFilter ¶ added in v0.3.0
func MapFilter[K comparable, V any](in map[K]V, fn func(k K, v V) bool) map[K]V
MapFilter returns the map which contains elements that `fn(key, value) == true`.
func MapFilterKeys ¶ added in v0.3.0
func MapFilterKeys[K comparable, V any](in map[K]V, fn func(k K) bool) map[K]V
MapFilterKeys returns the map which contains elements that `fn(key) == true`. That is a simplified version of MapFilter.
func MapFilterValues ¶ added in v0.3.0
func MapFilterValues[K comparable, V any](in map[K]V, fn func(v V) bool) map[K]V
MapFilterValues returns the map which contains elements that `fn(value) == true`. That is a simplified version of MapFilter.
func MapGetKeys ¶ added in v0.17.0
func MapGetKeys[K comparable, V any](m map[K]V) []K
MapGetKeys returns all keys of map.
func MapGetValues ¶ added in v0.17.0
func MapGetValues[K comparable, V any](m map[K]V) []V
MapGetValues returns all values of map. Not Uniq, unordered.
func MapMap ¶ added in v0.26.0
func MapMap[K, K1 comparable, V, V1 any](in map[K]V, fn func(K, V) (K1, V1)) map[K1]V1
MapMap apply fn to all kv pairs from in.
func MapMapErr ¶ added in v0.40.0
func MapMapErr[K, K1 comparable, V, V1 any](in map[K]V, fn func(K, V) (K1, V1, error)) (map[K1]V1, error)
MapMapErr apply fn to all kv pairs from in.
func MapMerge ¶ added in v0.3.0
func MapMerge[K comparable, V any](m1, m2 map[K]V, fn func(k K, v1, v2 V) V) map[K]V
MapMerge returns the map which contains all keys from m1, m2 and values from `fn(key, m1Value, m2Value)`.
func MaxDefault ¶ added in v0.25.0
func MaxDefault[T number](in ...T) T
MaxDefault returns max element from `in` or default value for specified type when `in` is empty.
func MaxOr ¶ added in v0.25.0
func MaxOr[T number](defaultVal T, in ...T) T
MaxOr returns max element from `in` or defaultVal when `in` is empty
func MinDefault ¶ added in v0.25.0
func MinDefault[T number](in ...T) T
MinDefault returns min element from `in` or default value for specified type when `in` is empty.
func MinOr ¶ added in v0.25.0
func MinOr[T number](defaultVal T, in ...T) T
MinOr returns min element from `in` or defaultVal when `in` is empty
func Must ¶ added in v0.39.0
Must will panic on error after calling typical function.
Example ¶
package main import ( "bytes" "fmt" "github.com/kazhuravlev/just" "io" ) func main() { val := just.Must(io.ReadAll(bytes.NewBufferString("this is body!"))) fmt.Println(string(val)) }
Output: this is body!
func Slice2Chan ¶ added in v0.38.0
Slice2Chan make chan with specified capacity from source slice.
func Slice2ChanFill ¶ added in v0.38.0
func Slice2ChanFill[T any](in []T) chan T
Slice2ChanFill make chan from source slice with will already filled by all elements from source slice.
func Slice2Map ¶ added in v0.9.0
func Slice2Map[T comparable](in []T) map[T]struct{}
Slice2Map make map from slice, which contains all values from `in` as map keys.
func Slice2MapFn ¶ added in v0.33.0
func Slice2MapFn[T any, K comparable, V any](in []T, fn func(idx int, elem T) (K, V)) map[K]V
Slice2MapFn apply fn to every elem. fn should return key and value, which will be applied to result map.
func Slice2MapFnErr ¶ added in v0.34.0
func Slice2MapFnErr[T any, K comparable, V any](in []T, fn func(idx int, elem T) (K, V, error)) (map[K]V, error)
Slice2MapFnErr apply fn to every elem. fn should return key and value, which will be applied to result map. return error when at least one fn returns an error.
func SliceAddNotExists ¶ added in v0.7.0
func SliceAddNotExists[T comparable](in []T, elem T) []T
SliceAddNotExists return `in` with `elem` inside when `elem` not exists in `in`.
func SliceAll ¶ added in v0.4.0
SliceAll returns true when `fn` returns true for all elements from `in`. Returns true when in is empty.
func SliceAny ¶ added in v0.4.0
SliceAny returns true when `fn` returns true for at least one element from `in`.
func SliceApply ¶ added in v0.31.0
SliceApply handle all elements from `in` by function `fn`. Function apply sequentially.
func SliceChain ¶ added in v0.19.0
func SliceChain[T any](in ...[]T) []T
SliceChain returns a slice where all `in` slices id appended to the end. Like append(append(in[0], in[1]...), in[2]...).
func SliceChunk ¶ added in v0.11.0
SliceChunk split `in` into chunks by fn(index, elem) == true.
func SliceChunkEvery ¶ added in v0.12.0
SliceChunkEvery split `in` into chunks by size `every`
func SliceContainsElem ¶ added in v0.10.0
func SliceContainsElem[T comparable](in []T, elem T) bool
SliceContainsElem returns true when `in` contains elem.
func SliceDifference ¶ added in v0.9.0
func SliceDifference[T comparable](oldSlice, newSlice []T) []T
SliceDifference return the difference between `oldSlice` and `newSlice`. Returns only elements which presented in `newSlice` but not presented in `oldSlice`. Example: [1,2,3], [3,4,5,5,5] => [4,5,5,5]
func SliceEqualUnordered ¶ added in v0.17.0
func SliceEqualUnordered[T comparable](in1, in2 []T) bool
SliceEqualUnordered returns true when all uniq values from `in1` contains in `in2`. Useful in tests for comparing expected and actual slices. Examples:
- [1,2,3], [2,3,3,3,1,1] => true
- [1], [1,1,1] => true
- [1], [1] => true
- [1], [2] => false
func SliceFillElem ¶ added in v0.11.0
SliceFillElem returns the slice with len `l` where all elements are equal to `elem`.
func SliceFilter ¶ added in v0.3.0
SliceFilter returns slice of values from `in` where `fn(elem) == true`.
func SliceFindAllElements ¶ added in v0.13.0
SliceFindAllElements return all elem from `in` that fn(index, elem) == true.
func SliceFindAllIndexes ¶ added in v0.13.0
SliceFindAllIndexes return all indexes from `in` that fn(index, elem) == true.
func SliceGroupBy ¶ added in v0.28.0
func SliceGroupBy[K comparable, V any](in []V, fn func(V) K) map[K][]V
SliceGroupBy will group all
func SliceIntersection ¶ added in v0.17.0
func SliceIntersection[T comparable](oldSlice, newSlice []T) []T
SliceIntersection returns elements that are presented in both slices. Example: [1,2,3], [2,4,3,3,3] => [2, 3]
func SliceMap ¶ added in v0.3.0
SliceMap returns the slice where each element of `in` was handled by `fn`.
func SliceMapErr ¶ added in v0.29.0
SliceMapErr do the same thing as SliceMap, but return error when error occurs in fn.
func SliceRange ¶ added in v0.15.0
func SliceRange[T number](start, stop, step T) []T
SliceRange produces a sequence of integers from start (inclusive) to stop (exclusive) by step.
func SliceReverse ¶ added in v0.4.0
func SliceReverse[T any](in []T) []T
SliceReverse reverse the slice.
func SliceSortCopy ¶ added in v0.20.0
SliceSortCopy copy and sort slice.
func SliceUnion ¶ added in v0.8.0
func SliceUnion[T comparable](in ...[]T) []T
SliceUnion returns only uniq items from all slices.
func SliceUniq ¶ added in v0.3.0
func SliceUniq[T comparable](in []T) []T
SliceUniq returns only unique values from `in`.
func SliceWithout ¶ added in v0.10.0
SliceWithout returns the slice `in` where fn(elem) == true.
func SliceWithoutElem ¶ added in v0.10.0
func SliceWithoutElem[T comparable](in []T, elem T) []T
SliceWithoutElem returns the slice `in` that not contains `elem`.
Types ¶
type KV ¶ added in v0.18.0
type KV[K comparable, V any] struct { Key K Val V }
KV represents the key-value of map.
func MapPairs ¶ added in v0.18.0
func MapPairs[K comparable, V any](m map[K]V) []KV[K, V]
MapPairs returns slice of KV structs that contains ket-value pairs.
type NullVal ¶ added in v0.5.0
NullVal represent the nullable value for this type.
func (*NullVal[T]) SetDefault ¶ added in v0.5.0
SetDefault set value `val` if NullVal.Valid == false.
type SliceElem ¶ added in v0.13.0
type SliceElem[T any] struct { // Idx is index of element in slice. Idx int // Val is value on slice by Idx index. Val T }
SliceElem represent element of slice.
func SliceFindAll ¶ added in v0.13.0
SliceFindAll return all elem and index from `in` that fn(index, elem) == true.
func SliceFindFirst ¶ added in v0.13.0
SliceFindFirst return first elem from `in` that fn(index, elem) == true. returns index of found elem or -1 if elem not found.
func SliceFindFirstElem ¶ added in v0.14.0
func SliceFindFirstElem[T comparable](in []T, elem T) SliceElem[T]
SliceFindFirstElem return first elem from `in` that equals to `elem`.
func SliceFindLast ¶ added in v0.13.0
SliceFindLast return last elem from `in` that fn(index, elem) == true. returns index of found elem or -1 if elem not found.
func SliceFindLastElem ¶ added in v0.14.0
func SliceFindLastElem[T comparable](in []T, elem T) SliceElem[T]
SliceFindLastElem return last elem from `in` that equals to `elem`.