xtypes

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: Apache-2.0 Imports: 3 Imported by: 18

README

XTypes

License GoDoc Testing Status Go Report Card Coverage Status

Package represents basic go types and collections with extended functionalty.

Collections

Slice
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

SliceReduce([]int{1, 2, 3}, func(val int, ret *float64) { *ret += 1/float64(val) })
// 1.83333...
LazySlice
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()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

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 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