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 ContextWithTimeout(ctx context.Context, d time.Duration, fn func(context.Context) error) error
- func ContextWithTimeout2[T any](ctx context.Context, d time.Duration, fn func(context.Context) (T, error)) (T, error)
- func ErrAs[T any](err error) (T, bool)
- func ErrIsAnyOf(err error, errSlice ...error) bool
- func ErrIsNotAnyOf(err error, errSlice ...error) bool
- func If[T any](condition bool, val1, val2 T) T
- func JsonParseType[T any](bb []byte) (*T, error)
- func JsonParseTypeF[T any](filename string) (*T, error)
- func MapApply[M ~map[K]V, K comparable, V any](in M, fn func(k K, v V))
- func MapContainsKey[M ~map[K]V, K comparable, V any](m M, key K) bool
- func MapContainsKeysAll[M ~map[K]V, K comparable, V any](m M, keys []K) bool
- func MapContainsKeysAny[M ~map[K]V, K comparable, V any](m M, keys []K) bool
- func MapCopy[M ~map[K]V, K comparable, V any](m M) M
- func MapDefaults[M ~map[K]V, K comparable, V any](m, defaults M) M
- func MapDropKeys[M ~map[K]V, K comparable, V any](in M, keys ...K)
- func MapFilter[M ~map[K]V, K comparable, V any](in M, fn func(k K, v V) bool) M
- func MapFilterKeys[M ~map[K]V, K comparable, V any](in M, fn func(k K) bool) M
- func MapFilterValues[M ~map[K]V, K comparable, V any](in M, fn func(v V) bool) M
- func MapGetDefault[M ~map[K]V, K comparable, V any](in M, key K, defaultVal V) V
- func MapGetKeys[M ~map[K]V, K comparable, V any](m M) []K
- func MapGetValues[M ~map[K]V, K comparable, V any](m M) []V
- func MapJoin[M ~map[K]V, K comparable, V any](maps ...M) M
- func MapMap[M ~map[K]V, K, K1 comparable, V, V1 any](in M, fn func(K, V) (K1, V1)) map[K1]V1
- func MapMapErr[M ~map[K]V, K, K1 comparable, V, V1 any](in M, fn func(K, V) (K1, V1, error)) (map[K1]V1, error)
- func MapMerge[M ~map[K]V, K comparable, V any](m1, m2 M, fn func(k K, v1, v2 V) V) M
- func MapNotNil[T ~map[K]V, K comparable, V any](in T) T
- func MapPopKeyDefault[M ~map[K]V, K comparable, V any](in M, key K, def V) V
- func MapSetVal[M ~map[K]V, K comparable, V any](in M, key K, val V) M
- 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 PointerUnwrap[T any](in *T) T
- func PointerUnwrapDefault[T builtin | any](in *T, defaultVal T) T
- func RunAfter(ctx context.Context, ticker <-chan time.Time, runNow bool, ...) error
- func Slice2Chan[T any](in []T, capacity int) chan T
- func Slice2ChanFill[T any](in []T) chan T
- func Slice2Iter[T any](in []T) func(func(int, T) bool)
- 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 SliceCopy[T any](in []T) []T
- 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 SliceFlatMap[T, V any](in []T, fn func(val T) []V) []V
- func SliceFlatMap2[T, V any](in []T, fn func(i int, val T) []V) []V
- func SliceFromElem[T any](elem T) []T
- func SliceGetFirstN[T any](in []T, maxElems int) []T
- func SliceGroupBy[K comparable, V any](in []V, fn func(V) K) map[K][]V
- func SliceIntersection[T comparable](oldSlice, newSlice []T) []T
- func SliceIter[T any](in []T) func(func(IterContext, T) bool)
- func SliceLastDefault[T any](in []T, defaultVal T) T
- func SliceLastN[T any](in []T, n int) []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 SliceNotNil[T any](in []T) []T
- func SliceRange[T number](start, stop, step T) []T
- func SliceReplaceFirst[T any](in []T, fn func(i int, elem T) bool, newElem T)
- func SliceReplaceFirstOrAdd[T any](in []T, fn func(i int, elem T) bool, newElem T) []T
- func SliceReverse[T any](in []T) []T
- func SliceShuffle[T any](in []T)
- func SliceShuffleCopy[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 SliceUniqStable[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 StrCharCount(s string) int
- func StrGetFirst(s string, n int) string
- func StrSplitByChars(s string) []rune
- func Sum[T number](in ...T) T
- type IterContext
- type KV
- type NullVal
- type Pool
- 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 the element is not equal to the default value for this type.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { fmt.Println(just.Bool(0), just.Bool(1), just.Bool(-1)) }
Output: false true false
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 contain adapted messages from the source channel. The resulting channel will be closed after the source channel is closed.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" "strconv" ) func main() { intCh := make(chan int, 4) just.ChanPut(intCh, []int{1, 2, 3, 4}) strCh := just.ChanAdapt(intCh, strconv.Itoa) fmt.Println(just.ChanReadN(strCh, 4)) }
Output: [1 2 3 4]
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.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { ch := make(chan int, 4) just.ChanPut(ch, []int{1, 2, 3, 4}) resultSlice := just.ChanReadN(ch, 4) fmt.Println(resultSlice) }
Output: [1 2 3 4]
func ChanReadN ¶ added in v0.38.0
ChanReadN will read N messages from the channel and return the resulting slice.
func ContextWithTimeout ¶ added in v0.55.0
ContextWithTimeout will create a new context with a specified timeout and call the function with this context.
func ContextWithTimeout2 ¶ added in v0.55.0
func ContextWithTimeout2[T any](ctx context.Context, d time.Duration, fn func(context.Context) (T, error)) (T, error)
ContextWithTimeout2 will do the same as ContextWithTimeout but returns 2 arguments from the function callback.
func ErrAs ¶ added in v0.56.1
ErrAs provide a more handful way to match the error.
Example ¶
err := fmt.Errorf("problem: %w", customErr{reason: 13}) e, ok := just.ErrAs[customErr](err) fmt.Printf("%#v, %t", e, ok)
Output: just_test.customErr{reason:13}, true
func ErrIsAnyOf ¶ added in v0.41.0
ErrIsAnyOf returns true when at least one expression `errors.Is(err, errSlice[N])` return true.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" "io" "math/rand" ) func main() { funcWithRandomError := func() error { errs := []error{ io.EOF, io.ErrNoProgress, io.ErrClosedPipe, } return errs[rand.Intn(len(errs))] } err := funcWithRandomError() // Instead of switch/case you can use: fmt.Println(just.ErrIsAnyOf(err, io.EOF, io.ErrClosedPipe, io.ErrNoProgress)) }
Output: true
func ErrIsNotAnyOf ¶ added in v0.41.0
ErrIsNotAnyOf returns true when all errors from errSlice are not `errors.Is(err, errSlice[N])`.
func If ¶ added in v0.62.0
If return val1 when condition is true; val2 in other case.
Usable to simplify constructions like:
var username string if request.Username != "" { username = request.Username } else { username = "__unknown__" }
func JsonParseType ¶ added in v0.68.0
JsonParseType parse byte slice to specific type.
func JsonParseTypeF ¶ added in v0.68.0
JsonParseTypeF parse json file into specific T.
func MapApply ¶ added in v0.35.0
func MapApply[M ~map[K]V, K comparable, V any](in M, fn func(k K, v V))
MapApply applies fn to each kv pair
func MapContainsKey ¶ added in v0.27.0
func MapContainsKey[M ~map[K]V, K comparable, V any](m M, key K) bool
MapContainsKey returns true if key is exists in the map.
func MapContainsKeysAll ¶ added in v0.27.0
func MapContainsKeysAll[M ~map[K]V, K comparable, V any](m M, keys []K) bool
MapContainsKeysAll returns true when at all keys exist in the map.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { m := map[int]int{ 1: 10, 2: 20, } containsAllKeys := just.MapContainsKeysAll(m, []int{1, 2}) fmt.Println(containsAllKeys) }
Output: true
func MapContainsKeysAny ¶ added in v0.27.0
func MapContainsKeysAny[M ~map[K]V, K comparable, V any](m M, keys []K) bool
MapContainsKeysAny returns true when at least one key exists in the map.
func MapCopy ¶ added in v0.18.0
func MapCopy[M ~map[K]V, K comparable, V any](m M) M
MapCopy returns a shallow copy of the map.
func MapDefaults ¶ added in v0.18.0
func MapDefaults[M ~map[K]V, K comparable, V any](m, defaults M) M
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 MapDropKeys ¶ added in v0.54.0
func MapDropKeys[M ~map[K]V, K comparable, V any](in M, keys ...K)
MapDropKeys remove all keys from the source map. Map will change in place.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { m := map[int]int{ 1: 10, 2: 20, } just.MapDropKeys(m, 1, 3) fmt.Printf("%#v", m) }
Output: map[int]int{2:20}
func MapFilter ¶ added in v0.3.0
func MapFilter[M ~map[K]V, K comparable, V any](in M, fn func(k K, v V) bool) M
MapFilter returns the map which contains elements that `fn(key, value) == true`.
func MapFilterKeys ¶ added in v0.3.0
func MapFilterKeys[M ~map[K]V, K comparable, V any](in M, fn func(k K) bool) M
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[M ~map[K]V, K comparable, V any](in M, fn func(v V) bool) M
MapFilterValues returns the map which contains elements that `fn(value) == true`. That is a simplified version of MapFilter.
func MapGetDefault ¶ added in v0.49.0
func MapGetDefault[M ~map[K]V, K comparable, V any](in M, key K, defaultVal V) V
MapGetDefault returns a value for a given key or default value if the key is not present in the source map.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { m := map[int]int{ 1: 10, 2: 20, } val := just.MapGetDefault(m, 3, 42) fmt.Println(val) }
Output: 42
func MapGetKeys ¶ added in v0.17.0
func MapGetKeys[M ~map[K]V, K comparable, V any](m M) []K
MapGetKeys returns all keys of the map.
func MapGetValues ¶ added in v0.17.0
func MapGetValues[M ~map[K]V, K comparable, V any](m M) []V
MapGetValues returns all values of the map. Not Uniq, unordered.
func MapJoin ¶ added in v0.47.0
func MapJoin[M ~map[K]V, K comparable, V any](maps ...M) M
MapJoin will create a new map containing all key-value pairs from app input maps. If several maps have duplicate keys - the last write wins.
func MapMap ¶ added in v0.26.0
func MapMap[M ~map[K]V, K, K1 comparable, V, V1 any](in M, fn func(K, V) (K1, V1)) map[K1]V1
MapMap applies fn to all kv pairs from in.
func MapMapErr ¶ added in v0.40.0
func MapMapErr[M ~map[K]V, K, K1 comparable, V, V1 any](in M, fn func(K, V) (K1, V1, error)) (map[K1]V1, error)
MapMapErr applies fn to all kv pairs from in.
func MapMerge ¶ added in v0.3.0
func MapMerge[M ~map[K]V, K comparable, V any](m1, m2 M, fn func(k K, v1, v2 V) V) M
MapMerge returns the map which contains all keys from m1, m2, and values from `fn(key, m1Value, m2Value)`.
func MapNotNil ¶ added in v0.53.0
func MapNotNil[T ~map[K]V, K comparable, V any](in T) T
MapNotNil returns the source map when it is not nil or creates an empty instance of this type.
func MapPopKeyDefault ¶ added in v0.65.0
func MapPopKeyDefault[M ~map[K]V, K comparable, V any](in M, key K, def V) V
MapPopKeyDefault will return value for given key and delete this key from source map. In case of key do not presented in map - returns default value.
func MapSetVal ¶ added in v0.71.0
func MapSetVal[M ~map[K]V, K comparable, V any](in M, key K, val V) M
MapSetVal update key with value for given map and return updated map.
func MaxDefault ¶ added in v0.25.0
func MaxDefault[T number](in ...T) T
MaxDefault returns the max element from `in` or the default value for a specified type when `in` is empty.
func MaxOr ¶ added in v0.25.0
func MaxOr[T number](defaultVal T, in ...T) T
MaxOr returns the max element from `in` or defaultVal when `in` is empty.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { values := []int{10, 20, 30} maxValue := just.MaxOr(999, values...) // This will print 30, because you have non-empty slice. fmt.Println(maxValue) }
Output: 30
func Min ¶
func Min[T number](in ...T) T
Min returns the min number from `in`.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { values := []int{10, 20, 30} minValue := just.Min(values...) fmt.Println(minValue) }
Output: 10
func MinDefault ¶ added in v0.25.0
func MinDefault[T number](in ...T) T
MinDefault returns the min element from `in` or the default value for a specified type when `in` is empty.
func MinOr ¶ added in v0.25.0
func MinOr[T number](defaultVal T, in ...T) T
MinOr returns the min element from `in` or defaultVal when `in` is empty.
func Must ¶ added in v0.39.0
Must will panic on an 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 PointerUnwrap ¶ added in v0.43.0
func PointerUnwrap[T any](in *T) T
PointerUnwrap returns the value from the pointer.
func PointerUnwrapDefault ¶ added in v0.52.0
func PointerUnwrapDefault[T builtin | any](in *T, defaultVal T) T
PointerUnwrapDefault returns a value from pointer or defaultVal when input is an empty pointer.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { someFuncThatReturnsNil := func() *int { return nil } value := just.PointerUnwrapDefault(someFuncThatReturnsNil(), 42) fmt.Println(value) }
Output: 42
func Slice2Chan ¶ added in v0.38.0
Slice2Chan make chan with specified capacity from source slice.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30, 40, 50} ch := just.Slice2Chan(input, 0) result := just.ChanReadN(ch, 5) fmt.Println(result) }
Output: [10 20 30 40 50]
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 Slice2Iter ¶ added in v0.66.0
Slice2Iter create an iterator from slice. Check this docs https://go.dev/ref/spec#For_range.
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.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30, 30} result := just.Slice2Map(input) fmt.Println(result) }
Output: map[10:{} 20:{} 30:{}]
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`.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30} result := just.SliceAddNotExists(input, 42) fmt.Println(result) }
Output: [10 20 30 42]
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 handles all elements from `in` by function `fn`. Function applies sequentially.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30} just.SliceApply(input, func(i int, v int) { fmt.Println(i, v) }) }
Output: 0 10 1 20 2 30
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`
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30, 40, 50} result := just.SliceChunkEvery(input, 2) fmt.Println(result) }
Output: [[10 20] [30 40] [50]]
func SliceContainsElem ¶ added in v0.10.0
func SliceContainsElem[T comparable](in []T, elem T) bool
SliceContainsElem returns true when `in` contains elem.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30} contains20 := just.SliceContainsElem(input, 20) fmt.Println(contains20) }
Output: true
func SliceCopy ¶ added in v0.51.0
func SliceCopy[T any](in []T) []T
SliceCopy return a copy of source slice.
func SliceDifference ¶ added in v0.9.0
func SliceDifference[T comparable](oldSlice, newSlice []T) []T
SliceDifference returns the difference between `oldSlice` and `newSlice`. Returns only elements 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 a slice of values from `in` where `fn(elem) == true`.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { input := []int{10, 20, 30} filtered := just.SliceFilter(input, func(v int) bool { return v > 15 }) fmt.Println(filtered) }
Output: [20 30]
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 SliceFlatMap ¶ added in v0.60.0
func SliceFlatMap[T, V any](in []T, fn func(val T) []V) []V
SliceFlatMap applies `fn` to each element of `in` and join all output slices.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" "strconv" ) func main() { input := []int{1, 2, 3} result := just.SliceFlatMap(input, func(i int) []string { return []string{strconv.Itoa(i), strconv.Itoa(i)} }) fmt.Println(result) }
Output: [1 1 2 2 3 3]
func SliceFlatMap2 ¶ added in v0.60.0
SliceFlatMap2 does the same as SliceFlatMap but receives the index of the element.
func SliceFromElem ¶ added in v0.45.0
func SliceFromElem[T any](elem T) []T
SliceFromElem return a slice which contains only one element `elem`.
func SliceGetFirstN ¶ added in v0.46.0
SliceGetFirstN return a subslice of source slice, which will contains not more than `maxElems` items.
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 SliceIter ¶ added in v0.72.0
func SliceIter[T any](in []T) func(func(IterContext, T) bool)
SliceIter create an iterator from slice. The first argument will contain a useful context struct.
func SliceLastDefault ¶ added in v0.62.2
func SliceLastDefault[T any](in []T, defaultVal T) T
SliceLastDefault return a last elem from slice or default value in case of zero slice.
func SliceLastN ¶ added in v0.70.0
SliceLastN return up to last n elements from input slice in original order.
func SliceMap ¶ added in v0.3.0
SliceMap returns the slice where each element of `in` was handled by `fn`.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" ) func main() { unsignedIntegers := []uint{1, 2, 3, 4} multiply := func(i uint) int { return int(i) * 10 } integers := just.SliceMap(unsignedIntegers, multiply) fmt.Println(integers) }
Output: [10 20 30 40]
func SliceMapErr ¶ added in v0.29.0
SliceMapErr does the same thing as SliceMap but returns an error when an error occurs in fn.
func SliceNotNil ¶ added in v0.53.0
func SliceNotNil[T any](in []T) []T
SliceNotNil return source slice when it is not nil or create empty instance of this type.
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 SliceReplaceFirst ¶ added in v0.58.0
SliceReplaceFirst will replace the first element in `in` such that fn(index, elem) == true. Will do nothing if the element is not found.
func SliceReplaceFirstOrAdd ¶ added in v0.59.0
SliceReplaceFirstOrAdd will replace the first element in `in` such that fn(index, elem) == true. Will add a `newElem` if the element is not found.
func SliceReverse ¶ added in v0.4.0
func SliceReverse[T any](in []T) []T
SliceReverse reverse the slice.
func SliceShuffle ¶ added in v0.69.0
func SliceShuffle[T any](in []T)
SliceShuffle will shuffle the slice in-place.
func SliceShuffleCopy ¶ added in v0.69.0
func SliceShuffleCopy[T any](in []T) []T
SliceShuffleCopy will make a copy and shuffle 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 unique values from `in`.
Example ¶
package main import ( "fmt" "github.com/kazhuravlev/just" "sort" ) func main() { data := []int{1, 1, 2, 2, 3, 3} uniqData := just.SliceUniq(data) sort.Ints(uniqData) fmt.Println(uniqData) }
Output: [1 2 3]
func SliceUniqStable ¶ added in v0.64.0
func SliceUniqStable[T comparable](in []T) []T
SliceUniqStable returns unique values from `in`. Keep original ordering.
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`.
func SliceZip ¶ added in v0.10.0
func SliceZip[T any](in ...[]T) [][]T
SliceZip returns merged together the values of each of the arrays with the values at the corresponding position. If the len of `in` is different - will use smaller one.
func StrCharCount ¶ added in v0.63.0
StrCharCount returns rune count in string.
func StrGetFirst ¶ added in v0.67.0
StrGetFirst return string contains first N valid chars.
func StrSplitByChars ¶ added in v0.63.0
StrSplitByChars returns slice of runes in string.
Types ¶
type IterContext ¶ added in v0.72.0
type KV ¶ added in v0.18.0
type KV[K comparable, V any] struct { Key K Val V }
KV represents the key-value of the map.
func MapPairs ¶ added in v0.18.0
func MapPairs[M ~map[K]V, K comparable, V any](m M) []KV[K, V]
MapPairs returns a slice of KV structs that contains key-value pairs.
type NullVal ¶ added in v0.5.0
NullVal represents the nullable value for this type. Deprecated: Use github.com/kazhuravlev/optional.
func NullDefaultFalse ¶ added in v0.57.0
func NullDefaultFalse[T comparable](val T) NullVal[T]
NullDefaultFalse returns NullVal for this type with Valid=true only when `val` is not equal to default value of type T.
func (NullVal[T]) MarshalYAML ¶ added in v0.55.2
MarshalYAML implements the interface for marshaling yaml.
func (*NullVal[T]) SetDefault ¶ added in v0.5.0
SetDefault set value `val` if NullVal.Valid == false.
func (*NullVal[T]) UnmarshalYAML ¶ added in v0.55.2
UnmarshalYAML implements the interface for unmarshalling yaml.
type Pool ¶ added in v0.50.0
type Pool[T any] struct { // contains filtered or unexported fields }
func NewPool ¶ added in v0.50.0
NewPool returns a new pool with concrete type and resets fn.
Example ¶
package main import ( "bytes" "fmt" "github.com/kazhuravlev/just" ) func main() { p := just.NewPool( func() *bytes.Buffer { return bytes.NewBuffer(nil) }, func(buf *bytes.Buffer) { fmt.Println(buf.String()); buf.Reset() }, ) buf := p.Get() buf.WriteString("Some data") // This example showing that Pool will call reset callback // on each Pool.Put call. p.Put(buf) }
Output: Some data
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`.