functions

package
v1.39.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ForNumbers = 1 << iota
	ForStrings
	ForStructs
	ForMaps

	ForAll               = ForNumbers | ForStrings | ForStructs
	ForNumbersAndStrings = ForNumbers | ForStrings
)

Variables

View Source
var Functions = []struct {
	Name string
	File string
	For  int
	BigO string
}{
	{"Abs", "abs.go", ForNumbers, "n"},
	{"All", "all.go", ForAll, "n"},
	{"Any", "any.go", ForAll, "n"},
	{"Append", "append.go", ForAll, "n"},
	{"AreSorted", "are_sorted.go", ForNumbersAndStrings, "n"},
	{"AreUnique", "are_unique.go", ForNumbersAndStrings, "n"},
	{"Average", "average.go", ForNumbers, "n"},
	{"Bottom", "bottom.go", ForAll, "n"},
	{"Contains", "contains.go", ForAll, "n"},
	{"Diff", "diff.go", ForAll, "n²"},
	{"DropTop", "drop_top.go", ForAll, "n"},
	{"DropWhile", "drop_while.go", ForAll, "n"},
	{"Each", "each.go", ForAll, "n"},
	{"Equals", "equals.go", ForAll, "n"},
	{"Extend", "extend.go", ForAll, "n"},
	{"Filter", "filter.go", ForAll, "n"},
	{"FilterNot", "filter_not.go", ForAll, "n"},
	{"FindFirstUsing", "find_first_using.go", ForAll, "n"},
	{"First", "first.go", ForAll, "1"},
	{"FirstOr", "first_or.go", ForAll, "1"},
	{"Float64s", "float64s.go", ForAll, "n"},
	{"Group", "group.go", ForNumbersAndStrings, "n"},
	{"Intersect", "intersect.go", ForNumbersAndStrings, "n"},
	{"Insert", "insert.go", ForAll, "n"},
	{"Ints", "ints.go", ForAll, "n"},
	{"Join", "join.go", ForAll, "n"},
	{"JSONBytes", "json_bytes.go", ForAll, "n"},
	{"JSONBytesIndent", "json_bytes_indent.go", ForAll, "n"},
	{"JSONString", "json_string.go", ForAll, "n"},
	{"JSONStringIndent", "json_string_indent.go", ForAll, "n"},
	{"Keys", "keys.go", ForMaps, "n"},
	{"Last", "last.go", ForAll, "1"},
	{"LastOr", "last_or.go", ForAll, "1"},
	{"Len", "len.go", ForAll, "1"},
	{"Map", "map.go", ForAll, "n"},
	{"Max", "max.go", ForNumbersAndStrings, "n"},
	{"Median", "median.go", ForNumbers, "n"},
	{"Min", "min.go", ForNumbersAndStrings, "n"},
	{"Mode", "mode.go", ForAll, "n"},
	{"Pop", "pop.go", ForAll, "n"},
	{"Product", "product.go", ForNumbers, "n"},
	{"Random", "random.go", ForAll, "1"},
	{"Reduce", "reduce.go", ForNumbersAndStrings, "n"},
	{"Reverse", "reverse.go", ForAll, "n"},
	{"Send", "send.go", ForAll, "n"},
	{"Sequence", "sequence.go", ForNumbers, "n"},
	{"SequenceUsing", "sequence_using.go", ForAll, "n"},
	{"Shift", "shift.go", ForAll, "n"},
	{"Shuffle", "shuffle.go", ForAll, "n"},
	{"Sort", "sort.go", ForNumbersAndStrings, "n⋅log(n)"},
	{"SortStableUsing", "sort_stable_using.go", ForStrings | ForStructs, "n⋅log(n)"},
	{"SortUsing", "sort_using.go", ForStrings | ForStructs, "n⋅log(n)"},
	{"Stddev", "stddev.go", ForNumbers, "n"},
	{"Strings", "strings.go", ForAll, "n"},
	{"SubSlice", "sub_slice.go", ForAll, "n"},
	{"Sum", "sum.go", ForNumbers, "n"},
	{"Top", "top.go", ForAll, "n"},
	{"StringsUsing", "strings_using.go", ForAll, "n"},
	{"Unique", "unique.go", ForNumbersAndStrings, "n"},
	{"Unshift", "unshift.go", ForAll, "n"},
	{"Values", "values.go", ForMaps, "n"},
}

Function is a list of functions and which types they are available to. It is a slice instead of a may to make sure we iterate in a predicable order so regenerating files is deterministic.

Functions

This section is empty.

Types

type ElementType

type ElementType float64
var ElementZeroValue ElementType

func (ElementType) Equals added in v1.21.0

func (a ElementType) Equals(b ElementType) bool

func (ElementType) String added in v1.22.0

func (a ElementType) String() string

type KeySliceType added in v1.5.0

type KeySliceType []KeyType

type KeyType added in v1.5.0

type KeyType string

type MapType added in v1.5.0

type MapType map[KeyType]ElementType

func (MapType) Keys added in v1.5.0

func (m MapType) Keys() KeySliceType

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 (MapType) Values added in v1.5.0

func (m MapType) Values() []ElementType

Values returns the values in the map.

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

type SliceType

type SliceType []ElementType

func (SliceType) Abs added in v1.11.0

func (ss SliceType) Abs() SliceType

Abs is a function which returns the absolute value of all the elements in the slice.

func (SliceType) All added in v1.4.0

func (ss SliceType) All(fn func(value ElementType) 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 (SliceType) Any added in v1.4.0

func (ss SliceType) Any(fn func(value ElementType) 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 (SliceType) Append added in v1.3.0

func (ss SliceType) Append(elements ...ElementType) SliceType

Append will return a new slice with the elements appended to the end.

It is acceptable to provide zero arguments.

func (SliceType) AreSorted

func (ss SliceType) AreSorted() bool

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

func (SliceType) AreUnique

func (ss SliceType) AreUnique() bool

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

func (SliceType) Average

func (ss SliceType) Average() float64

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

func (SliceType) Bottom added in v1.7.0

func (ss SliceType) Bottom(n int) (top SliceType)

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 (SliceType) Contains

func (ss SliceType) Contains(lookingFor ElementType) 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 (SliceType) Diff added in v1.19.0

func (ss SliceType) Diff(against SliceType) (added, removed SliceType)

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 (SliceType) DropTop added in v1.25.0

func (ss SliceType) DropTop(n int) (drop SliceType)

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 (SliceType) DropWhile added in v1.39.0

func (ss SliceType) DropWhile(f func(s ElementType) bool) (ss2 SliceType)

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 (SliceType) Each added in v1.10.0

func (ss SliceType) Each(fn func(ElementType)) SliceType

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

cars.Each(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.
cars.Each(func (car *Car) {
    car.Color = "Red"
})

func (SliceType) Equals added in v1.32.0

func (ss SliceType) Equals(rhs SliceType) 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 (SliceType) Extend added in v1.3.0

func (ss SliceType) Extend(slices ...SliceType) (ss2 SliceType)

Extend will return a new slice with the slices of elements appended to the end.

It is acceptable to provide zero arguments.

func (SliceType) Filter added in v1.14.0

func (ss SliceType) Filter(condition func(ElementType) bool) (ss2 SliceType)

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 (SliceType) FilterNot added in v1.14.0

func (ss SliceType) FilterNot(condition func(ElementType) bool) (ss2 SliceType)

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 (SliceType) FindFirstUsing added in v1.27.0

func (ss SliceType) FindFirstUsing(fn func(value ElementType) 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 (SliceType) First

func (ss SliceType) First() ElementType

First returns the first element, or zero. Also see FirstOr().

func (SliceType) FirstOr

func (ss SliceType) FirstOr(defaultValue ElementType) ElementType

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

func (SliceType) Float64s added in v1.23.0

func (ss SliceType) Float64s() pie.Float64s

Float64s transforms each element to a float64.

func (SliceType) Group added in v1.35.0

func (ss SliceType) Group() map[ElementType]int

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

func (SliceType) Insert added in v1.37.0

func (ss SliceType) Insert(index int, values ...ElementType) SliceType

Insert a value at an index

func (SliceType) Intersect added in v1.15.0

func (ss SliceType) Intersect(slices ...SliceType) (ss2 SliceType)

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 (SliceType) Ints added in v1.23.0

func (ss SliceType) Ints() pie.Ints

Ints transforms each element to an integer.

func (SliceType) JSONBytes added in v1.26.0

func (ss SliceType) JSONBytes() []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 (SliceType) JSONBytesIndent added in v1.30.0

func (ss SliceType) JSONBytesIndent(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 (SliceType) JSONString

func (ss SliceType) JSONString() 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 (SliceType) JSONStringIndent added in v1.30.0

func (ss SliceType) JSONStringIndent(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 (SliceType) Join added in v1.34.0

func (ss SliceType) Join(glue string) (s string)

Join returns a string from joining each of the elements.

func (SliceType) Last

func (ss SliceType) Last() ElementType

Last returns the last element, or zero. Also see LastOr().

func (SliceType) LastOr

func (ss SliceType) LastOr(defaultValue ElementType) ElementType

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

func (SliceType) Len

func (ss SliceType) Len() int

Len returns the number of elements.

func (SliceType) Map added in v1.14.0

func (ss SliceType) Map(fn func(ElementType) ElementType) (ss2 SliceType)

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 (SliceType) Max

func (ss SliceType) Max() (max ElementType)

Max is the maximum value, or zero.

func (SliceType) Median added in v1.9.0

func (ss SliceType) Median() ElementType

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 (SliceType) Min

func (ss SliceType) Min() (min ElementType)

Min is the minimum value, or zero.

func (SliceType) Mode added in v1.29.0

func (ss SliceType) Mode() SliceType

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 (*SliceType) Pop added in v1.36.0

func (ss *SliceType) Pop() (popped *ElementType)

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 (SliceType) Product added in v1.16.0

func (ss SliceType) Product() (product ElementType)

Product is the product of all of the elements.

func (SliceType) Random added in v1.12.0

func (ss SliceType) Random(source rand.Source) ElementType

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

func (SliceType) Reduce added in v1.17.0

func (ss SliceType) Reduce(reducer func(ElementType, ElementType) ElementType) (el ElementType)

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

Returns a zero value of ElementType 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 (SliceType) Reverse

func (ss SliceType) Reverse() SliceType

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 (SliceType) Send added in v1.13.0

func (ss SliceType) Send(ctx context.Context, ch chan<- ElementType) SliceType

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 sended elements if len(this) != len(old) considered func was canceled

func (SliceType) Sequence added in v1.20.0

func (ss SliceType) Sequence(params ...int) SliceType

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 (SliceType) SequenceUsing added in v1.24.0

func (ss SliceType) SequenceUsing(creator func(int) ElementType, params ...int) SliceType

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 (SliceType) Shift added in v1.33.0

func (ss SliceType) Shift() (ElementType, SliceType)

Shift will return two values: the shifted value and the rest slice.

func (SliceType) Shuffle added in v1.6.0

func (ss SliceType) Shuffle(source rand.Source) SliceType

Shuffle returns shuffled slice by your rand.Source

func (SliceType) Sort

func (ss SliceType) Sort() SliceType

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 (SliceType) SortStableUsing added in v1.18.0

func (ss SliceType) SortStableUsing(less func(a, b ElementType) bool) SliceType

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

func (SliceType) SortUsing added in v1.18.0

func (ss SliceType) SortUsing(less func(a, b ElementType) bool) SliceType

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

func (SliceType) Stddev added in v1.38.0

func (ss SliceType) Stddev() float64

Stddev is the standard deviation

func (SliceType) Strings added in v1.22.0

func (ss SliceType) Strings() pie.Strings

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 (SliceType) StringsUsing added in v1.31.0

func (ss SliceType) StringsUsing(transform func(ElementType) string) pie.Strings

StringsUsing transforms each element to a string.

func (SliceType) SubSlice added in v1.28.0

func (ss SliceType) SubSlice(start int, end int) (subSlice SliceType)

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 (SliceType) Sum

func (ss SliceType) Sum() (sum ElementType)

Sum is the sum of all of the elements.

func (SliceType) Top added in v1.7.0

func (ss SliceType) Top(n int) (top SliceType)

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 (SliceType) Unique

func (ss SliceType) Unique() SliceType

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 (SliceType) Unshift added in v1.33.0

func (ss SliceType) Unshift(elements ...ElementType) (unshift SliceType)

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

type StringElementType

type StringElementType string

type StringSliceType

type StringSliceType []StringElementType

Jump to

Keyboard shortcuts

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