Documentation ¶
Index ¶
- func All[T any](values []T, predicate func(T) bool) bool
- func Any[T any](values []T, predicate func(T) bool) bool
- func Contains[T comparable](values []T, value T) bool
- func Count[T any](slice []T, predicate func(T) bool) int
- func Difference[T comparable](slice, other []T) []T
- func Drop[T any](values []T, index int) (rest []T)
- func Each[T any](values []T, action func(T)) []T
- func Filter[T any](values []T, predicate func(T) bool) (res []T)
- func Find[T any](values []T, predicate func(T) bool) (res T, err error)
- func Flatmap[T any](values []T, mapper func(n T) []T) []T
- func GroupBy[K comparable, V any](values []V, f func(V) K) map[K][]V
- func Intersection[T comparable](a, b []T) (res []T)
- func JoinProject[L, R, O any, S comparable](left []L, right []R, leftSelector func(L) S, rightSelector func(R) S, ...) (results []O)
- func Last[T any](values []T) T
- func Map[T, P any](values []T, transform func(T) P) []P
- func Max[T constraints.Ordered](values []T) T
- func Min[T constraints.Ordered](values []T) T
- func OrderBy[T any](list []T, predicate func(T, T) bool) []T
- func Partition[T any](values []T, predicate func(T) bool) ([]T, []T)
- func Range(start int, end int) (result []int)
- func Reduce[T, P any](values []T, reduction func(T, P) P, acc P) P
- func SortSliceASC[T constraints.Ordered](s []T)
- func SortSliceDESC[T constraints.Ordered](s []T)
- func Sum[T constraints.Ordered](values []T) (sum T)
- func SumMap[T any, R constraints.Ordered](list []T, selector func(T) R) (sum R)
- func Ternary[T any](condition bool, pos, neg T) T
- func ToPointer[T any](in T) *T
- func Unique[T comparable](values []T) (uniques []T)
- type Err
- type Ok
- type Pipe
- func (c Pipe[T]) All(predicate func(T) bool) bool
- func (c Pipe[T]) Any(predicate func(T) bool) bool
- func (c Pipe[T]) Contains(value T) bool
- func (c Pipe[T]) Each(action func(T))
- func (c Pipe[T]) Filter(predicate func(n T) bool) Pipe[T]
- func (c Pipe[T]) Find(predicate func(n T) bool) (T, error)
- func (c Pipe[T]) Map(transform func(n T) T) Pipe[T]
- func (c Pipe[T]) Max() T
- func (c Pipe[T]) Min() T
- func (c Pipe[T]) Partition(predicate func(T) bool) ([]T, []T)
- func (c Pipe[T]) Reduce(reducer func(n, acc T) T, acc T) T
- type Result
- type Tuple
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All returns true if all the values in the slice pass the predicate truth test. Short-circuits and stops traversing the slice if a false element is found.
func Any ¶
Any returns true if any of the values in the slice pass the predicate truth test. Short-circuits and stops traversing the slice if a true element is found.
func Contains ¶
func Contains[T comparable](values []T, value T) bool
Contains returns true if the value is present in the slice
func Count ¶ added in v0.6.0
Count returns the number of elements in the slice that satisfy the predicate. example: Count([]int{1,2,3,4,5}, func(n int) bool { return n%2 == 0 }) // 2
func Difference ¶
func Difference[T comparable](slice, other []T) []T
Difference Returns a copy of the array with all instances of the values that are not present in the other array.
func Drop ¶
Drop returns the rest of the elements in a slice. Pass an index to return the values of the slice from that index onward.
func Each ¶
func Each[T any](values []T, action func(T)) []T
Each iterates over a slice of elements, yielding each in turn to an action function. Returns the slice for piping.
func Filter ¶
Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).
func Find ¶
Find looks through each value in the slice, returning the first one that passes a truth test (predicate), or the default value for the type and an error if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire slice.
func Flatmap ¶ added in v0.5.0
func Flatmap[T any](values []T, mapper func(n T) []T) []T
Flatmap flatten the input slice element into the new slice. FlatMap maps every element with the help of a mapper function, then flattens the input slice element into the new slice.
func GroupBy ¶
func GroupBy[K comparable, V any](values []V, f func(V) K) map[K][]V
GroupBy splits a slice into a map[K][]V grouped by the result of the iterator function.
func Intersection ¶
func Intersection[T comparable](a, b []T) (res []T)
Intersection computes the list of values that are the intersection of all the slices. Each value in the result is present in each of the slices.
func JoinProject ¶ added in v0.5.0
func JoinProject[L, R, O any, S comparable]( left []L, right []R, leftSelector func(L) S, rightSelector func(R) S, projection func(Tuple[L, []R]) O) (results []O)
Joins two slices together and returns a []O where O is defined by the output of your projection function The selectors allow you to pick the keys from your structure to use as the join keys While the projection functions allows you to reformat joined datasets (Tuple of [T, []P]) into your own struct or type
func Map ¶
func Map[T, P any](values []T, transform func(T) P) []P
Map produces a new slice of values by mapping each value in the slice through a transform function.
func Max ¶
func Max[T constraints.Ordered](values []T) T
Max returns the maximum value in the slice. This function can currently only compare numbers reliably. This function uses operator <.
func Min ¶
func Min[T constraints.Ordered](values []T) T
Min returns the minimum value in the slice. This function can currently only compare numbers reliably. This function uses operator <.
func OrderBy ¶ added in v0.5.0
Orders a slice by a field value within a struct, the predicate allows you to pick the fields you want to orderBy. Use > for ASC or < for DESC
func (left Person, right Person) bool { return left.Age > right.Age }
func Partition ¶
Partition splits the slice into two slices: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.
func Range ¶ added in v0.5.0
Creates a sequence of numbers, i.e. u.Range(0, 3) = [0 1 2 3], while u.Range(3, 0) = [3 2 1 0]
func Reduce ¶
func Reduce[T, P any](values []T, reduction func(T, P) P, acc P) P
Reduce combine a list of values into a single value. acc is the initial state, and each successive step of it should be returned by the reduction function.
func SortSliceASC ¶ added in v0.7.0
func SortSliceASC[T constraints.Ordered](s []T)
sort any slice ASENDING
func SortSliceDESC ¶ added in v0.7.0
func SortSliceDESC[T constraints.Ordered](s []T)
sort any slice DESCENDING
func SumMap ¶ added in v0.5.0
func SumMap[T any, R constraints.Ordered](list []T, selector func(T) R) (sum R)
Sums the values you select from your struct, basically a sort cut instead of having to perform a u.Map followed by a u.Sum
func ToPointer ¶ added in v0.7.0
func ToPointer[T any](in T) *T
Convert values to pointers
Instead of: v := "value" MyPointerVar = &v
Or v1 := "value1" v2 := 100
obj := Obj{ Field1: &v, Field2: &v2, }
Use: MyPointerVar = ToPointer("value")
func Unique ¶
func Unique[T comparable](values []T) (uniques []T)
Types ¶
type Pipe ¶
type Pipe[T constraints.Ordered] struct { Value []T }
func NewPipe ¶
func NewPipe[T constraints.Ordered](value []T) Pipe[T]
NewPipe starts a Pipe. All future method calls will return Pipe structs. When you've finished the computation, call Value to retrieve the final value.
Methods not returning a slice such as Reduce, All, Any, will break the Pipe and return Value instantly.
func (Pipe[T]) All ¶
All returns true if all the values in the slice pass the predicate truth test. Short-circuits and stops traversing the slice if a false element is found. Breaks the Pipe.
func (Pipe[T]) Any ¶
Any returns true if any of the values in the slice pass the predicate truth test. Short-circuits and stops traversing the slice if a true element is found. Breaks the Pipe.
func (Pipe[T]) Contains ¶
Contains returns true if the value is present in the slice and breaks the Pipe.
func (Pipe[T]) Each ¶
func (c Pipe[T]) Each(action func(T))
Each iterates over a slice of elements, yielding each in turn to an action function. Breaks the Pipe.
func (Pipe[T]) Filter ¶
Filter looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).
func (Pipe[T]) Find ¶
Find looks through each value in the slice, returning the first one that passes a truth test (predicate), or the default value for the type and an error if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire slice. Breaks the Pipe.
func (Pipe[T]) Map ¶
Map produces a new slice of values by mapping each value in the slice through a transform function.
TODO: Move from T to P.
func (Pipe[T]) Max ¶
func (c Pipe[T]) Max() T
Max returns the maximum value in the slice. This function can currently only compare numbers reliably. This function uses operator <. Breaks the Pipe.
func (Pipe[T]) Min ¶
func (c Pipe[T]) Min() T
Min returns the minimum value in the slice. This function can currently only compare numbers reliably. This function uses operator <. Breaks the Pipe.
type Result ¶
type Result[T any] interface { ToValue() (*T, error) IsSuccess() bool // contains filtered or unexported methods }
Result represent the outcome of an operation where failure is possible
type Tuple ¶ added in v0.5.0
type Tuple[L, R any] struct { Left L Right R }
func Join ¶ added in v0.5.0
func Join[T, P any, S comparable]( left []T, right []P, leftSelector func(T) S, rightSelector func(P) S) []Tuple[T, []P]
Joins two slices together and returns a Tuple of [T, []P], the selectors allow you to pick the keys you want to use from your struct's to join the sets together