dash

package
v0.0.0-...-09f6022 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2022 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package dash brings a number of functional utilities to Go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cycle

func Cycle[T any](slice []T, n uint) []T

Cycle returns a new slice by repeating slice n times.

func Drop

func Drop[T any](slice []T, n int) []T

Drop returns a new slice containing the remaining values of slice except the first n values.

func Every

func Every[T any](slice []T, pred func(T) bool) bool

Every returns true if pred never returns false, returns false otherwise.

func Filter

func Filter[T any](slice []T, pred func(T) bool) []T

Filter returns a new slice containing all values of slice that the function when function pred returns true.

func FlatMap

func FlatMap[T any, M any](slice []T, f func(T) []M) []M

FlatMap returns a new slice containing values produced by applying function f to every value in slice, and flattern to a one-dimensional slice.

func GroupBy

func GroupBy[T any, D comparable](slice []T, f func(T) D) map[D][]T

GroupBy returns a map. The keys are the return values of applying f with the values of slice. The values are values of slice which evaluates to it's key.

func Include

func Include[T comparable](slice []T, e T) bool

Include returns true if value e is in slice.

func Intersection

func Intersection[T comparable](slices ...[]T) (result []T)

Intersection returns a single slice containing uniq values among slices.

func Keys

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

Keys returns a slice contains all keys in map m

func Map

func Map[T any, M any](slice []T, f func(T) M) []M

Map returns a new slice containing values produced by applying function f to every value in slice.

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge maps into one map, uses the value in the latter map.

func MergeWith

func MergeWith[K comparable, V any](f func(v1 V, v2 V) V, maps ...map[K]V) map[K]V

MergeWith returns a new map contains all keys in maps and values by applying function f to all values with the same key

func None

func None[T any](slice []T, pred func(T) bool) bool

None returns true if pred never returns true, returns false otherwise.

func ParallelMap

func ParallelMap[T any, M any](slice []T, f func(T) M) []M

ParallelMap works just like Map including the result order, but f is applied in parallel with goroutine. This version creates len(slice) of goroutines, use ParallelMapN if the amount of goroutines created at the same time needs to be limited.

func ParallelMapN

func ParallelMapN[T any, M any](slice []T, f func(T) M, limit int) []M

ParallelMapN is a version of ParallelMap, but has a limit of goroutine created at the same time.

func Partition

func Partition[T any](slice []T, n int) [][]T

Partition turn a slice into n-sized(n>0) slices the last k elements are dropped if k<n

func PartitionStep

func PartitionStep[T any](slice []T, n uint, s uint) [][]T

Partition turn a slice into n-sized(n>0) slices each start with offset s(s>0), right to previous start location the last partition is dropped when size<n

func Reduce

func Reduce[T any, A any](slice []T, f func(A, T) A, init A) A

Reduce combines all values of slice into a single value by applying function f with the accumulator value and every value in slice. First iteration of accumulator value is init, the subsequent accumulator value is the return value of f.

func Reverse

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

Reverse return a new slice in reversed order.

func Rotate

func Rotate[T any](slice []T, n uint) []T

Rotate returns a new slice by rotating self so that the element at n is the first element of the new slice.

func Shuffle

func Shuffle[T any](slice []T) []T

Shuffle returns a new slice which containing all the values of slice, but in a random order.

func Some

func Some[T any](slice []T, pred func(T) bool) bool

Some returns false if pred never returns true, returns true otherwise.

func Take

func Take[T any](slice []T, n int) []T

Take returns a new slice containing the first n values of slice.

func Uniq

func Uniq[T comparable](slice []T) (result []T)

Uniq returns a new slice containing all uniq values in slice.

func Values

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

Keys returns a slice contains all values in map m

Types

type Pair

type Pair[T any, M any] struct {
	First  T
	Second M
}

Pair is a pair of two values. Access the values with .First and .Second

func NewPair

func NewPair[T any, M any](first T, second M) Pair[T, M]

NewPair creates a Pair contains values first and second.

Jump to

Keyboard shortcuts

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