goulash

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2023 License: MIT Imports: 6 Imported by: 0

README

Goulash

Pipeline Release Go Report Card Maintainability Test Coverage

Goulash provides a bunch of useful functional programming helpers leveraging Go generics.

Table of Contents

Quick Start

Installation
go get github.com/farbodsalimi/goulash
Example
package main

import (
	"fmt"

	__ "github.com/farbodsalimi/goulash"
)

type Player struct {
	id    int
	name  string
	score float64
	bonus float64
}

func main() {
	players := []Player{
		{id: 1, name: "David", score: 20, bonus: 15},
		{id: 2, name: "Jessica", score: 10, bonus: 25},
		{id: 3, name: "Alex", score: 40, bonus: 45},
		{id: 4, name: "Tom", score: 30, bonus: 25},
	}

	totalScore := __.Reduce(__.Map(__.Filter(players, func(p Player) bool {
		return len(p.name) > 4
	}), func(p Player) float64 {
		return p.score + p.bonus
	}), func(acc, newScore float64) float64 {
		return acc + newScore
	}, 0)

	fmt.Println(totalScore)
}

Functions

All
result := __.All([]float64{3.1, 4.1, 5.1, 1.1, 0})
fmt.Println(result) // false
Any
result := __.Any([]float64{3.1, 4.1, 5.1, 1.1, 0})
fmt.Println(result) // true
Chunk
chunks := __.Chunk([]int{1, 2, 3, 4}, 2)
fmt.Println(chunks) // [[1 2] [3 4]]
Compact
compacted := __.Compact([]int{0, 1, 2, 3})
fmt.Println(compacted) // [1 2 3]
Concat
concatenated := __.Concat([]int{1, 2, 3}, []int{4, 5, 6, 7})
fmt.Println(concatenated) // [1 2 3 4 5 6 7]
Contains
result := __.Contains([]float64{3.1, 4.1, 5.1, 1.1, -2.1}, -2.1)
fmt.Println(result) // true
Difference
diff := __.Difference([]int{1, 2, 3}, []int{1, 2})
fmt.Println(diff) // [3]
Filter
filtered := __.Filter([]int{1, 2, 3, 4, 5, 6}, func(n int) bool {
	return n%2 == 1
})
fmt.Println(filtered) // [1 3 5]
Fold
folded := __.Fold([]int{1, 2, 3}, 0, func(a int, b int) int {
	return a + b
})
fmt.Println(folded) // 6
ForEach
var forEachResult [][]int
__.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]]
GroupBy
grouped := __.GroupBy([]float64{6.1, 4.2, 6.3}, math.Floor)
fmt.Println(grouped) // map[4:[4.2] 6:[6.1 6.3]]
Intersection
intersected := __.Intersection([]string{"a", "b", "c", "d", "e"}, []string{"d", "e"})
fmt.Println(intersected) // ["d", "e"]
Join
joined := __.Join([]uint{1, 2, 3, 4}, ", ")
fmt.Println(joined) // 1, 2, 3, 4
joined := __.Join([]float64{1.1, 2.1, 3.1, 1.1, 2.1, 3.1}, "~")
fmt.Println(joined) // 1.1~2.1~3.1~1.1~2.1~3.1
joined := __.Join([]string{"a", "b", "c", "d"}, "*")
fmt.Println(joined) // a*b*c*d
Keys
keys := __.Keys(map[string]string{"key1": "value", "key2": "value", "key3": "value"})
fmt.Println(keys) // ["key1", "key2", "key3"
Map
mapped := __.Map([]int{1, 2, 3}, math.Pow10)
fmt.Println(mapped) // [10 100 1000]
Max
maxResult := __.Max([]float32{6.1, 4.2, 6.3}...)
fmt.Println(maxResult) // 6.3
Min
minResult := __.Min([]float64{6.1, 4.2, 6.3}...)
fmt.Println(minResult) // 4.2
MinMax
min, max := __.MinMax([]float64{6.1, 4.2, 6.3}...)
fmt.Println(min, max) // 4.2 6.3
Reduce
reduced := __.Reduce([]uint{6, 7, 8}, func(a uint, b uint) uint {
	return a + b
}, 0)
fmt.Println(reduced) // 21
Sort
sorted := __.Sort([]int{6, 1, 2, 3, -1, 0, 4, 7, 5})
fmt.Println(sorted) // [-1 0 1 2 3 4 5 6 7]
Ternary
result := __.Ternary((2 + 2) == 4, "yup", "nope")
fmt.Println(result) // yup
Union
unified := __.Union([][]int{{1, 2}, {2, 3, 4}, {3, 4, 5, 6, 7}}...)
fmt.Println(unified) // [1 2 3 4 5 6 7]
Unique
uniq := __.Unique([]int{1, 1, 1, 1, 2, 3})
fmt.Println(uniq) // [1 2 3]
Values
values := __.Values(map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"})
fmt.Println(values) // ["value1", "value2", "value3"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.2.0

func All[T constraints.Ordered](slice []T) bool

All returns true only if every element in slice evaluates to true

func Any added in v0.2.0

func Any[T constraints.Ordered](slice []T) bool

Any returns true only if any element in slice evaluates to true

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 Contains added in v0.2.0

func Contains[T, M any](object T, element M) bool

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 Fold added in v0.3.0

func Fold[T any](slice []T, empty T, fn func(a T, b T) T) 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 Join added in v0.3.0

func Join[T any](slice []T, delimiter string) string

Join creates and returns a new string by concatenating all of the elements in a slice, separated by a specified delimiter

func Keys added in v0.2.0

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

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 Ternary added in v0.2.0

func Ternary[T any](condition bool, exprIfTrue T, exprIfFalse T) T

Ternary evaluates a bool expression and returns the result of one of the two expressions, depending on whether the bool expression evaluates to true or false

func Union

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

func Unique

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

func Values added in v0.2.0

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

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