Documentation ¶
Overview ¶
A package of helper functions and datastructures inspired by other languages, mainly python
Index ¶
- Variables
- func All(conditions ...bool) bool
- func Any(conditions ...bool) bool
- func Append[L ~[]T, T any](arr *L, value T)
- func Assert(condition bool, messages ...string)
- func AssertEqual[T comparable](x, y T)
- func AssertFalse(condition bool)
- func AssertGreaterThan[T constraints.Ordered](x, y T)
- func AssertGreaterThanOrEqual[T constraints.Ordered](x, y T)
- func AssertLessThan[T constraints.Ordered](x, y T)
- func AssertLessThanOrEqual[T constraints.Ordered](x, y T)
- func AssertMapsEqual[M map[K]V, K comparable, V comparable](x, y M)
- func AssertMembersEqual[L ~[]T, T comparable](x, y L)
- func AssertSlicesEqual[L ~[]T, T comparable](x, y L)
- func AssertTrue(condition bool)
- func Clear[L ~[]T, T any](arr *L)
- func Combinations[T any](arr []T, length int) <-chan []T
- func Contains[L ~[]T, T comparable](arr L, value T) bool
- func Copy[L ~[]T, T any](arr L) L
- func Count[L ~[]T, T comparable](arr L, value T) int
- func Every[L ~[]T, T any](arr L, f func(T) bool) bool
- func Extend[L ~[]T, T any](arr *L, items []T)
- func Filter[L ~[]T, T any](arr L, condition func(T) bool) []T
- func Find[L ~[]T, T any](arr L, condition func(T) bool) (value T, found bool)
- func ForEach[L ~[]T, T any](arr L, f func(index int, value T))
- func ForEachRef[L ~[]T, T any](arr L, f func(index int, value *T))
- func GeneratorToArray[T any](c <-chan []T) [][]T
- func Index[L ~[]T, T comparable](arr L, value T) int
- func Insert[L ~[]T, T any](arr *L, value T, index int)
- func Map[L ~[]T, T any, V any](arr L, f func(T) V) []V
- func Max[T constraints.Ordered](elements ...T) (T, error)
- func MembersMatch[L ~[]T, T comparable](arr1 L, arr2 L) bool
- func Min[T constraints.Ordered](elements ...T) (T, error)
- func NumPermutations[T any](arr []T) int
- func Permutations[T any](arr []T, length int) <-chan []T
- func Pop[L ~[]T, T any](arr *L) T
- func Prod[T Number](numbers ...T) T
- func Reduce[L ~[]T, T any, V any](arr L, f func(V, T) V, acc V) V
- func Remove[L ~[]T, T any](arr *L, index int) T
- func Reverse[L ~[]T, T any](arr *L)
- func Shift[L ~[]T, T any](arr *L) T
- func Some[L ~[]T, T any](arr L, f func(T) bool) bool
- func Sum[T Number](numbers ...T) T
- func UnShift[L ~[]T, T any](arr *L, value T)
- func ValueAt[L ~[]T, T any](arr L, index int) T
- func Zip[L ~[]T, T any](arrs ...[]T) ([][]T, error)
- type ComparableList
- type Counter
- func (c *Counter[T]) Add(value T)
- func (c *Counter[T]) Elements() counterElements[T]
- func (c Counter[T]) Get(value T) int
- func (c *Counter[T]) MostCommon(n int) counterElements[T]
- func (c Counter[T]) String() string
- func (c *Counter[T]) Subtract(value T)
- func (c Counter[T]) Total() int
- func (c *Counter[T]) Update(arrs ...[]T)
- type Deque
- func (d Deque[T]) Capacity() int
- func (d *Deque[T]) Clear()
- func (d Deque[T]) Copy() *Deque[T]
- func (d Deque[T]) Elements() []T
- func (d *Deque[T]) ExtendLeft(arr []T)
- func (d *Deque[T]) ExtendRight(arr []T)
- func (d Deque[T]) Index(nums ...int) []T
- func (d *Deque[T]) Len() int
- func (d *Deque[T]) PeekLeft() T
- func (d *Deque[T]) PeekRight() T
- func (d *Deque[T]) PopLeft() T
- func (d *Deque[T]) PopRight() T
- func (d *Deque[T]) PushLeft(value T)
- func (d *Deque[T]) PushRight(value T)
- func (d *Deque[T]) Reverse()
- func (d *Deque[T]) Rotate(n int)
- func (d Deque[T]) String() string
- type Dict
- func (dict Dict[K, V]) Clear()
- func (dict Dict[K, V]) Copy() Dict[K, V]
- func (dict Dict[K, V]) Get(key K, fallback ...V) V
- func (dict Dict[K, V]) Has(key K) bool
- func (dict Dict[K, V]) Items() []DictItem[K, V]
- func (dict Dict[K, V]) Keys() []K
- func (dict Dict[K, V]) Pop(key K, fallback ...V) (V, bool)
- func (dict Dict[K, V]) SetDefault(key K, d V) V
- func (dict1 Dict[K, V]) Update(dict2 Dict[K, V])
- func (dict Dict[K, V]) Values() []V
- type DictItem
- type List
- func (list *List[T]) Append(value T)
- func (list List[T]) At(index int) T
- func (list *List[T]) Clear()
- func (list List[T]) Copy() List[T]
- func (list *List[T]) Extend(items []T)
- func (list List[T]) Find(f func(T) bool) (value T, found bool)
- func (list List[T]) ForEach(f func(int, T))
- func (list List[T]) ForEachRef(f func(int, *T))
- func (list *List[T]) Insert(value T, index int)
- func (list List[T]) Map(f func(T) any) []any
- func (list *List[T]) Pop() T
- func (list List[T]) Reduce(f func(any, T) any, acc any) any
- func (list *List[T]) Remove(index int) T
- func (list *List[T]) Reverse()
- func (list *List[T]) Shift() T
- func (list List[T]) Some(f func(T) bool) bool
- func (list *List[T]) UnShift(value T)
- type Number
- type Set
- func (set Set[T]) Add(values ...T)
- func (set Set[T]) Clear()
- func (set Set[T]) Copy() Set[T]
- func (set Set[T]) Difference(sets ...Set[T]) Set[T]
- func (set Set[T]) DifferenceUpdate(sets ...Set[T])
- func (set Set[T]) Discard(value T)
- func (set Set[T]) Equals(set2 Set[T]) bool
- func (set Set[T]) Has(value T) bool
- func (set Set[T]) Intersection(sets ...Set[T]) Set[T]
- func (set Set[T]) IntersectionUpdate(sets ...Set[T])
- func (set1 Set[T]) IsDisjoint(set2 Set[T]) bool
- func (set1 Set[T]) IsSubset(set2 Set[T]) bool
- func (set1 Set[T]) IsSuperset(set2 Set[T]) bool
- func (set Set[T]) Members() []T
- func (set Set[T]) Pop() (T, error)
- func (set Set[T]) Remove(value T) bool
- func (set Set[T]) String() string
- func (set1 Set[T]) SymmetricDifference(set2 Set[T]) Set[T]
- func (set Set[T]) SymmetricDifferenceUpdate(sets ...Set[T])
- func (set1 Set[T]) Union(set2 Set[T]) Set[T]
- func (set Set[T]) Update(sets ...Set[T])
Examples ¶
- All
- Any
- Combinations
- Counter.Add
- Counter.Elements
- Counter.Get
- Counter.MostCommon
- Counter.Subtract
- Counter.Total
- Counter.Update
- Deque.Capacity
- Deque.Clear
- Deque.Copy
- Deque.Elements
- Deque.ExtendLeft
- Deque.ExtendRight
- Deque.Index
- Deque.PeekLeft
- Deque.PeekRight
- Deque.PopLeft
- Deque.PopRight
- Deque.PushLeft
- Deque.PushRight
- Deque.Reverse
- Deque.Rotate
- Dict.Clear
- Dict.Copy
- Dict.Get
- Dict.Has
- Dict.Items
- Dict.Keys
- Dict.Pop
- Dict.SetDefault
- Dict.Values
- GeneratorToArray
- Max
- Min
- NewCounter
- NewDeque
- NewSet
- NumPermutations
- Permutations
- Prod
- Set.Add
- Set.Clear
- Set.Copy
- Set.Difference
- Set.DifferenceUpdate
- Set.Discard
- Set.Equals
- Set.Has
- Set.Intersection
- Set.IntersectionUpdate
- Set.IsDisjoint
- Set.IsSubset
- Set.IsSuperset
- Set.Members
- Set.Pop
- Set.Remove
- Set.SymmetricDifference
- Set.SymmetricDifferenceUpdate
- Set.Union
- Set.Update
- Sum
Constants ¶
This section is empty.
Variables ¶
var ErrAssertion error = errors.New("assertion failed")
Functions ¶
func All ¶
Returns true if all of the given conditions are true
Example ¶
x := 9 isValid := All(x > 0, x < 10, x%2 != 0) fmt.Println(isValid)
Output: true
func Any ¶
Returns true if at least one of the given conditions are true
Example ¶
fruits := "apple banana grape" isValid := Any(strings.HasPrefix(fruits, "grape"), strings.Contains(fruits, "banana"), strings.HasSuffix(fruits, "apple")) fmt.Println(isValid)
Output: true
func AssertEqual ¶
func AssertEqual[T comparable](x, y T)
func AssertFalse ¶
func AssertFalse(condition bool)
func AssertGreaterThan ¶
func AssertGreaterThan[T constraints.Ordered](x, y T)
func AssertGreaterThanOrEqual ¶
func AssertGreaterThanOrEqual[T constraints.Ordered](x, y T)
func AssertLessThan ¶
func AssertLessThan[T constraints.Ordered](x, y T)
func AssertLessThanOrEqual ¶
func AssertLessThanOrEqual[T constraints.Ordered](x, y T)
func AssertMapsEqual ¶
func AssertMapsEqual[M map[K]V, K comparable, V comparable](x, y M)
func AssertMembersEqual ¶
func AssertMembersEqual[L ~[]T, T comparable](x, y L)
func AssertSlicesEqual ¶
func AssertSlicesEqual[L ~[]T, T comparable](x, y L)
func AssertTrue ¶
func AssertTrue(condition bool)
func Combinations ¶
Returns an iterable containing all possible subsets of the array of the given length
Example ¶
nums := []int{1, 2, 3} for combo := range Combinations(nums, 2) { fmt.Println(combo) }
Output: [1 2] [1 3] [2 3]
func Contains ¶
func Contains[L ~[]T, T comparable](arr L, value T) bool
func Count ¶
func Count[L ~[]T, T comparable](arr L, value T) int
func ForEachRef ¶
func GeneratorToArray ¶
func GeneratorToArray[T any](c <-chan []T) [][]T
Converts a channel used as a generator to an array
Example ¶
nums := []int{1, 2, 3} combos := Combinations(nums, 2) fmt.Println(GeneratorToArray(combos))
Output: [[1 2] [1 3] [2 3]]
func Index ¶
func Index[L ~[]T, T comparable](arr L, value T) int
func Max ¶
func Max[T constraints.Ordered](elements ...T) (T, error)
Returns the highest value of the given arguments. Returns an error if no arguments are given.
Example ¶
max, err := Max(1, 23, 100, -5, 10) fmt.Println(max, err) max2, err2 := Max[int]() fmt.Println(max2, err2)
Output: 100 <nil> 0 Max() expected at least 1 argument, got 0
func MembersMatch ¶
func MembersMatch[L ~[]T, T comparable](arr1 L, arr2 L) bool
func Min ¶
func Min[T constraints.Ordered](elements ...T) (T, error)
Returns the lowest value of the given arguments. Returns an error if no arguments are given.
Example ¶
min, err := Min(1, 23, 100, -5, 10) fmt.Println(min, err) min2, err2 := Min[int]() fmt.Println(min2, err2)
Output: -5 <nil> 0 Min() expected at least 1 argument, got 0
func NumPermutations ¶
Returns the number of permutations of the array
Example ¶
nums := []int{1, 2, 3, 4, 5} fmt.Println(NumPermutations(nums))
Output: 120
func Permutations ¶
Returns an iterable containing all possible orderings of subsets of array of the given length. If -1 is provided for the length, the length of the array is used.
Example ¶
nums := []int{1, 2, 3} for p := range Permutations(nums, -1) { fmt.Println(p) }
Output: [1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 2 1] [3 1 2]
func Prod ¶
func Prod[T Number](numbers ...T) T
Multiplies the given numeric arguments and returns the product
Example ¶
product := Prod(1, 23, 100, -5, 10) fmt.Println(product) // Output -11500
Output:
Types ¶
type ComparableList ¶
type ComparableList[T comparable] struct { List[T] }
func (ComparableList[T]) Contains ¶
func (list ComparableList[T]) Contains(value T) bool
func (ComparableList[T]) Count ¶
func (list ComparableList[T]) Count(value T) int
func (ComparableList[T]) Every ¶
func (list ComparableList[T]) Every(f func(T) bool) bool
func (ComparableList[T]) Filter ¶
func (list ComparableList[T]) Filter(f func(T) bool) List[T]
func (ComparableList[T]) Index ¶
func (list ComparableList[T]) Index(value T) int
type Counter ¶
type Counter[T comparable] struct { // contains filtered or unexported fields }
A datastructure for counting comparable elements. Elements and their counts are stored in a map as key value pairs
func NewCounter ¶
func NewCounter[T comparable](values []T) Counter[T]
Returns a new counter containing the counts of the specified elements
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) fmt.Println(counter.Elements())
Output: [{apple 1} {banana 3} {orange 2}]
func (*Counter[T]) Add ¶
func (c *Counter[T]) Add(value T)
Increments the count for the specified element
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) fmt.Println(counter.Get("apple")) // Adding an element already in the counter counter.Add("apple") fmt.Println(counter.Get("apple")) // Adding an element not yet in the counter counter.Add("grape") fmt.Println(counter.Get("grape"))
Output: 1 2 1
func (*Counter[T]) Elements ¶
func (c *Counter[T]) Elements() counterElements[T]
Returns the elements and their counts in the order they were added
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) fmt.Println(counter.Elements())
Output: [{apple 1} {banana 3} {orange 2}]
func (Counter[T]) Get ¶
Returns the count of the specified element
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) fmt.Println(counter.Get("banana")) fmt.Println(counter.Get("grape"))
Output: 3 0
func (*Counter[T]) MostCommon ¶
Returns the n most common elements and their counts. If n is less than 0, returns all elements sorted by most common. Elements with they same count are returned in the order in which they were added.
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) top2 := counter.MostCommon(2) fmt.Println(top2) counter.Add("orange") // Ties are broken by the order elements were added sortedElements := counter.MostCommon(-1) fmt.Println(sortedElements)
Output: [{banana 3} {orange 2}] [{banana 3} {orange 3} {apple 1}]
func (*Counter[T]) Subtract ¶
func (c *Counter[T]) Subtract(value T)
Decrements the count of the specified element
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) // Subtracting an element in the counter counter.Subtract("banana") fmt.Println(counter.Get("banana")) // Subtracting an element not in the counter counter.Subtract("grape") fmt.Println(counter.Get("grape"))
Output: 2 -1
func (Counter[T]) Total ¶
Returns the sum of the counts all of all elements
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) fmt.Println(counter.Total())
Output: 6
func (*Counter[T]) Update ¶
func (c *Counter[T]) Update(arrs ...[]T)
Adds the counts of the elements in the provided arrays to the counter
Example ¶
fruits := []string{"apple", "banana", "banana", "banana", "orange", "orange"} counter := NewCounter(fruits) moreFruits := []string{"apple", "banana", "orange", "grape", "grape"} evenMoreFruits := []string{"apple", "banana", "pineapple"} counter.Update(moreFruits, evenMoreFruits) fmt.Println(counter.Elements())
Output: [{apple 3} {banana 5} {orange 3} {grape 2} {pineapple 1}]
type Deque ¶
type Deque[T any] struct { // contains filtered or unexported fields }
A combination of a stack and queue or "double-ended queue" with fast and memory efficient pushes and pops for both ends.
func NewDeque ¶
Returns a deque the specified minimum capacity (defaults to 1)
Example ¶
deque := NewDeque[int](8) deque.ExtendRight([]int{1, 2, 3}) fmt.Println(deque)
Output: [1 2 3]
func (Deque[T]) Capacity ¶
Returns the current capacity of the deque. This will resize automatically as elements are added or removed
Example ¶
deque := NewDeque[int](8) // Provide minimum capacity fmt.Println(deque.Capacity()) for i := 0; i < 10; i++ { deque.PushRight(i) } fmt.Println(deque.Capacity()) // Automatically grows as needed deque.Clear() fmt.Println(deque.Capacity()) // Automatically shrinks as needed
Output: 8 16 8
func (*Deque[T]) Clear ¶
func (d *Deque[T]) Clear()
Removes all elements from the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) deque.Clear() fmt.Println(deque.Len())
Output: 0
func (Deque[T]) Copy ¶
Returns a copy of the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) deque2 := deque.Copy() fmt.Println(deque, deque2)
Output: [1 2 3] [1 2 3]
func (Deque[T]) Elements ¶
func (d Deque[T]) Elements() []T
Returns an array containing all elements from the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) elements := deque.Elements() fmt.Println(elements)
Output: [1 2 3]
func (*Deque[T]) ExtendLeft ¶
func (d *Deque[T]) ExtendLeft(arr []T)
Adds the given elements to the left side of the deque. The series of left pushes results in reversing the order of given elements.
Example ¶
deque := NewDeque[int]() deque.ExtendLeft([]int{1, 2, 3}) fmt.Println(deque)
Output: [3 2 1]
func (*Deque[T]) ExtendRight ¶
func (d *Deque[T]) ExtendRight(arr []T)
Adds the given elements to the right side of the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) fmt.Println(deque)
Output: [1 2 3]
func (Deque[T]) Index ¶
Returns a slice of the queue based on the given indices. If one index is provided, an slice containing the single item at the index is returned. If two indices are provided, a slice containging the elements between the two indicies is returned.
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3, 4, 5}) // Sindle index fmt.Println(deque.Index(2)) // Two indices fmt.Println(deque.Index(1, 4))
Output: [3] [2 3 4]
func (*Deque[T]) PeekLeft ¶
func (d *Deque[T]) PeekLeft() T
Returns the first element (leftmost) in the deque. Panics if called on an empty deque.
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) head := deque.PeekLeft() fmt.Println(head)
Output: 1
func (*Deque[T]) PeekRight ¶
func (d *Deque[T]) PeekRight() T
Returns the last element (rightmost) of the deque. Panics if called on an empty deque.
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) tail := deque.PeekRight() fmt.Println(tail)
Output: 3
func (*Deque[T]) PopLeft ¶
func (d *Deque[T]) PopLeft() T
Removes the first element (leftmost) from the deque and returns it. Panics if called on an empty deque.
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) popped := deque.PopLeft() fmt.Println(popped) fmt.Println(deque)
Output: 1 [2 3]
func (*Deque[T]) PopRight ¶
func (d *Deque[T]) PopRight() T
Removes the last element (rightmost) from the deque and returns it. Panics if called on an empty deque.
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) popped := deque.PopRight() fmt.Println(popped) fmt.Println(deque)
Output: 3 [1 2]
func (*Deque[T]) PushLeft ¶
func (d *Deque[T]) PushLeft(value T)
Adds an element to the beginning of the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) deque.PushLeft(4) fmt.Println(deque)
Output: [4 1 2 3]
func (*Deque[T]) PushRight ¶
func (d *Deque[T]) PushRight(value T)
Adds an element to the end of the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) deque.PushRight(4) fmt.Println(deque)
Output: [1 2 3 4]
func (*Deque[T]) Reverse ¶
func (d *Deque[T]) Reverse()
Reverses the order of the elements in the deque
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) fmt.Println(deque) deque.Reverse() fmt.Println(deque)
Output: [1 2 3] [3 2 1]
func (*Deque[T]) Rotate ¶
Rotate the deque n elements to the right. If n is negative, the deque is rotated to the left.
Example ¶
deque := NewDeque[int]() deque.ExtendRight([]int{1, 2, 3}) fmt.Println(deque) deque.Rotate(2) fmt.Println(deque) deque.Rotate(-2) fmt.Println(deque)
Output: [1 2 3] [3 1 2] [1 2 3]
type Dict ¶
type Dict[K comparable, V any] map[K]V
A wrapper around a map with several convenience methods
func (Dict[K, V]) Clear ¶
func (dict Dict[K, V]) Clear()
Removes all elements from the dictionary
Example ¶
d := Dict[int, string]{1: "foo"} d.Clear() fmt.Println(d.Items())
Output: []
func (Dict[K, V]) Copy ¶
Returns a copy of the dictionary
Example ¶
d := Dict[int, string]{1: "foo"} d2 := d.Copy() fmt.Println(d2.Items())
Output: [{1 foo}]
func (Dict[K, V]) Get ¶
func (dict Dict[K, V]) Get(key K, fallback ...V) V
Returns the value associated with given key. In the case the the key is not present, a fallback value is returned if provided. Otherwise the zero-value for the value type is returned.
Example ¶
d := Dict[int, string]{1: "foo"} fmt.Println(d.Get(1)) fmt.Println(d.Get(2)) fmt.Println(d.Get(2, "bar"))
Output: foo bar
func (Dict[K, V]) Has ¶
Returns true if the dictionary contains the given key.
Example ¶
d := Dict[int, string]{1: "foo"} fmt.Println(d.Has(1)) fmt.Println(d.Has(2))
Output: true false
func (Dict[K, V]) Items ¶
Returns an array of dictionary items (a struct with Key and Value fields)
Example ¶
d := Dict[int, string]{ 1: "foo", 2: "bar", 3: "baz", } items := d.Items() sort.Slice(items, func(i, j int) bool { return items[i].Key < items[j].Key }) fmt.Println(items)
Output: [{1 foo} {2 bar} {3 baz}]
func (Dict[K, V]) Keys ¶
func (dict Dict[K, V]) Keys() []K
Returns an array of keys present in the dictionary
Example ¶
d := Dict[int, string]{ 1: "foo", 2: "bar", 3: "baz", } keys := d.Keys() sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) fmt.Println(keys)
Output: [1 2 3]
func (Dict[K, V]) Pop ¶
Removes the given key from the dictionary and returns it's associated value. If the key is not present in the dictionary, a fallback is returned if provided. Otherwise a zero-value is returned. Returns a second boolean value which is true if the key was present in the dictionary.
Example ¶
d := Dict[int, string]{ 1: "foo", 2: "bar", 3: "baz", } popped, ok := d.Pop(1) fmt.Println(popped, ok) fmt.Println(d.Has(1)) fmt.Println(len(d)) popped2, ok2 := d.Pop(4, "fallback") fmt.Println(popped2, ok2) popped3, ok3 := d.Pop(5) fmt.Println(popped3, ok3)
Output: foo true false 2 fallback false false
func (Dict[K, V]) SetDefault ¶
func (dict Dict[K, V]) SetDefault(key K, d V) V
Sets the value for the given key is the key is not already present. Returns the value for the given key.
Example ¶
d := Dict[int, string]{ 1: "foo", 2: "bar", } d2 := Dict[int, string]{ 2: "baz", 3: "bar", } d.Update(d2) items := d.Items() sort.Slice(items, func(i, j int) bool { return items[i].Key < items[j].Key }) fmt.Println(items)
Output: [{1 foo} {2 baz} {3 bar}]
func (Dict[K, V]) Values ¶
func (dict Dict[K, V]) Values() []V
Returns an array of the values of the dictionary
Example ¶
d := Dict[int, string]{ 1: "foo", 2: "bar", 3: "baz", } values := d.Values() sort.Slice(values, func(i, j int) bool { return values[i] < values[j] }) fmt.Println(values)
Output: [bar baz foo]
type DictItem ¶
type DictItem[K comparable, V any] struct { Key K Value V }
type Number ¶
type Number interface { constraints.Integer | constraints.Float | constraints.Complex }
type Set ¶
type Set[T comparable] map[T]struct{}
A data structure for storing unique values without any particular order
func NewSet ¶
func NewSet[T comparable](values ...T) Set[T]
Returns a new set containing the values provided
Example ¶
set := NewSet(1, 2, 3, 3) // Values are unique fmt.Println(len(set))
Output: 3
func (Set[T]) Add ¶
func (set Set[T]) Add(values ...T)
Adds element(s) to the set
Example ¶
set := NewSet(1, 2, 3) set.Add(4) fmt.Println(set.Has(4)) set.Add(5, 6, 7) expected := NewSet(1, 2, 3, 4, 5, 6, 7) fmt.Println(expected.Equals(set))
Output: true true
func (Set[T]) Clear ¶
func (set Set[T]) Clear()
Removes all the elements from the set
Example ¶
set := NewSet(1, 2, 3) set.Clear() fmt.Println(set.Members())
Output: []
func (Set[T]) Copy ¶
Returns a copy of the set
Example ¶
set1 := NewSet(1, 2, 3) set2 := set1.Copy() fmt.Println(set1.Equals(set2))
Output: true
func (Set[T]) Difference ¶
Returns a set containing the difference between two or more sets
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) difference := set1.Difference(set2) expected := NewSet(1) fmt.Println(difference.Equals(expected))
Output: true
func (Set[T]) DifferenceUpdate ¶
Removes the items in this set that are also included in one or more other sets
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) set1.DifferenceUpdate(set2) expected := NewSet(1) fmt.Println(expected.Equals(set1))
Output: true
func (Set[T]) Discard ¶
func (set Set[T]) Discard(value T)
Remove the specified item. If the item is not present this is a noop
Example ¶
set := NewSet(1, 2, 3) set.Discard(1) fmt.Println(set.Has(1))
Output: false
func (Set[T]) Equals ¶
Returns true if the sets contain the same members regardless of order
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) fmt.Println(set1.Equals(set2)) set1.Add(4) set2.Add(1) fmt.Println(set1.Equals(set2))
Output: false true
func (Set[T]) Has ¶
Returns true if the set contins the specified element
Example ¶
set := NewSet(1, 2, 3) fmt.Println(set.Has(1)) fmt.Println(set.Has(4))
Output: true false
func (Set[T]) Intersection ¶
Returns a set, that is the intersection of two or more sets
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) intersection := set1.Intersection(set2) expected := NewSet(2, 3) fmt.Println(intersection.Equals(expected))
Output: true
func (Set[T]) IntersectionUpdate ¶
Removes the items in this set that are not present in other, specified set(s)
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) set1.IntersectionUpdate(set2) expected := NewSet(2, 3) fmt.Println(set1.Equals(expected))
Output: true
func (Set[T]) IsDisjoint ¶
Returns true if the sets have no common elements
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(4, 5, 6) fmt.Println(set1.IsDisjoint(set2)) set1.Add(4) fmt.Println(set1.IsDisjoint(set2))
Output: true false
func (Set[T]) IsSubset ¶
Returns true if the set is contained by the given set
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(1, 2, 3, 4) fmt.Println(set1.IsSubset(set2)) set1.Add(5) fmt.Println(set1.IsSubset(set2))
Output: true false
func (Set[T]) IsSuperset ¶
Returns true is the set contains the given set
Example ¶
set1 := NewSet(1, 2, 3, 4) set2 := NewSet(2, 3, 4) fmt.Println(set1.IsSuperset(set2)) set2.Add(5) fmt.Println(set1.IsSuperset(set2))
Output: true false
func (Set[T]) Members ¶
func (set Set[T]) Members() []T
Returns the elements contained by the set. Elements are unordered.
Example ¶
set := NewSet(1, 2, 3) members := set.Members() sortable := sort.IntSlice(members) sortable.Sort() fmt.Println(sortable)
Output: [1 2 3]
func (Set[T]) Pop ¶
Returns an element from the set and an error if the set is empty
Example ¶
set := NewSet(1) val, err := set.Pop() fmt.Println(val, err, len(set)) val2, err2 := set.Pop() fmt.Println(val2, err2, len(set))
Output: 1 <nil> 0 0 cannot pop item from an empty set 0
func (Set[T]) Remove ¶
Removes the specified element from the set. Returns true if the element was in the set
Example ¶
set := NewSet(1, 2, 3) ok := set.Remove(3) fmt.Println(ok) fmt.Println(set.Has(3)) ok2 := set.Remove(4) fmt.Println(ok2)
Output: true false false
func (Set[T]) SymmetricDifference ¶
Returns a set that contains all items from both sets, except items that are present in both sets
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) symmetricDifference := set1.SymmetricDifference(set2) expected := NewSet(1, 4) fmt.Println(expected.Equals(symmetricDifference))
Output: true
func (Set[T]) SymmetricDifferenceUpdate ¶
Removes the items that are present in both sets, and inserts the items that are not present in both sets
Example ¶
set1 := NewSet(1, 2, 3) set2 := NewSet(2, 3, 4) set1.SymmetricDifferenceUpdate(set2) expected := NewSet(1, 4) fmt.Println(expected.Equals(set1))
Output: true