pie

package module
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: MIT Imports: 9 Imported by: 96

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T constraints.Integer | constraints.Float](val T) T

Abs returns the absolute value.

func All

func All[T any](ss []T, fn func(value T) bool) bool

All will return true if all callbacks return true. It follows the same logic as the all() function in Python.

If the list is empty then true is always returned.

func Any

func Any[T any](ss []T, fn func(value T) bool) bool

Any will return true if any callbacks return true. It follows the same logic as the any() function in Python.

If the list is empty then false is always returned.

func AreSorted

func AreSorted[T constraints.Ordered](ss []T) bool

AreSorted will return true if the slice is already sorted. It is a wrapper for sort.SliceIsSorted.

func AreUnique

func AreUnique[T comparable](ss []T) bool

AreUnique will return true if the slice contains elements that are all different (unique) from each other.

func Average

func Average[T constraints.Integer | constraints.Float](ss []T) float64

Average is the average of all of the elements, or zero if there are no elements.

func Bottom

func Bottom[T any](ss []T, n int) (top []T)

Bottom will return n elements from bottom

that means that elements is taken from the end of the slice for this [1,2,3] slice with n == 2 will be returned [3,2] if the slice has less elements then n that'll return all elements if n < 0 it'll return empty slice.

func Chunk added in v2.3.0

func Chunk[T any](ss []T, chunkLength int) [][]T

Chunk splits the input and returns multi slices whose length equals chunkLength, except for the last slice which may contain fewer elements.

Examples:

Chunk([1, 2, 3], 4) => [ [1, 2, 3] ]
Chunk([1, 2, 3], 3) => [ [1, 2, 3] ]
Chunk([1, 2, 3], 2) => [ [1, 2], [3] ]
Chunk([1, 2, 3], 1) => [ [1], [2], [3] ]
Chunk([], 1)        => [ [] ]
Chunk([1, 2, 3], 0) => panic: chunkLength should be greater than 0

func Contains

func Contains[T comparable](ss []T, lookingFor T) bool

Contains returns true if the element exists in the slice.

When using slices of pointers it will only compare by address, not value.

func Delete added in v2.7.0

func Delete[T any](ss []T, idx ...int) []T

Removes elements at indices in idx from input slice, returns resulting slice. If an index is out of bounds, skip it.

func Diff

func Diff[T comparable](ss []T, against []T) (added, removed []T)

Diff returns the elements that needs to be added or removed from the first slice to have the same elements in the second slice.

The order of elements is not taken into consideration, so the slices are treated sets that allow duplicate items.

The added and removed returned may be blank respectively, or contain upto as many elements that exists in the largest slice.

func DropTop

func DropTop[T any](ss []T, n int) (drop []T)

DropTop will return the rest slice after dropping the top n elements if the slice has less elements then n that'll return empty slice if n < 0 it'll return empty slice.

func DropWhile

func DropWhile[T comparable](ss []T, f func(s T) bool) (ss2 []T)

Drop items from the slice while f(item) is true. Afterwards, return every element until the slice is empty. It follows the same logic as the dropwhile() function from itertools in Python.

func Each

func Each[T any](ss []T, fn func(T)) []T

Each is more condensed version of Transform that allows an action to happen on each elements and pass the original slice on.

pie.Each(cars, func (car *Car) {
    fmt.Printf("Car color is: %s\n", car.Color)
})

Pie will not ensure immutability on items passed in so they can be manipulated, if you choose to do it this way, for example:

// Set all car colors to Red.
pie.Each(cars, func (car *Car) {
    car.Color = "Red"
})

func Equals

func Equals[T comparable](ss []T, rhs []T) bool

Equals compare elements from the start to the end,

if they are the same is considered the slices are equal if all elements are the same is considered the slices are equal if each slice == nil is considered that they're equal

if element realizes Equals interface it uses that method, in other way uses default compare

func Filter

func Filter[T any](ss []T, condition func(T) bool) (ss2 []T)

Filter will return a new slice containing only the elements that return true from the condition. The returned slice may contain zero elements (nil).

FilterNot works in the opposite way of Filter.

func FilterNot

func FilterNot[T any](ss []T, condition func(T) bool) (ss2 []T)

FilterNot works the same as Filter, with a negated condition. That is, it will return a new slice only containing the elements that returned false from the condition. The returned slice may contain zero elements (nil).

func FindFirstUsing

func FindFirstUsing[T any](ss []T, fn func(value T) bool) int

FindFirstUsing will return the index of the first element when the callback returns true or -1 if no element is found. It follows the same logic as the findIndex() function in Javascript.

If the list is empty then -1 is always returned.

func First added in v2.1.0

func First[T any](ss []T) T

First returns the first element or a zero value if there are no elements.

func FirstOr

func FirstOr[T any](ss []T, defaultValue T) T

FirstOr returns the first element or a default value if there are no elements.

func Flat added in v2.4.0

func Flat[T any](ss [][]T) (ss2 []T)

Flat flattens the two-dimensional slice into one-dimensional slice. Slices of zero-length are ignored.

Examples:

Flat([[100], [101, 102], [102, 103]])   => [100, 101, 102, 102, 103]
Flat([nil, [101, 102], []])             => [101, 102]

func Float64

func Float64[T constraints.Ordered](x T) float64

Float64 transforms a value into a float64. This should only be used on slices that resolve to strings that represent numbers. An invalid value will use zero.

func Float64s

func Float64s[T constraints.Ordered](ss []T) []float64

Float64s transforms each element to a float64.

func Group

func Group[T comparable](ss []T) map[T]int

Group returns a map of the value with an individual count.

func GroupBy added in v2.5.0

func GroupBy[T comparable, U any](values []U, getKey func(U) T) map[T][]U

GroupBy groups slice elements by key returned by getKey function for each slice element.

It returns a map in which slices of elements of original slice are matched to keys defined by getKey function. It returns non-nil map, if empty or nil slice is passed.

For example, if you want to group integers by their remainder from division by 5, you can use this function as follows:

_ = pie.GroupBy(
    []int{23, 76, 37, 11, 23, 47},
    func(num int) int {
        return num % 5
    },
)

In above case map {1:[76, 11], 2:[37, 47], 3:[23, 23]} is returned.

func Insert

func Insert[T any](ss []T, index int, values ...T) []T

Insert a value at an index.

func Int

func Int[T constraints.Ordered](x T) int

Int transforms a value into an int. This should only be used on slices that resolve to strings that represent numbers. An invalid value will use zero and fractional values will be truncated.

func Intersect

func Intersect[T comparable](ss []T, slices ...[]T) (ss2 []T)

Intersect returns items that exist in all lists.

It returns slice without any duplicates. If zero slice arguments are provided, then nil is returned.

func Ints

func Ints[T constraints.Ordered](ss []T) []int

Ints transforms each element to an integer.

func JSONBytes

func JSONBytes[T constraints.Ordered](ss []T) []byte

JSONBytes returns the JSON encoded array as bytes.

One important thing to note is that it will treat a nil slice as an empty slice to ensure that the JSON value return is always an array.

func JSONBytesIndent

func JSONBytesIndent[T constraints.Ordered](ss []T, prefix, indent string) []byte

JSONBytesIndent returns the JSON encoded array as bytes with indent applied.

One important thing to note is that it will treat a nil slice as an empty slice to ensure that the JSON value return is always an array. See json.MarshalIndent for details.

func JSONString

func JSONString[T constraints.Ordered](ss []T) string

JSONString returns the JSON encoded array as a string.

One important thing to note is that it will treat a nil slice as an empty slice to ensure that the JSON value return is always an array.

func JSONStringIndent

func JSONStringIndent[T constraints.Ordered](ss []T, prefix, indent string) string

JSONStringIndent returns the JSON encoded array as a string with indent applied.

One important thing to note is that it will treat a nil slice as an empty slice to ensure that the JSON value return is always an array. See json.MarshalIndent for details.

func Join

func Join[T constraints.Ordered](ss []T, glue string) (s string)

Join returns a string from joining each of the elements.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns the keys in the map. All of the items will be unique.

Due to Go's randomization of iterating maps the order is not deterministic.

func Last added in v2.1.0

func Last[T any](ss []T) T

Last returns the last element or a zero value if there are no elements.

func LastOr

func LastOr[T any](ss []T, defaultValue T) T

LastOr returns the last element or a default value if there are no elements.

func Map

func Map[T any, U any](ss []T, fn func(T) U) (ss2 []U)

Map will return a new slice where each element has been mapped (transformed). The number of elements returned will always be the same as the input.

Be careful when using this with slices of pointers. If you modify the input value it will affect the original slice. Be sure to return a new allocated object or deep copy the existing one.

func Max

func Max[T constraints.Ordered](ss []T) (max T)

Max is the maximum value, or zero.

func Median

func Median[T constraints.Integer | constraints.Float](ss []T) T

Median returns the value separating the higher half from the lower half of a data sample.

Zero is returned if there are no elements in the slice.

If the number of elements is even, then the ElementType mean of the two "median values" is returned.

func Min

func Min[T constraints.Ordered](ss []T) (min T)

Min is the minimum value, or zero.

func Mode

func Mode[T comparable](ss []T) []T

Mode returns a new slice containing the most frequently occuring values.

The number of items returned may be the same as the input or less. It will never return zero items unless the input slice has zero items.

func Pop

func Pop[T any](ss *[]T) (popped *T)

Pop the first element of the slice

Usage Example:

type knownGreetings []string
greetings := knownGreetings{"ciao", "hello", "hola"}
for greeting := greetings.Pop(); greeting != nil; greeting = greetings.Pop() {
    fmt.Println(*greeting)
}

func Product

func Product[T constraints.Integer | constraints.Float](ss []T) (product T)

Product is the product of all of the elements.

func Random

func Random[T constraints.Integer | constraints.Float](ss []T, source rand.Source) T

Random returns a random element by your rand.Source, or zero.

func Reduce

func Reduce[T any](ss []T, reducer func(T, T) T) (el T)

Reduce continually applies the provided function over the slice. Reducing the elements to a single value.

Returns a zero value of T if there are no elements in the slice. It will panic if the reducer is nil and the slice has more than one element (required to invoke reduce). Otherwise returns result of applying reducer from left to right.

func Reverse

func Reverse[T any](ss []T) []T

Reverse returns a new copy of the slice with the elements ordered in reverse. This is useful when combined with Sort to get a descending sort order:

ss.Sort().Reverse()

func Rotate added in v2.8.0

func Rotate[T any](ss []T, n int) []T

Rotate return slice circularly rotated by a number of positions n. If n is positive, the slice is rotated right. If n is negative, the slice is rotated left.

func Send

func Send[T any](ctx context.Context, ss []T, ch chan<- T) []T

Send sends elements to channel in normal act it sends all elements but if func canceled it can be less

it locks execution of gorutine it doesn't close channel after work returns sent elements if len(this) != len(old) considered func was canceled

func Sequence

func Sequence[T constraints.Integer | constraints.Float](ss []T, params ...int) []T

Sequence generates all numbers in range or returns nil if params invalid

There are 3 variations to generate:

  1. [0, n).
  2. [min, max).
  3. [min, max) with step.

if len(params) == 1 considered that will be returned slice between 0 and n, where n is the first param, [0, n). if len(params) == 2 considered that will be returned slice between min and max, where min is the first param, max is the second, [min, max). if len(params) > 2 considered that will be returned slice between min and max with step, where min is the first param, max is the second, step is the third one, [min, max) with step, others params will be ignored

func SequenceUsing

func SequenceUsing[T any](ss []T, creator func(int) T, params ...int) []T

SequenceUsing generates slice in range using creator function

There are 3 variations to generate:

  1. [0, n).
  2. [min, max).
  3. [min, max) with step.

if len(params) == 1 considered that will be returned slice between 0 and n, where n is the first param, [0, n). if len(params) == 2 considered that will be returned slice between min and max, where min is the first param, max is the second, [min, max). if len(params) > 2 considered that will be returned slice between min and max with step, where min is the first param, max is the second, step is the third one, [min, max) with step, others params will be ignored

func Shift

func Shift[T any](ss []T) (T, []T)

Shift will return two values: the shifted value and the rest slice. if the slice is empty then returned shifted value is the zero value of the slice elements and the rest slice is empty slice

func Shuffle

func Shuffle[T any](ss []T, source rand.Source) []T

Shuffle returns a new shuffled slice by your rand.Source. The original slice is not modified.

func Sort

func Sort[T constraints.Ordered](ss []T) []T

Sort works similar to sort.SliceType(). However, unlike sort.SliceType the slice returned will be reallocated as to not modify the input slice.

See Reverse() and AreSorted().

func SortStableUsing

func SortStableUsing[T comparable](ss []T, less func(a, b T) bool) []T

SortStableUsing works similar to sort.SliceStable. However, unlike sort.SliceStable the slice returned will be reallocated as to not modify the input slice.

func SortUsing

func SortUsing[T any](ss []T, less func(a, b T) bool) []T

SortUsing works similar to sort.Slice. However, unlike sort.Slice the slice returned will be reallocated as to not modify the input slice.

func Stddev

func Stddev[T constraints.Integer | constraints.Float](ss []T) float64

Stddev is the standard deviation

func String

func String[T constraints.Ordered](s T) string

String transforms a value into a string. Nil values will be treated as empty strings.

If the element type implements fmt.Stringer it will be used. Otherwise it will fallback to the result of:

fmt.Sprintf("%v")

func Strings

func Strings[T constraints.Ordered](ss []T) []string

Strings transforms each element to a string.

If the element type implements fmt.Stringer it will be used. Otherwise it will fallback to the result of:

fmt.Sprintf("%v")

func StringsUsing

func StringsUsing[T any](ss []T, transform func(T) string) []string

StringsUsing transforms each element to a string.

func SubSlice

func SubSlice[T any](ss []T, start int, end int) (subSlice []T)

SubSlice will return the subSlice from start to end(excluded)

Condition 1: If start < 0 or end < 0, nil is returned. Condition 2: If start >= end, nil is returned. Condition 3: Return all elements that exist in the range provided, if start or end is out of bounds, zero items will be placed.

func Sum

func Sum[T constraints.Integer | constraints.Float](ss []T) (sum T)

Sum is the sum of all of the elements.

func Top

func Top[T any](ss []T, n int) (top []T)

Top will return n elements from head of the slice if the slice has less elements then n that'll return all elements if n < 0 it'll return empty slice.

func Unique

func Unique[T comparable](ss []T) []T

Unique returns a new slice with all of the unique values.

The items will be returned in a randomized order, even with the same input.

The number of items returned may be the same as the input or less. It will never return zero items unless then input slice has zero items.

A slice with zero elements is considered to be unique.

See AreUnique().

func Unshift

func Unshift[T any](ss []T, elements ...T) (unshift []T)

Unshift adds one or more elements to the beginning of the slice and returns the new slice.

func Values

func Values[K comparable, V any](m map[K]V) []V

Values returns the values in the map.

Due to Go's randomization of iterating maps the order is not deterministic.

Types

type OfNumericSlice

type OfNumericSlice[T constraints.Integer | constraints.Float] struct {
	Result []T
}

OfNumericSlice provides the proxy methods that operate on slices. If the last method in the chain does not return a single value, you can access the Result to get final slice.

func OfNumeric

func OfNumeric[T constraints.Integer | constraints.Float](ss []T) OfNumericSlice[T]

OfNumeric encapsulates a slice to be used in multiple chained operations. OfNumeric requires that elements be numerical for certain operations to be performed.

func (OfNumericSlice[T]) All

func (o OfNumericSlice[T]) All(fn func(value T) bool) bool

func (OfNumericSlice[T]) Any

func (o OfNumericSlice[T]) Any(fn func(value T) bool) bool

func (OfNumericSlice[T]) AreSorted

func (o OfNumericSlice[T]) AreSorted() bool

func (OfNumericSlice[T]) AreUnique

func (o OfNumericSlice[T]) AreUnique() bool

func (OfNumericSlice[T]) Average

func (o OfNumericSlice[T]) Average() float64

func (OfNumericSlice[T]) Bottom

func (o OfNumericSlice[T]) Bottom(n int) OfNumericSlice[T]

func (OfNumericSlice[T]) Contains

func (o OfNumericSlice[T]) Contains(lookingFor T) bool

func (OfNumericSlice[T]) Delete added in v2.7.0

func (o OfNumericSlice[T]) Delete(idx ...int) OfNumericSlice[T]

func (OfNumericSlice[T]) Diff

func (o OfNumericSlice[T]) Diff(against []T) ([]T, []T)

func (OfNumericSlice[T]) DropTop

func (o OfNumericSlice[T]) DropTop(n int) OfNumericSlice[T]

func (OfNumericSlice[T]) DropWhile

func (o OfNumericSlice[T]) DropWhile(f func(s T) bool) OfNumericSlice[T]

func (OfNumericSlice[T]) Each

func (o OfNumericSlice[T]) Each(fn func(T)) OfNumericSlice[T]

func (OfNumericSlice[T]) Equals

func (o OfNumericSlice[T]) Equals(rhs []T) bool

func (OfNumericSlice[T]) Filter

func (o OfNumericSlice[T]) Filter(condition func(T) bool) OfNumericSlice[T]

func (OfNumericSlice[T]) FilterNot

func (o OfNumericSlice[T]) FilterNot(condition func(T) bool) OfNumericSlice[T]

func (OfNumericSlice[T]) FindFirstUsing

func (o OfNumericSlice[T]) FindFirstUsing(fn func(value T) bool) int

func (OfNumericSlice[T]) First added in v2.2.0

func (o OfNumericSlice[T]) First() T

func (OfNumericSlice[T]) FirstOr

func (o OfNumericSlice[T]) FirstOr(defaultValue T) T

func (OfNumericSlice[T]) Float64s

func (o OfNumericSlice[T]) Float64s() []float64

func (OfNumericSlice[T]) Group

func (o OfNumericSlice[T]) Group() map[T]int

func (OfNumericSlice[T]) Insert

func (o OfNumericSlice[T]) Insert(index int, values ...T) OfNumericSlice[T]

func (OfNumericSlice[T]) Intersect

func (o OfNumericSlice[T]) Intersect(slices ...[]T) OfNumericSlice[T]

func (OfNumericSlice[T]) Ints

func (o OfNumericSlice[T]) Ints() []int

func (OfNumericSlice[T]) JSONBytes

func (o OfNumericSlice[T]) JSONBytes() []byte

func (OfNumericSlice[T]) JSONBytesIndent

func (o OfNumericSlice[T]) JSONBytesIndent(prefix, indent string) []byte

func (OfNumericSlice[T]) JSONString

func (o OfNumericSlice[T]) JSONString() string

func (OfNumericSlice[T]) JSONStringIndent

func (o OfNumericSlice[T]) JSONStringIndent(prefix, indent string) string

func (OfNumericSlice[T]) Join

func (o OfNumericSlice[T]) Join(glue string) string

func (OfNumericSlice[T]) Last added in v2.2.0

func (o OfNumericSlice[T]) Last() T

func (OfNumericSlice[T]) LastOr

func (o OfNumericSlice[T]) LastOr(defaultValue T) T

func (OfNumericSlice[T]) Map

func (o OfNumericSlice[T]) Map(fn func(T) T) OfNumericSlice[T]

func (OfNumericSlice[T]) Max

func (o OfNumericSlice[T]) Max() T

func (OfNumericSlice[T]) Median

func (o OfNumericSlice[T]) Median() T

func (OfNumericSlice[T]) Min

func (o OfNumericSlice[T]) Min() T

func (OfNumericSlice[T]) Mode

func (o OfNumericSlice[T]) Mode() OfNumericSlice[T]

func (OfNumericSlice[T]) Product

func (o OfNumericSlice[T]) Product() T

func (OfNumericSlice[T]) Random

func (o OfNumericSlice[T]) Random(source rand.Source) T

func (OfNumericSlice[T]) Reduce

func (o OfNumericSlice[T]) Reduce(reducer func(T, T) T) T

func (OfNumericSlice[T]) Reverse

func (o OfNumericSlice[T]) Reverse() OfNumericSlice[T]

func (OfNumericSlice[T]) Send

func (o OfNumericSlice[T]) Send(ctx context.Context, ch chan<- T) OfNumericSlice[T]

func (OfNumericSlice[T]) Sequence

func (o OfNumericSlice[T]) Sequence(params ...int) OfNumericSlice[T]

func (OfNumericSlice[T]) SequenceUsing

func (o OfNumericSlice[T]) SequenceUsing(creator func(int) T, params ...int) OfNumericSlice[T]

func (OfNumericSlice[T]) Shuffle

func (o OfNumericSlice[T]) Shuffle(source rand.Source) OfNumericSlice[T]

func (OfNumericSlice[T]) Sort

func (o OfNumericSlice[T]) Sort() OfNumericSlice[T]

func (OfNumericSlice[T]) SortStableUsing

func (o OfNumericSlice[T]) SortStableUsing(less func(a, b T) bool) OfNumericSlice[T]

func (OfNumericSlice[T]) SortUsing

func (o OfNumericSlice[T]) SortUsing(less func(a, b T) bool) OfNumericSlice[T]

func (OfNumericSlice[T]) Stddev

func (o OfNumericSlice[T]) Stddev() float64

func (OfNumericSlice[T]) Strings

func (o OfNumericSlice[T]) Strings() []string

func (OfNumericSlice[T]) StringsUsing

func (o OfNumericSlice[T]) StringsUsing(transform func(T) string) []string

func (OfNumericSlice[T]) SubSlice

func (o OfNumericSlice[T]) SubSlice(start int, end int) OfNumericSlice[T]

func (OfNumericSlice[T]) Sum

func (o OfNumericSlice[T]) Sum() T

func (OfNumericSlice[T]) Top

func (o OfNumericSlice[T]) Top(n int) OfNumericSlice[T]

func (OfNumericSlice[T]) Unique

func (o OfNumericSlice[T]) Unique() OfNumericSlice[T]

func (OfNumericSlice[T]) Unshift

func (o OfNumericSlice[T]) Unshift(elements ...T) OfNumericSlice[T]

type OfOrderedSlice

type OfOrderedSlice[T constraints.Ordered] struct {
	Result []T
}

OfOrderedSlice provides the proxy methods that operate on slices. If the last method in the chain does not return a single value, you can access the Result to get final slice.

func OfOrdered

func OfOrdered[T constraints.Ordered](ss []T) OfOrderedSlice[T]

OfOrdered encapsulates a slice to be used in multiple chained operations. OfOrdered requires that elements be numerical or a string for certain operations to be performed.

func (OfOrderedSlice[T]) All

func (o OfOrderedSlice[T]) All(fn func(value T) bool) bool

func (OfOrderedSlice[T]) Any

func (o OfOrderedSlice[T]) Any(fn func(value T) bool) bool

func (OfOrderedSlice[T]) AreSorted

func (o OfOrderedSlice[T]) AreSorted() bool

func (OfOrderedSlice[T]) AreUnique

func (o OfOrderedSlice[T]) AreUnique() bool

func (OfOrderedSlice[T]) Bottom

func (o OfOrderedSlice[T]) Bottom(n int) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Contains

func (o OfOrderedSlice[T]) Contains(lookingFor T) bool

func (OfOrderedSlice[T]) Delete added in v2.7.0

func (o OfOrderedSlice[T]) Delete(idx ...int) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Diff

func (o OfOrderedSlice[T]) Diff(against []T) ([]T, []T)

func (OfOrderedSlice[T]) DropTop

func (o OfOrderedSlice[T]) DropTop(n int) OfOrderedSlice[T]

func (OfOrderedSlice[T]) DropWhile

func (o OfOrderedSlice[T]) DropWhile(f func(s T) bool) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Each

func (o OfOrderedSlice[T]) Each(fn func(T)) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Equals

func (o OfOrderedSlice[T]) Equals(rhs []T) bool

func (OfOrderedSlice[T]) Filter

func (o OfOrderedSlice[T]) Filter(condition func(T) bool) OfOrderedSlice[T]

func (OfOrderedSlice[T]) FilterNot

func (o OfOrderedSlice[T]) FilterNot(condition func(T) bool) OfOrderedSlice[T]

func (OfOrderedSlice[T]) FindFirstUsing

func (o OfOrderedSlice[T]) FindFirstUsing(fn func(value T) bool) int

func (OfOrderedSlice[T]) First added in v2.2.0

func (o OfOrderedSlice[T]) First() T

func (OfOrderedSlice[T]) FirstOr

func (o OfOrderedSlice[T]) FirstOr(defaultValue T) T

func (OfOrderedSlice[T]) Float64s

func (o OfOrderedSlice[T]) Float64s() []float64

func (OfOrderedSlice[T]) Group

func (o OfOrderedSlice[T]) Group() map[T]int

func (OfOrderedSlice[T]) Insert

func (o OfOrderedSlice[T]) Insert(index int, values ...T) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Intersect

func (o OfOrderedSlice[T]) Intersect(slices ...[]T) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Ints

func (o OfOrderedSlice[T]) Ints() []int

func (OfOrderedSlice[T]) JSONBytes

func (o OfOrderedSlice[T]) JSONBytes() []byte

func (OfOrderedSlice[T]) JSONBytesIndent

func (o OfOrderedSlice[T]) JSONBytesIndent(prefix, indent string) []byte

func (OfOrderedSlice[T]) JSONString

func (o OfOrderedSlice[T]) JSONString() string

func (OfOrderedSlice[T]) JSONStringIndent

func (o OfOrderedSlice[T]) JSONStringIndent(prefix, indent string) string

func (OfOrderedSlice[T]) Join

func (o OfOrderedSlice[T]) Join(glue string) string

func (OfOrderedSlice[T]) Last added in v2.2.0

func (o OfOrderedSlice[T]) Last() T

func (OfOrderedSlice[T]) LastOr

func (o OfOrderedSlice[T]) LastOr(defaultValue T) T

func (OfOrderedSlice[T]) Map

func (o OfOrderedSlice[T]) Map(fn func(T) T) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Max

func (o OfOrderedSlice[T]) Max() T

func (OfOrderedSlice[T]) Min

func (o OfOrderedSlice[T]) Min() T

func (OfOrderedSlice[T]) Mode

func (o OfOrderedSlice[T]) Mode() OfOrderedSlice[T]

func (OfOrderedSlice[T]) Reverse

func (o OfOrderedSlice[T]) Reverse() OfOrderedSlice[T]

func (OfOrderedSlice[T]) Send

func (o OfOrderedSlice[T]) Send(ctx context.Context, ch chan<- T) OfOrderedSlice[T]

func (OfOrderedSlice[T]) SequenceUsing

func (o OfOrderedSlice[T]) SequenceUsing(creator func(int) T, params ...int) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Shuffle

func (o OfOrderedSlice[T]) Shuffle(source rand.Source) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Sort

func (o OfOrderedSlice[T]) Sort() OfOrderedSlice[T]

func (OfOrderedSlice[T]) SortStableUsing

func (o OfOrderedSlice[T]) SortStableUsing(less func(a, b T) bool) OfOrderedSlice[T]

func (OfOrderedSlice[T]) SortUsing

func (o OfOrderedSlice[T]) SortUsing(less func(a, b T) bool) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Strings

func (o OfOrderedSlice[T]) Strings() []string

func (OfOrderedSlice[T]) StringsUsing

func (o OfOrderedSlice[T]) StringsUsing(transform func(T) string) []string

func (OfOrderedSlice[T]) SubSlice

func (o OfOrderedSlice[T]) SubSlice(start int, end int) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Top

func (o OfOrderedSlice[T]) Top(n int) OfOrderedSlice[T]

func (OfOrderedSlice[T]) Unique

func (o OfOrderedSlice[T]) Unique() OfOrderedSlice[T]

func (OfOrderedSlice[T]) Unshift

func (o OfOrderedSlice[T]) Unshift(elements ...T) OfOrderedSlice[T]

type OfSlice

type OfSlice[T any] struct {
	Result []T
}

OfSlice provides the proxy methods that operate on slices. If the last method in the chain does not return a single value, you can access the Result to get final slice.

func Of

func Of[T any](ss []T) OfSlice[T]

Of encapsulates a slice to be used in multiple chained operations.

func (OfSlice[T]) All

func (o OfSlice[T]) All(fn func(value T) bool) bool

All will return true if all callbacks return true. It follows the same logic as the all() function in Python.

If the list is empty then true is always returned.

func (OfSlice[T]) Any

func (o OfSlice[T]) Any(fn func(value T) bool) bool

Any will return true if any callbacks return true. It follows the same logic as the any() function in Python.

If the list is empty then false is always returned.

func (OfSlice[T]) Bottom

func (o OfSlice[T]) Bottom(n int) OfSlice[T]

Bottom will return n elements from bottom

that means that elements is taken from the end of the slice for this [1,2,3] slice with n == 2 will be returned [3,2] if the slice has less elements then n that'll return all elements if n < 0 it'll return empty slice.

func (OfSlice[T]) Delete added in v2.7.0

func (o OfSlice[T]) Delete(idx ...int) OfSlice[T]

Removes element at index in idx from input slice, returns resulting slice. If an index in idx out of bounds, skip it.

func (OfSlice[T]) DropTop

func (o OfSlice[T]) DropTop(n int) OfSlice[T]

DropTop will return the rest slice after dropping the top n elements if the slice has less elements then n that'll return empty slice if n < 0 it'll return empty slice.

func (OfSlice[T]) Each

func (o OfSlice[T]) Each(fn func(T)) OfSlice[T]

Each is more condensed version of Transform that allows an action to happen on each elements and pass the original slice on.

pie.Each(cars, func (car *Car) {
    fmt.Printf("Car color is: %s\n", car.Color)
})

Pie will not ensure immutability on items passed in so they can be manipulated, if you choose to do it this way, for example:

// Set all car colors to Red.
pie.Each(cars, func (car *Car) {
    car.Color = "Red"
})

func (OfSlice[T]) Filter

func (o OfSlice[T]) Filter(condition func(T) bool) OfSlice[T]

Filter will return a new slice containing only the elements that return true from the condition. The returned slice may contain zero elements (nil).

FilterNot works in the opposite way of Filter.

func (OfSlice[T]) FilterNot

func (o OfSlice[T]) FilterNot(condition func(T) bool) OfSlice[T]

FilterNot works the same as Filter, with a negated condition. That is, it will return a new slice only containing the elements that returned false from the condition. The returned slice may contain zero elements (nil).

func (OfSlice[T]) FindFirstUsing

func (o OfSlice[T]) FindFirstUsing(fn func(value T) bool) int

FindFirstUsing will return the index of the first element when the callback returns true or -1 if no element is found. It follows the same logic as the findIndex() function in Javascript.

If the list is empty then -1 is always returned.

func (OfSlice[T]) First added in v2.2.0

func (o OfSlice[T]) First() T

First returns the first element or a zero value if there are no elements.

func (OfSlice[T]) FirstOr

func (o OfSlice[T]) FirstOr(defaultValue T) T

FirstOr returns the first element or a default value if there are no elements.

func (OfSlice[T]) Insert

func (o OfSlice[T]) Insert(index int, values ...T) OfSlice[T]

Insert a value at an index.

func (OfSlice[T]) Last added in v2.2.0

func (o OfSlice[T]) Last() T

Last returns the last element or a zero value if there are no elements.

func (OfSlice[T]) LastOr

func (o OfSlice[T]) LastOr(defaultValue T) T

LastOr returns the last element or a default value if there are no elements.

func (OfSlice[T]) Map

func (o OfSlice[T]) Map(fn func(T) T) OfSlice[T]

Map will return a new slice where each element has been mapped (transformed). The number of elements returned will always be the same as the input.

Be careful when using this with slices of pointers. If you modify the input value it will affect the original slice. Be sure to return a new allocated object or deep copy the existing one.

func (OfSlice[T]) Reverse

func (o OfSlice[T]) Reverse() OfSlice[T]

Reverse returns a new copy of the slice with the elements ordered in reverse. This is useful when combined with Sort to get a descending sort order:

ss.Sort().Reverse()

func (OfSlice[T]) Rotate added in v2.8.0

func (o OfSlice[T]) Rotate(n int) OfSlice[T]

Rotate returns slice circularly rotated by a number of positions n. If n is positive, the slice is rotated right. If n is negative, the slice is rotated left.

func (OfSlice[T]) Send

func (o OfSlice[T]) Send(ctx context.Context, ch chan<- T) OfSlice[T]

Send sends elements to channel in normal act it sends all elements but if func canceled it can be less

it locks execution of gorutine it doesn't close channel after work returns sent elements if len(this) != len(old) considered func was canceled

func (OfSlice[T]) SequenceUsing

func (o OfSlice[T]) SequenceUsing(creator func(int) T, params ...int) OfSlice[T]

SequenceUsing generates slice in range using creator function

There are 3 variations to generate:

  1. [0, n).
  2. [min, max).
  3. [min, max) with step.

if len(params) == 1 considered that will be returned slice between 0 and n, where n is the first param, [0, n). if len(params) == 2 considered that will be returned slice between min and max, where min is the first param, max is the second, [min, max). if len(params) > 2 considered that will be returned slice between min and max with step, where min is the first param, max is the second, step is the third one, [min, max) with step, others params will be ignored

func (OfSlice[T]) Shuffle

func (o OfSlice[T]) Shuffle(source rand.Source) OfSlice[T]

Shuffle returns a new shuffled slice by your rand.Source. The original slice is not modified.

func (OfSlice[T]) SortUsing

func (o OfSlice[T]) SortUsing(less func(a, b T) bool) OfSlice[T]

SortUsing works similar to sort.Slice. However, unlike sort.Slice the slice returned will be reallocated as to not modify the input slice.

func (OfSlice[T]) StringsUsing

func (o OfSlice[T]) StringsUsing(transform func(T) string) []string

StringsUsing transforms each element to a string.

func (OfSlice[T]) SubSlice

func (o OfSlice[T]) SubSlice(start int, end int) OfSlice[T]

SubSlice will return the subSlice from start to end(excluded)

Condition 1: If start < 0 or end < 0, nil is returned. Condition 2: If start >= end, nil is returned. Condition 3: Return all elements that exist in the range provided, if start or end is out of bounds, zero items will be placed.

func (OfSlice[T]) Top

func (o OfSlice[T]) Top(n int) OfSlice[T]

Top will return n elements from head of the slice if the slice has less elements then n that'll return all elements if n < 0 it'll return empty slice.

func (OfSlice[T]) Unshift

func (o OfSlice[T]) Unshift(elements ...T) OfSlice[T]

Unshift adds one or more elements to the beginning of the slice and returns the new slice.

type Zipped added in v2.6.0

type Zipped[T1, T2 any] struct {
	A T1
	B T2
}

A pair struct containing two zipped values.

func Zip added in v2.6.0

func Zip[T1, T2 any](ss1 []T1, ss2 []T2) []Zipped[T1, T2]

Zip will return a new slice containing pairs with elements from input slices. If input slices have diffrent length, the output slice will be truncated to the length of the smallest input slice.

func ZipLongest added in v2.6.0

func ZipLongest[T1, T2 any](ss1 []T1, ss2 []T2) []Zipped[T1, T2]

ZipLongest will return a new slice containing pairs with elements from input slices. If input slices have diffrent length, missing elements will be padded with default values.

Jump to

Keyboard shortcuts

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