Gollection is a simple library that provides a convenient set of helpers for working with collections of data. The word "Gollection" is a portmanteau of the words "go" and "collection".
A collection is a container of related data such as a slice or a map. Currently, go channels are beyond the scope if this project due to their lack of flexibility.
It can manipulate, partition, transform, and get stats about collections.
In an attempt to keep our go files small and clean.
v1.0 (Amethyst)
gollection.Append([]int{1, 2, 3}, 4, 5, 6)
// [1, 2, 3, 4, 5, 6]gollection.At([]int{1, 2, 3}, 1)
// 2, nilgollection.Average([]int{1, 2, 3})
// 2alias for Average
gollection.Chunk([]int{1, 2, 3, 4, 5, 6}, 3)
// [[1, 2, 3], [4, 5, 6]]gollection.ChunkBy([]int{1, 2, 3, 4, 5, 6}, func(n int) bool { return n%2 == 1 })
// [[1, 2], [3, 4], [5, 6]]gollection.Collapse([][]int{{1, 2}, {3, 4}, {5, 6}})
// [1, 2, 3, 4, 5, 6]gollection.CollapseMap([]map[string]int{{"a": 1, "c": 4}, {"b": 2, "a": 3}})
// {"a": 3, "b": 2, "c": 4}gollection.Combine([]string{"a", "b"}, []int{1, 2})
// {"a": 1, "b": 2}alias for Combine
gollection.CombineMap(map[string]int{"a": 1}, map[string]int{"b": 2, "a": 3})
// {"a": 3, "b": 2}gollection.Contains([]int{1, 2, 3, 4}, 4)
// truegollection.ContainsKey(map[string]int{"k": 1}, "k")
// truealias for len
gollection.CountBy([]int{1, 2, 3, 4}, func(x int) bool { return x%2 == 0 })
// 2gollection.Counts([]string{"a", "a", "b"})
// {"a": 2, "b": 1}gollection.CrossJoin([]int{1, 2, 3}, []string{"x", "y"})
// [[1, "x"], [1, "y"], [2, "x"], [2, "y"], [3, "x"], [3, "y"]]gollection.Diff([]int{1, 2, 3}, []int{2})
// [1,3]gollection.DiffAssoc(map[string]int{"a": 1, "b": 3}, map[string]int{"a": 1, "b": 2})
// {"b": 3}gollection.DiffKeys(map[string]int{"a": 1, "b": 2}, map[string]int{"b": 3})
// ["a"]gollection.DoesntContain([]int{1, 3, 4, 5}, 2)
// truegollection.Duplicates([]int{1, 1, 2, 3, 3})
// [1,3]sum:= 0
gollection.Each([]int{1, 2, 3}, func(n int) { sum += n })
// sum = 6gollection.Every([]int{2, 4, 6}, func(n int) bool { return n%2 == 0 })
// truegollection.Except([]int{1, 2, 3}, func(n int) bool { return n == 2 })
// [1,3]gollection.Filter([]int{1, 2, 3}, func(n int) bool { return n == 2 })
// [2]gollection.First([]int{1, 2, 3}, func(n, i int) bool { return n > 2 })
// 3gollection.FirstOr([]int{1, 2, 3}, func(n, i int) bool { return n > 3 }, 4)
// 4gollection.FirstOrFail([]int{1, 2, 3}, func(n, i int) bool { return n > 3 })
// panicgollection.FirstWhere([]map[string]any{{"id": 1, "size": "S"}, {"id": 2, "size": "L"}}, "size", "L")
// {"id": 2, "size": "L"}gollection.Flatten([][]int{{1, 2, 3}, {4, 5}, {6}})
// [1,2,3,4,5,6]gollection.FlattenMap([]map[string]int{{"a": 1}, {"b": 2, "a": 3}, {"a": 5, "c": 4}})
// {"a": 5, "b": 2, "c": 4}gollection.Flip(map[string]int{"b": 2, "a": 3})
// {2: "b", 3: "c"}gollection.Forget(map[string]int{"a": 2, "b": 3, "c": 4}, "a", "c")
// {"b": 3}gollection.GroupBy([]map[string]string{{"t": "a", "u": "1"}, {"t": "b", "u": "2"}, {"t": "a", "u": "3"}}, "t")
// {"a": [{"t": "a", "u": "1"}, {"t": "a", "u": "3"}], "b": {"t": "b", "u": "2"}}alias for Contains
gollection.HasAny([]int{1, 2, 3}, []int{0, 3})
// truegollection.HasAnyKeys(map[string]int{"a": 1}, "a", "b")
// truegollection.HasEvery([]int{1, 2, 3}, []int{1, 2})
// truegollection.HasOne([]int{1, 4, 5}, []int{1, 2, 3})
// true
gollection.HasOne([]int{1, 2, 5}, []int{1, 2, 3})
// falsegollection.IndexOf([]string{"a", "b", "c"}, "b")
// 1
gollection.IndexOf([]string{"a", "b", "c"}, "d")
// -1gollection.IsEmpty([]string{"a", "b", "c"})
// falsegollection.Intersect([]int{1, 1, 2}, []int{1, 2, 2})
// [1, 2]gollection.IntersectAssoc(map[string]int{"a": 1, "b": 2}, map[string]int{"a": 1, "b": 9})
// {"a": 1}gollection.IntersectByKeys(map[string]int{"a": 1, "b": 2}, map[string]int{"b": 9})
// {"b": 2}gollection.KeyBy([]map[string]int{{"k": 1}, {"k": 2, "a": 3}, {"k": 2, "b": 4}}, "k")
// {1: [{"k": 1}], 2: [{"k": 2, "a": 3}, {"k": 2, "b": 4}]}gollection.Last([]int{1, 2, 3, 4}, func(n, i int) bool { return n >= 2 })
// 4
gollection.Last([]int{1, 2, 3, 4}, func(n, i int) bool { return n > 4 })
// nilgollection.LastOr([]int{1, 2, 3, 4}, func(n, i int) bool { return n >= 4 }, 99)
// 4
gollection.LastOr([]int{1, 2, 3, 4}, func(n, i int) bool { return n > 4 }, 99)
// 99gollection.LastOrFail([]int{1, 2, 3, 4}, func(n, i int) bool { return n >= 4 })
// 4
gollection.LastOrFail([]int{1, 2, 3, 4}, func(n, i int) bool { return n > 4 })
// panicgollection.LastWhere([]map[string]any{{"n": 1}, {"n": 2}, {"n": 2}}, "n", 2)
// {"n": 2}
gollection.LastWhere([]map[string]any{{"n": 1}, {"n": 2}, {"n": 2}}, "n", 3)
// nilalias for Count
gollection.Map([]int{1, 2}, func(n int) string { return string(rune('0' + n)) })
// ["1", "2"]gollection.Max([]int{1, 3, 2, 5, 4})
// 5gollection.Mode([]int{1, 2, 1, 2, 3, 2})
// ["1", "2"]gollection.Min([]int{3, 2, 5, 4})
// 2gollection.Multiply([]int{1, 2}, 3)
// [1, 1, 1, 2, 2, 2]gollection.Nth([]string{"a", "b"}, 1)
// "b"gollection.NthFromLast([]string{"a", "b"}, 1)
// "a"gollection.Only([]map[string]int{{"a": 1, "b": 2}, {"a": 3, "b": 4, "c": 5}}, "a", "b")
// [{"a": 1, "b": 2}, {"a": 3, "b": 4}]gollection.PadLeft([]int{1}, 2, 0)
// [0, 0, 1]gollection.PadRight([]int{1}, 4, 3)
// [1, 3, 3, 3, 3]gollection.Partition([]int{1, 2, 3, 4}, func(i, n int) bool { return n%2 == 0 })
// [[2,4], [1,3]]
gollection.Partition([]int{1, 2, 3, 4}, func(i, n int) bool { return i < 2 })
// [[1,2], [3,4]]gollection.Percentage([]int{1, 2, 3, 4}, func(n int) bool { return n%2 == 0 })
// 50gollection.Prepend([]int{3, 4}, 1, 2)
// [1, 2, 3, 4]gollection.Push([]int{3, 4}, 1)
// [3, 4, 1]gollection.Pop([]int{3, 4, 1})
// 1, [3, 4]gollection.Random([]int{1, 2, 3, 4, 5})
// 4, nil
gollection.Random([]int{})
// nil, Errorgollection.Range([]int{0, 1, 2, 3, 4, 5}, 2, 4)
// [2, 3], nil
gollection.Range([]int{0, 1, 2, 3, 4, 5}, 4, 2)
// nil, Errorgollection.Reduce([]int{1, 2, 3}, func(acc, n int) int { return acc + n }, 0)
// 6gollection.ReduceWithIndex([]int{1, 2, 3}, func(acc, n, i int) int { return acc + i }, 0)
// 3gollection.Reject([]int{1, 2, 3}, func(n int) bool { return n == 2 })
// [1, 3]gollection.RejectWithIndex([]int{1, 2, 3}, func(n, i int) bool { return i == 2 })
// [1, 2]gollection.Replace([]int{1, 2, 3, 4, 5}, map[int]int{1: 9, 3: 10})
// [1, 9, 3, 10, 5]gollection.Reverse([]int{1, 2, 3, 4, 5})
// [5, 4, 3, 2, 1]gollection.ReverseMap(map[int]string{1: "x", 3: "y"})
// {"x": 1, "y": 3}gollection.Select([]map[string]any{{"a": 1, "b": "x", "c": 2.0}, {"a": 2, "b": "y", "c": 1.5}}, "a", "b")
// [{"a": 1, "b": "x"}, {"a": 2, "b": "y"}]gollection.Shuffle([]int{1, 2, 3, 4, 5})
// [4, 1, 3, 5, 2]gollection.Skip([]int{1, 1, 1, 2, 3}, 2)
// [1, 2, 3]gollection.SkipUntil([]int{1, 2, 3}, func(n int) bool { return n == 2 })
// [2, 3]gollection.SkipWhile([]int{1, 2, 1}, func(n int) bool { return n < 2 })
// [2, 1]gollection.Slice([]int{1, 2, 3, 4, 5}, 1, 3)
// [2, 3]gollection.Sort([]int{3, 1, 2}, gollection.SortDirectionAsc)
// [1, 2, 3]
gollection.Sort([]int{1, 3, 2}, gollection.SortDirectionDesc)
// [3, 2, 1]gollection.SortAsc([]int{2, 1})
// [1, 2]gollection.SortBy([]map[string]int{{"v": 2}, {"v": 1}}, "v", gollection.SortDirectionAsc)
// [{"v": 1}, {"v": 2}]gollection.SortByAsc([]map[string]int{{"v": 3}, {"v": 1}}, "v")
// [{"v": 1}, {"v": 3}]gollection.SortByDesc([]map[string]int{{"v": 1}, {"v": 3}}, "v")
// [{"v": 3}, {"v": 1}]gollection.SortDesc([]int{1, 3, 2})
// [3, 2, 1]gollection.Splice([]int{1, 2, 3, 4}, 1, 2, 9, 9)
// [1, 9, 9, 4]gollection.Split([]int{1, 2, 3, 4, 5}, 2)
// [[1, 2], [3, 4], [5]]gollection.SplitInto([]int{1, 2, 3, 4, 5, 6}, 2)
// [[1, 2, 3], [4, 5, 6]]gollection.Take([]int{1, 2, 3}, 2)
// [1, 2]gollection.TakeUntil([]int{1, 2, 3}, func(n int) bool { return n == 2 })
// [1]gollection.TakeWhile([]int{1, 2, 3}, func(n int) bool { return n < 3 })
// [1, 2]gollection.Union([]int{1, 2}, []int{2, 3})
// [1, 2, 3]gollection.Unique([]int{1, 1, 2, 3, 3})
// [2]gollection.Where([]map[string]int{{"k": 1}, {"k": 2}}, "k", 2)
// [{"k": 2}]gollection.WhereBetween([]map[string]int{{"x": 2}, {"x": 5}, {"x": 10}}, "x", 2, 5)
// [{"x": 2}, {"x": 5}]gollection.WhereIn([]map[string]int{{"t": 1}, {"t": 9}}, "t", []int{9})
// [{"t": 9}]gollection.WhereNot([]map[string]int{{"t": 1}, {"t": 2}}, "t", 1)
// [{"t": 2}]gollection.WhereNotBetween([]map[string]int{{"x": 1}, {"x": 4}, {"x": 7}}, "x", 3, 6)
// [{"x": 1}, {"x": 7}]gollection.WhereNotIn([]map[string]int{{"t": 1}, {"t": 2}}, "t", []int{1})
// [{"t": 2}]gollection.Zip([]int{1, 2}, []string{"a", "b"})
// [[1, "a"], [2, "b"]]Did we miss something? Create an issue or a PR.