go_func

package module
v0.0.0-...-b7c96d7 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2023 License: MIT Imports: 7 Imported by: 1

README ΒΆ

πŸ• github.com/elliotchance/pie

GoDoc Build Status codecov

Enjoy a slice! pie is a library of utility functions for common operations on slices and maps.

Quick Start

If you are using (or require) Go 1.17 or below, you will have to use v1.

pie can be used in two ways, the first is to use the regular parameterized functions:

Run this program

package main

import (
    "fmt"
    "strings"

    
)

func main() {
    names := FilterNot([]string{"Bob", "Sally", "John", "Jane"},
        func(name string) bool {
            return strings.HasPrefix(name, "J")
        })

    fmt.Println(names) // "[Bob Sally]"
}

Or, if you need to chain multiple operations you can use one of:

  • Of - works with any element type, but functions are limited.
  • OfOrdered - only works with numbers and strings, but has more functions.
  • OfNumeric - only works with numbers, but has all functions.

Run this program

package main

import (
    "fmt"
    "strings"

    
)

func main() {
    name := Of([]string{"Bob", "Sally", "John", "Jane"}).
        FilterNot(func(name string) bool {
            return strings.HasPrefix(name, "J")
        }).
        Map(strings.ToUpper).
        Last()

    fmt.Println(name) // "SALLY"
}

You can find the full documentation here.

FAQ

What are the requirements?

pie v2 only supports Go 1.18+. If you have an older version you can use v1.

What are the goals of pie?

  1. Type safety. I never want to hit runtime bugs because I could pass in the wrong type, or perform an invalid type case out the other end.

  2. Performance. The functions need to be as fast as native Go implementations otherwise there's no point in this library existing.

  3. Nil-safe. All of the functions will happily accept nil and treat them as empty slices. Apart from less possible panics, it makes it easier to chain.

  4. Immutable. Functions never modify inputs (except in cases where it would be illogical), unlike some built-ins such as sort.Strings.

How do I contribute a function?

Pull requests are always welcome.

Here is a comprehensive list of steps to follow to add a new function:

  1. Create a new file for your function (tip: copy an existing file can be quicker). Add your implmentation and comment.

  2. Create appropriate tests.

  3. If your function accepts a slice, it should also be added to the OfSlice API (see of.go).

Why is the emoji a slice of pizza instead of a pie?

I wanted to pick a name for the project that was short and had an associated emoji. I liked pie, but then I found out that the pie emoji is not fully supported everywhere. I didn't want to change the name of the project to cake, but pizza pie still made sense. I'm not sure if I will change it back to a pie later.

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

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 Compose10 ΒΆ

func Compose10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any](fn10 func(T10) R, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 10 functions

func Compose11 ΒΆ

func Compose11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R any](fn11 func(T11) R, fn10 func(T10) T11, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 11 functions

func Compose12 ΒΆ

func Compose12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R any](fn12 func(T12) R, fn11 func(T11) T12, fn10 func(T10) T11, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 12 functions

func Compose13 ΒΆ

func Compose13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R any](fn13 func(T13) R, fn12 func(T12) T13, fn11 func(T11) T12, fn10 func(T10) T11, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 13 functions

func Compose14 ΒΆ

func Compose14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R any](fn14 func(T14) R, fn13 func(T13) T14, fn12 func(T12) T13, fn11 func(T11) T12, fn10 func(T10) T11, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 14 functions

func Compose15 ΒΆ

func Compose15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R any](fn15 func(T15) R, fn14 func(T14) T15, fn13 func(T13) T14, fn12 func(T12) T13, fn11 func(T11) T12, fn10 func(T10) T11, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 15 functions

func Compose16 ΒΆ

func Compose16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R any](fn16 func(T16) R, fn15 func(T15) T16, fn14 func(T14) T15, fn13 func(T13) T14, fn12 func(T12) T13, fn11 func(T11) T12, fn10 func(T10) T11, fn9 func(T9) T10, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 16 functions

func Compose2 ΒΆ

func Compose2[T1, T2, R any](fn2 func(T2) R, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of two functions

func Compose3 ΒΆ

func Compose3[T1, T2, T3, R any](fn3 func(T3) R, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of three functions

func Compose4 ΒΆ

func Compose4[T1, T2, T3, T4, R any](fn4 func(T4) R, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of four functions

func Compose5 ΒΆ

func Compose5[T1, T2, T3, T4, T5, R any](fn5 func(T5) R, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 5 functions

func Compose6 ΒΆ

func Compose6[T1, T2, T3, T4, T5, T6, R any](fn6 func(T6) R, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 6 functions

func Compose7 ΒΆ

func Compose7[T1, T2, T3, T4, T5, T6, T7, R any](fn7 func(T7) R, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 7 functions

func Compose8 ΒΆ

func Compose8[T1, T2, T3, T4, T5, T6, T7, T8, R any](fn8 func(T8) R, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 8 functions

func Compose9 ΒΆ

func Compose9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](fn9 func(T9) R, fn8 func(T8) T9, fn7 func(T7) T8, fn6 func(T6) T7, fn5 func(T5) T6, fn4 func(T4) T5, fn3 func(T3) T4, fn2 func(T2) T3, fn1 func(T1) T2) func(T1) R

Performs right-to-left function composition of 9 functions

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 Curry10 ΒΆ

func Curry10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) R

Allow to transform a function that receives 10 params in a sequence of unary functions

func Curry11 ΒΆ

func Curry11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) func(T11) R

Allow to transform a function that receives 11 params in a sequence of unary functions

func Curry12 ΒΆ

func Curry12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) func(T11) func(T12) R

Allow to transform a function that receives 12 params in a sequence of unary functions

func Curry13 ΒΆ

func Curry13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) func(T11) func(T12) func(T13) R

Allow to transform a function that receives 13 params in a sequence of unary functions

func Curry14 ΒΆ

func Curry14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) func(T11) func(T12) func(T13) func(T14) R

Allow to transform a function that receives 14 params in a sequence of unary functions

func Curry15 ΒΆ

func Curry15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) func(T11) func(T12) func(T13) func(T14) func(T15) R

Allow to transform a function that receives 15 params in a sequence of unary functions

func Curry16 ΒΆ

func Curry16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) func(T10) func(T11) func(T12) func(T13) func(T14) func(T15) func(T16) R

Allow to transform a function that receives 16 params in a sequence of unary functions

func Curry2 ΒΆ

func Curry2[T1, T2, R any](fn func(T1, T2) R) func(T1) func(T2) R

Allow to transform a function that receives 2 params in a sequence of unary functions

func Curry3 ΒΆ

func Curry3[T1, T2, T3, R any](fn func(T1, T2, T3) R) func(T1) func(T2) func(T3) R

Allow to transform a function that receives 3 params in a sequence of unary functions

func Curry4 ΒΆ

func Curry4[T1, T2, T3, T4, R any](fn func(T1, T2, T3, T4) R) func(T1) func(T2) func(T3) func(T4) R

Allow to transform a function that receives 4 params in a sequence of unary functions

func Curry5 ΒΆ

func Curry5[T1, T2, T3, T4, T5, R any](fn func(T1, T2, T3, T4, T5) R) func(T1) func(T2) func(T3) func(T4) func(T5) R

Allow to transform a function that receives 5 params in a sequence of unary functions

func Curry6 ΒΆ

func Curry6[T1, T2, T3, T4, T5, T6, R any](fn func(T1, T2, T3, T4, T5, T6) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) R

Allow to transform a function that receives 6 params in a sequence of unary functions

func Curry7 ΒΆ

func Curry7[T1, T2, T3, T4, T5, T6, T7, R any](fn func(T1, T2, T3, T4, T5, T6, T7) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) R

Allow to transform a function that receives 7 params in a sequence of unary functions

func Curry8 ΒΆ

func Curry8[T1, T2, T3, T4, T5, T6, T7, T8, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) R

Allow to transform a function that receives 8 params in a sequence of unary functions

func Curry9 ΒΆ

func Curry9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](fn func(T1, T2, T3, T4, T5, T6, T7, T8, T9) R) func(T1) func(T2) func(T3) func(T4) func(T5) func(T6) func(T7) func(T8) func(T9) R

Allow to transform a function that receives 9 params in a sequence of unary functions

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.

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.
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 Find ΒΆ

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

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

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

func First ΒΆ

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

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 Group ΒΆ

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

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

func GroupBy ΒΆ

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:

_ = 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 IndexOf ΒΆ

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

IndexOf 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 Insert ΒΆ

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

Insert a value at an index.

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

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, int) 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) (min 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[IterType, AccType any](f func(acc AccType, el IterType, i int) AccType, arr []IterType, acc AccType) AccType

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 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 constraints.Integer | constraints.Float](ss []T) (T, []T)

Shift will return two values: the shifted value and the rest 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 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 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 OfSplitString ΒΆ

func OfSplitString(s string, sep string) OfSlice[string]

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]) 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.

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.
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]) Find ΒΆ

func (o OfSlice[T]) Find(fn func(T, int) bool) T

func (OfSlice[T]) First ΒΆ

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]) IndexOf ΒΆ

func (o OfSlice[T]) IndexOf(fn func(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]) Insert ΒΆ

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

Insert a value at an index.

func (OfSlice[T]) Join ΒΆ

func (o OfSlice[T]) Join(sep string) string

func (OfSlice[T]) Last ΒΆ

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, int) 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]) 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]) 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.

Jump to

Keyboard shortcuts

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