Goulash offers a variety of useful utility functions for everyday programming tasks by leveraging Go generics and keeping the functional programming paradigm in mind.
Table of Contents
- Goulash
- Quick Start
- Functions
- All
- Any
- Chunk
- Combinations
- Compact
- Concat
- Contains
- Count
- Difference
- Drop
- DropWhile
- Filter
- Find
- FindIndex
- FlatMap
- Flatten
- Fold
- ForEach
- GroupBy
- IndexOf
- Intersection
- Join
- Keys
- LastIndexOf
- Map
- Max
- Min
- MinMax
- None
- Partition
- Permutations
- Product
- RandomSample
- Range
- Reduce
- Repeat
- Reverse
- Scan
- Sliding
- Sort
- Sum
- Take
- TakeWhile
- Ternary
- Transpose
- Union
- Unique
- Values
- Zip
go get github.com/farbodsalimi/goulashpackage 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)
}result := __.All([]float64{3.1, 4.1, 5.1, 1.1, 0})
fmt.Println(result) // falseresult := __.Any([]float64{3.1, 4.1, 5.1, 1.1, 0})
fmt.Println(result) // truechunks := __.Chunk([]int{1, 2, 3, 4}, 2)
fmt.Println(chunks) // [[1 2] [3 4]]combs := __.Combinations([]int{1, 2, 3, 4}, 2)
fmt.Println(combs) // [[1 2] [1 3] [1 4] [2 3] [2 4] [3 4]]compacted := __.Compact([]int{0, 1, 2, 3})
fmt.Println(compacted) // [1 2 3]concatenated := __.Concat([]int{1, 2, 3}, []int{4, 5, 6, 7})
fmt.Println(concatenated) // [1 2 3 4 5 6 7]result := __.Contains([]float64{3.1, 4.1, 5.1, 1.1, -2.1}, -2.1)
fmt.Println(result) // trueevenCount := __.Count([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n%2 == 0
})
fmt.Println(evenCount) // 2diff := __.Difference([]int{1, 2, 3}, []int{1, 2})
fmt.Println(diff) // [3]afterFirst2 := __.Drop([]int{1, 2, 3, 4, 5}, 2)
fmt.Println(afterFirst2) // [3 4 5]dropped := __.DropWhile([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n < 4
})
fmt.Println(dropped) // [4 5]filtered := __.Filter([]int{1, 2, 3, 4, 5, 6}, func(n int) bool {
return n%2 == 1
})
fmt.Println(filtered) // [1 3 5]found, exists := __.Find([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n > 3
})
fmt.Println(found, exists) // 4 trueindex := __.FindIndex([]string{"a", "b", "c"}, func(s string) bool {
return s == "b"
})
fmt.Println(index) // 1flatten := __.FlatMap([]string{"it's sunny in", "", "California"}, func(s string) []string {
return strings.Split(s, " ")
})
fmt.Println(flatten) // ["it's", "sunny", "in", "", "California"]flattened := __.Flatten([][]int{{1, 2}, {3, 4}, {5}})
fmt.Println(flattened) // [1 2 3 4 5]folded := __.Fold([]int{1, 2, 3}, 0, func(a int, b int) int {
return a + b
})
fmt.Println(folded) // 6var 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]]grouped := __.GroupBy([]float64{6.1, 4.2, 6.3}, math.Floor)
fmt.Println(grouped) // map[4:[4.2] 6:[6.1 6.3]]index := __.IndexOf([]string{"a", "b", "c", "b"}, "b")
fmt.Println(index) // 1intersected := __.Intersection([]string{"a", "b", "c", "d", "e"}, []string{"d", "e"})
fmt.Println(intersected) // ["d", "e"]joined := __.Join([]uint{1, 2, 3, 4}, ", ")
fmt.Println(joined) // 1, 2, 3, 4joined := __.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.1joined := __.Join([]string{"a", "b", "c", "d"}, "*")
fmt.Println(joined) // a*b*c*dkeys := __.Keys(map[string]string{"key1": "value", "key2": "value", "key3": "value"})
fmt.Println(keys) // ["key1", "key2", "key3"lastIndex := __.LastIndexOf([]string{"a", "b", "c", "b"}, "b")
fmt.Println(lastIndex) // 3mapped := __.Map([]int{1, 2, 3}, math.Pow10)
fmt.Println(mapped) // [10 100 1000]maxResult := __.Max([]float32{6.1, 4.2, 6.3}...)
fmt.Println(maxResult) // 6.3minResult := __.Min([]float64{6.1, 4.2, 6.3}...)
fmt.Println(minResult) // 4.2min, max := __.MinMax([]float64{6.1, 4.2, 6.3}...)
fmt.Println(min, max) // 4.2 6.3hasEven := __.None([]int{1, 3, 5}, func(n int) bool {
return n%2 == 0
})
fmt.Println(hasEven) // trueevens, odds := __.Partition([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n%2 == 0
})
fmt.Println(evens) // [2 4]
fmt.Println(odds) // [1 3 5]perms := __.Permutations([]int{1, 2, 3})
fmt.Println(len(perms)) // 6 (all permutations)result := __.Product([]int{2, 3, 4})
fmt.Println(result) // 24sample := __.RandomSample([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 3)
fmt.Println(sample) // [random selection of 3 elements]numbers := __.Range(1, 5)
fmt.Println(numbers) // [1 2 3 4]reduced := __.Reduce([]uint{6, 7, 8}, func(a uint, b uint) uint {
return a + b
}, 0)
fmt.Println(reduced) // 21repeated := __.Repeat("hello", 3)
fmt.Println(repeated) // ["hello" "hello" "hello"]reversed := __.Reverse([]int{1, 2, 3, 4, 5})
fmt.Println(reversed) // [5 4 3 2 1]cumSum := __.Scan([]int{1, 2, 3, 4}, func(acc, n int) int {
return acc + n
}, 0)
fmt.Println(cumSum) // [0 1 3 6 10]windows := __.Sliding([]int{1, 2, 3, 4, 5}, 3)
fmt.Println(windows) // [[1 2 3] [2 3 4] [3 4 5]]sorted := __.Sort([]int{6, 1, 2, 3, -1, 0, 4, 7, 5})
fmt.Println(sorted) // [-1 0 1 2 3 4 5 6 7]total := __.Sum([]int{1, 2, 3, 4, 5})
fmt.Println(total) // 15first3 := __.Take([]int{1, 2, 3, 4, 5}, 3)
fmt.Println(first3) // [1 2 3]taken := __.TakeWhile([]int{1, 2, 3, 4, 1}, func(n int) bool {
return n < 4
})
fmt.Println(taken) // [1 2 3]result := __.Ternary((2 + 2) == 4, "yup", "nope")
fmt.Println(result) // yupmatrix := [][]int{{1, 2, 3}, {4, 5, 6}}
transposed := __.Transpose(matrix)
fmt.Println(transposed) // [[1 4] [2 5] [3 6]]unified := __.Union([][]int{{1, 2}, {2, 3, 4}, {3, 4, 5, 6, 7}}...)
fmt.Println(unified) // [1 2 3 4 5 6 7]uniq := __.Unique([]int{1, 1, 1, 1, 2, 3})
fmt.Println(uniq) // [1 2 3]values := __.Values(map[string]string{"key1": "value1", "key2": "value2", "key3": "value3"})
fmt.Println(values) // ["value1", "value2", "value3"]zipped := __.Zip([]int{1, 2, 3}, []string{"a", "b"})
fmt.Println(zipped) // []Pair[int, string]{{First: 1, Second: "a"}, {First: 2, Second: "b"}}