xtypes

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: Apache-2.0 Imports: 5 Imported by: 28

README

XTypes

License GoDoc Testing Status Go Report Card Coverage Status

Package represents basic go types and collections with extended functionalty.

Collections

Slice
xtypes.Slice[int]([]int{1, 2, 3}).
  Filter(func(val int) bool { return val > 1 }).
  // [2, 3]
  Apply(func(val int) int { return val * val }).
  // [4, 9]
  Sort(func(a, b int) bool { return a > b }).
  // [9, 4]
  ReduceIntoOne(func(val int, ret *int) { *ret += val })
  // 13

xtypes.SliceReduce([]int{1, 2, 3}, func(val int, ret *float64) { *ret += 1/float64(val) })
// 1.83333...
LazySlice
xtypes.NewLazySlice([]int{1, 2, 3}).
  Filter(func(val int) bool { return val > 1 }).
  // [2, 3]
  Apply(func(val int) int { return val * val }).
  // [4, 9]
  Commit()
Any type boxing
val := xtypes.Any(1)

fmt.Println(val.Int()) // 1
fmt.Println(val.Float64()) // 1.0
fmt.Println(val.String()) // "1"
fmt.Println(val.Bool()) // true

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBoxContainerIsNil = errors.New("container is nil")
)

Functions

func FirstVal

func FirstVal[T any](v ...*T) *T

FirstVal returns the first non-nil value.

func Generator

func Generator[T any](ctx context.Context, size int, genFn func(ctx context.Context, prev T) (T, bool)) <-chan T

Generator returns generator with context Done support

func GeneratorSimple

func GeneratorSimple[T any](size int, genFn func(prev T) (T, bool)) <-chan T

GeneratorSimple returns generator which is not controlled from external

func MapEqual

func MapEqual[T ~map[K]V, K comparable, V comparable](mp, otherMap T) bool

MapEqual comparing two maps of the same type

func MapReduce

func MapReduce[K comparable, V any, R any](mp map[K]V, reduce func(key K, val V, ret *R)) R

MapReduce map and return new value

func Max

func Max[T constraints.Ordered](x, y T) T

Max value Deprecated: use `max` in go1.21+ instead.

func Min

func Min[T constraints.Ordered](x, y T) T

Min value Deprecated: use `min` in go1.21+ instead.

func SliceReduce

func SliceReduce[T any, R any](sl []T, reduce func(val T, ret *R)) R

SliceReduce slice and return new value

func SliceUnique

func SliceUnique[T comparable](sl []T) []T

SliceUnique return new slice without duplicated values

Types

type Any added in v0.3.0

type Any struct {
	Val any
}

Any is a type alias for any.

func (*Any) Bool added in v0.3.0

func (a *Any) Bool() bool

Bool retrieves the value of the Any instance as a boolean. It uses the gocast.Bool function to convert the value to a boolean. It returns false if the Any instance is nil or if the value cannot be converted to a boolean. Otherwise, it returns the converted boolean value.

func (*Any) Float64 added in v0.3.0

func (a *Any) Float64() float64

Float64 retrieves the value of the Any instance as a 64-bit floating-point number. It uses the gocast.Number function to convert the value to a 64-bit floating-point number. It returns 0 if the Any instance is nil or if the value cannot be converted to a 64-bit floating-point number. Otherwise, it returns the converted 64-bit floating-point number.

func (*Any) Get added in v0.3.0

func (a *Any) Get() any

Get retrieves the value of the Any instance. It returns nil if the Any instance is nil. Otherwise, it returns the value.

func (*Any) GetOr added in v0.3.0

func (a *Any) GetOr(def any) any

GetOr retrieves the value of the Any instance or returns a default value. It returns the value if the Any instance is not nil, otherwise it returns the default value.

func (*Any) Int added in v0.3.0

func (a *Any) Int() int

Int retrieves the value of the Any instance as an integer. It uses the gocast.Number function to convert the value to an integer. It returns 0 if the Any instance is nil or if the value cannot be converted to an integer. Otherwise, it returns the converted integer value. It also returns an error if the conversion fails.

func (*Any) Int64 added in v0.3.0

func (a *Any) Int64() int64

Int64 retrieves the value of the Any instance as a 64-bit integer. It uses the gocast.Number function to convert the value to a 64-bit integer. It returns 0 if the Any instance is nil or if the value cannot be converted to a 64-bit integer. Otherwise, it returns the converted 64-bit integer value.

func (*Any) IsEmpty added in v0.3.0

func (a *Any) IsEmpty() bool

IsEmpty checks if the value is empty. It uses the gocast.IsEmpty function to determine if the value is empty. It returns true if the value is empty, otherwise false. It also checks if the Any instance itself is nil.

func (*Any) IsNil added in v0.3.0

func (a *Any) IsNil() bool

IsNil checks if the value is nil. It returns true if the value is nil, otherwise false.

func (*Any) Set added in v0.3.0

func (a *Any) Set(val any) error

Set sets the value of the Any instance. It returns an error if the Any instance is nil. Otherwise, it sets the value and returns nil.

func (*Any) String added in v0.3.0

func (a *Any) String() string

String retrieves the value of the Any instance as a string. It uses the gocast.String function to convert the value to a string. It returns an empty string if the Any instance is nil or if the value cannot be converted to a string. Otherwise, it returns the converted string value.

func (*Any) TryBool added in v0.3.0

func (a *Any) TryBool() (bool, error)

TryBool attempts to retrieve the value of the Any instance as a boolean. It uses the gocast.TryCast function to convert the value to a boolean. It returns an error if the Any instance is nil or if the value cannot be converted to a boolean. Otherwise, it returns the converted boolean value. It also returns an error if the conversion fails.

func (*Any) TryFloat64 added in v0.3.0

func (a *Any) TryFloat64() (float64, error)

TryFloat64 attempts to retrieve the value of the Any instance as a 64-bit floating-point number. It uses the gocast.TryNumber function to convert the value to a 64-bit floating-point number. It returns an error if the Any instance is nil or if the value cannot be converted to a 64-bit floating-point number. Otherwise, it returns the converted 64-bit floating-point number. It also returns an error if the conversion fails.

func (*Any) TryInt added in v0.3.0

func (a *Any) TryInt() (int, error)

TryInt attempts to retrieve the value of the Any instance as an integer. It uses the gocast.TryNumber function to convert the value to an integer. It returns an error if the Any instance is nil or if the value cannot be converted to an integer. Otherwise, it returns the converted integer value. It also returns an error if the conversion fails.

func (*Any) TryInt64 added in v0.3.0

func (a *Any) TryInt64() (int64, error)

TryInt64 attempts to retrieve the value of the Any instance as a 64-bit integer. It uses the gocast.TryNumber function to convert the value to a 64-bit integer. It returns an error if the Any instance is nil or if the value cannot be converted to a 64-bit integer. Otherwise, it returns the converted 64-bit integer value. It also returns an error if the conversion fails.

func (*Any) TryString added in v0.3.0

func (a *Any) TryString() (string, error)

TryString attempts to retrieve the value of the Any instance as a string. It uses the gocast.TryStr function to convert the value to a string. It returns an error if the Any instance is nil or if the value cannot be converted to a string. Otherwise, it returns the converted string value. It also returns an error if the conversion fails.

func (*Any) TryUint added in v0.3.0

func (a *Any) TryUint() (uint, error)

TryUint attempts to retrieve the value of the Any instance as an unsigned integer.

func (*Any) TryUint64 added in v0.3.0

func (a *Any) TryUint64() (uint64, error)

TryUint64 attempts to retrieve the value of the Any instance as a 64-bit unsigned integer. It uses the gocast.TryNumber function to convert the value to a 64-bit unsigned integer. It returns an error if the Any instance is nil or if the value cannot be converted to a 64-bit unsigned integer. Otherwise, it returns the converted 64-bit unsigned integer value. It also returns an error if the conversion fails.

func (*Any) Uint added in v0.3.0

func (a *Any) Uint() uint

Uint retrieves the value of the Any instance as an unsigned integer. It uses the gocast.Number function to convert the value to an unsigned integer. It returns 0 if the Any instance is nil or if the value cannot be converted to an unsigned integer. Otherwise, it returns the converted unsigned integer value.

func (*Any) Uint64 added in v0.3.0

func (a *Any) Uint64() uint64

Uint64 retrieves the value of the Any instance as a 64-bit unsigned integer. It uses the gocast.Number function to convert the value to a 64-bit unsigned integer. It returns 0 if the Any instance is nil or if the value cannot be converted to a 64-bit unsigned integer. Otherwise, it returns the converted 64-bit unsigned integer value.

type LazySlice

type LazySlice[T any] struct {
	// contains filtered or unexported fields
}

LazySlice type extended with banch of processing methods

func NewLazySlice

func NewLazySlice[T any](sl []T) *LazySlice[T]

NewLazySlice creates new lazy slice

func (*LazySlice[T]) Apply

func (sl *LazySlice[T]) Apply(apply func(val T) T) *LazySlice[T]

Apply the function to each element of the slice

func (*LazySlice[T]) Commit

func (sl *LazySlice[T]) Commit() Slice[T]

Commit all changes and return new slice

func (*LazySlice[T]) Each

func (sl *LazySlice[T]) Each(iter func(val T)) *LazySlice[T]

Each iterates every element in the list

func (*LazySlice[T]) Filter

func (sl *LazySlice[T]) Filter(filter func(val T) bool) *LazySlice[T]

Filter slice values and return new slice without excluded values

type Map

type Map[K comparable, V any] map[K]V

Map type extended with banch of processing methods

func MapApply

func MapApply[K comparable, V any, NK comparable, NV any](mp map[K]V, apply func(key K, val V) (NK, NV)) Map[NK, NV]

AmpApply the function to each element of the map

func (Map[K, V]) Apply

func (mp Map[K, V]) Apply(apply func(key K, val V) (K, V)) Map[K, V]

Apply the function to each element of the slice

func (Map[K, V]) Copy

func (mp Map[K, V]) Copy() Map[K, V]

Copy map

func (Map[K, V]) Each

func (mp Map[K, V]) Each(iter func(key K, val V)) Map[K, V]

Each iterates every element in the map

func (Map[K, V]) Filter

func (mp Map[K, V]) Filter(filter func(key K, val V) bool) Map[K, V]

Filter map values and return new map without excluded values

func (Map[K, V]) Keys

func (mp Map[K, V]) Keys() Slice[K]

Keys returns all keys of the map

func (Map[K, V]) Merge added in v0.2.0

func (mp Map[K, V]) Merge(maps ...Map[K, V]) Map[K, V]

Merge maps into one

func (Map[K, V]) MergeConflict added in v0.2.0

func (mp Map[K, V]) MergeConflict(conflictFunc func(V, V) V, maps ...Map[K, V]) Map[K, V]

MergeConflict maps into one with conflict function resolver

func (Map[K, V]) ReduceIntoOne

func (mp Map[K, V]) ReduceIntoOne(reduce func(key K, val V, ret *V)) V

ReduceIntoOne map and return single value

func (Map[K, V]) Set

func (mp Map[K, V]) Set(key K, val V) Map[K, V]

Set value to the map

func (Map[K, V]) Values

func (mp Map[K, V]) Values() Slice[V]

Values returns all values of the map

type Slice

type Slice[T any] []T

Slice type extended with banch of processing methods

func SliceApply

func SliceApply[T any, N any](sl []T, apply func(val T) N) Slice[N]

SliceApply the function to each element of the slice

func (Slice[T]) Append

func (sl Slice[T]) Append(val T) Slice[T]

Append value to slice

func (Slice[T]) Apply

func (sl Slice[T]) Apply(apply func(val T) T) Slice[T]

Apply the function to each element of the slice

func (Slice[T]) BinarySearch

func (sl Slice[T]) BinarySearch(fn func(val T) bool) int

BinarySearch slice values

func (Slice[T]) Copy

func (sl Slice[T]) Copy() Slice[T]

Copy slice

func (Slice[T]) Each

func (sl Slice[T]) Each(iter func(val T)) Slice[T]

Each iterates every element in the list

func (Slice[T]) Filter

func (sl Slice[T]) Filter(filter func(val T) bool) Slice[T]

Filter slice values and return new slice without excluded values

func (Slice[T]) First

func (sl Slice[T]) First() *T

First value from slice

func (Slice[T]) FirstOr

func (sl Slice[T]) FirstOr(def T) T

FirstOr value from slice or default value

func (Slice[T]) Has

func (sl Slice[T]) Has(fn func(val T) bool) bool

Has slice values

func (Slice[T]) IndexOf

func (sl Slice[T]) IndexOf(fn func(val T) bool) int

IndexOf slice values

func (Slice[T]) Last

func (sl Slice[T]) Last() *T

Last value from slice

func (Slice[T]) LastOr

func (sl Slice[T]) LastOr(def T) T

LastOr value from slice or default value

func (Slice[T]) Len

func (sl Slice[T]) Len() int

Len of slice

func (Slice[T]) Prepend

func (sl Slice[T]) Prepend(val T) Slice[T]

Prepend value to slice

func (Slice[T]) ReduceIntoOne

func (sl Slice[T]) ReduceIntoOne(apply func(val T, ret *T)) T

ReduceIntoOne slice and return new single value

func (Slice[T]) RemoveAt

func (sl Slice[T]) RemoveAt(i int) Slice[T]

RemoveAt value at index from slice

func (Slice[T]) RemoveRange

func (sl Slice[T]) RemoveRange(i, j int) Slice[T]

RemoveRange values from slice

func (Slice[T]) Sort

func (sl Slice[T]) Sort(cmp func(a, b T) bool) Slice[T]

Sort slice values

func (Slice[T]) ValueOr

func (sl Slice[T]) ValueOr(i int, def T) T

ValueOr return value from slice or default value

Jump to

Keyboard shortcuts

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