goulash

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 4 Imported by: 0

README

goulash

Pipeline Go Report Card

goulash provides a bunch of useful functional programming helpers leveraging generics.

Table of Contents

Functions

Chunk
chunks := goulash.Chunk([]int{1, 2, 3, 4}, 2)
fmt.Println(chunks) // [[1 2] [3 4]]
Compact
compacted := goulash.Compact([]int{0, 1, 2, 3})
fmt.Println(compacted) // [1 2 3]
Concat
concatenated := goulash.Concat([]int{1, 2, 3}, []int{4, 5, 6, 7})
fmt.Println(concatenated) // [1 2 3 4 5 6 7]
Filter
filtered := goulash.Filter([]int{1, 2, 3, 4, 5, 6},
func(n int) bool {
return n%2 == 1
})
fmt.Println(filtered) // [1 3 5]
Difference
diff := goulash.Difference([]int{1, 2, 3}, []int{1, 2})
fmt.Println(diff) // [3]
ForEach
var forEachResult [][]int
goulash.ForEach([]int{1, 2, 3}, func(value int, args ...any) {
index := args[0].(int)
forEachResult = append(forEachResult, []int{index, value})
})
fmt.Println(forEachResult) // [[0 1] [1 2] [2 3]]
Intersection
intersected := goulash.Intersection([]string{"a", "b", "c", "d", "e"}, []string{"d", "e"})
fmt.Println(intersected) // ["d", "e"]
GroupBy
grouped := goulash.GroupBy([]float64{6.1, 4.2, 6.3}, math.Floor)
fmt.Println(grouped) // map[4:[4.2] 6:[6.1 6.3]]
Map
mapped := goulash.Map([]int{1, 2, 3}, math.Pow10)
fmt.Println(mapped) // [10 100 1000]
Max
maxResult := goulash.Max([]float32{6.1, 4.2, 6.3}...)
fmt.Println(maxResult) // 6.3
Min
minResult := goulash.Min([]float64{6.1, 4.2, 6.3}...)
fmt.Println(minResult) // 4.2
MinMax
min, max := goulash.MinMax([]float64{6.1, 4.2, 6.3}...)
fmt.Println(min, max) // 4.2 6.3
Reduce
reduced := goulash.Reduce([]uint{6, 7, 8}, func(a uint, b uint) uint {
return a + b
}, 0)
fmt.Println(reduced) // 21
Sort
sorted := goulash.Sort([]int{6, 1, 2, 3, -1, 0, 4, 7, 5})
fmt.Println(sorted) // [-1 0 1 2 3 4 5 6 7]
Union
unified := goulash.Union([][]int{{1, 2}, {2, 3, 4}, {3, 4, 5, 6, 7}}...)
fmt.Println(unified) // [1 2 3 4 5 6 7]
Unique
uniq := goulash.Unique([]int{1, 1, 1, 1, 2, 3})
fmt.Println(uniq) // [1 2 3]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chunk

func Chunk[T any](slice []T, size int) [][]T

func Compact

func Compact[T constraints.Ordered](slice []T) []T

func Concat

func Concat[T any](slices ...[]T) []T

func Difference

func Difference[T constraints.Ordered](slice1 []T, slice2 []T) []T

func Filter

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

func ForEach

func ForEach[T any](slice []T, fn func(currentValue T, args ...any))

func GroupBy

func GroupBy[T constraints.Ordered, M constraints.Ordered](
	slice []T,
	fn func(currentValue T) M,
) map[M][]T

func Intersection

func Intersection[T constraints.Ordered](slice1 []T, slice2 []T) []T

func Map

func Map[T any, M any](slice []T, fn func(currentValue T) M) []M

func Max

func Max[T constraints.Ordered](numbers ...T) T

func Min

func Min[T constraints.Ordered](numbers ...T) T

func MinMax

func MinMax[T constraints.Ordered](numbers ...T) (min T, max T)

func Reduce

func Reduce[T, M any](slice []T, fn func(M, T) M, initValue M) M

func Sort

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

func Union

func Union[T comparable](slices ...[]T) []T

func Unique

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

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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