godino

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: BSD-2-Clause Imports: 7 Imported by: 0

README

Godino

Tests Go Reference Go Report

Helper functions and datastructures inspired by other languages, mainly python.

Documentation

Overview

A package of helper functions and datastructures inspired by other languages, mainly python

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrAssertion error = errors.New("assertion failed")

Functions

func All

func All(conditions ...bool) bool

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

func Any(conditions ...bool) bool

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 Append

func Append[L ~[]T, T any](arr *L, value T)

func Assert

func Assert(condition bool, messages ...string)

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 Clear

func Clear[L ~[]T, T any](arr *L)

func Combinations

func Combinations[T any](arr []T, length int) <-chan []T

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 Copy

func Copy[L ~[]T, T any](arr L) L

func Count

func Count[L ~[]T, T comparable](arr L, value T) int

func Every

func Every[L ~[]T, T any](arr L, f func(T) bool) bool

func Extend

func Extend[L ~[]T, T any](arr *L, items []T)

func Filter

func Filter[L ~[]T, T any](arr L, condition func(T) bool) []T

func Find

func Find[L ~[]T, T any](arr L, condition func(T) bool) (value T, found bool)

func ForEach

func ForEach[L ~[]T, T any](arr L, f func(index int, value T))

func ForEachRef

func ForEachRef[L ~[]T, T any](arr L, f func(index int, value *T))

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 Insert

func Insert[L ~[]T, T any](arr *L, value T, index int)

func Map

func Map[L ~[]T, T any, V any](arr L, f func(T) V) []V

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

func NumPermutations[T any](arr []T) int

Returns the number of permutations of the array

Example
nums := []int{1, 2, 3, 4, 5}
fmt.Println(NumPermutations(nums))
Output:

120

func Permutations

func Permutations[T any](arr []T, length int) <-chan []T

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 Pop

func Pop[L ~[]T, T any](arr *L) T

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:

func Reduce

func Reduce[L ~[]T, T any, V any](arr L, f func(V, T) V, acc V) V

func Remove

func Remove[L ~[]T, T any](arr *L, index int) T

func Reverse

func Reverse[L ~[]T, T any](arr *L)

func Shift

func Shift[L ~[]T, T any](arr *L) T

func Some

func Some[L ~[]T, T any](arr L, f func(T) bool) bool

func Sum

func Sum[T Number](numbers ...T) T

Adds the given numeric arguments and returns the sum

Example
sum := Sum(1, 23, 100, -5, 10)
fmt.Println(sum)
Output:

129

func UnShift

func UnShift[L ~[]T, T any](arr *L, value T)

func ValueAt

func ValueAt[L ~[]T, T any](arr L, index int) T

func Zip

func Zip[L ~[]T, T any](arrs ...[]T) ([][]T, error)

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

func (c Counter[T]) Get(value T) int

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

func (c *Counter[T]) MostCommon(n int) counterElements[T]

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]) String

func (c Counter[T]) String() string

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

func (c Counter[T]) Total() int

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

func NewDeque[T any](capacity ...int) *Deque[T]

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

func (d Deque[T]) Capacity() int

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

func (d Deque[T]) Copy() *Deque[T]

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

func (d Deque[T]) Index(nums ...int) []T

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]) Len

func (d *Deque[T]) Len() int

Returns the number of elements in the deque

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

func (d *Deque[T]) Rotate(n int)

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]

func (Deque[T]) String

func (d Deque[T]) String() string

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

func (dict Dict[K, V]) Copy() Dict[K, V]

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

func (dict Dict[K, V]) Has(key K) bool

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

func (dict Dict[K, V]) Items() []DictItem[K, V]

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

func (dict Dict[K, V]) Pop(key K, fallback ...V) (V, bool)

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]) Update

func (dict1 Dict[K, V]) Update(dict2 Dict[K, V])

Updates the keys and values from the given dictionary

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 List

type List[T any] []T

func (*List[T]) Append

func (list *List[T]) Append(value T)

func (List[T]) At

func (list List[T]) At(index int) T

func (*List[T]) Clear

func (list *List[T]) Clear()

func (List[T]) Copy

func (list List[T]) Copy() List[T]

func (*List[T]) Extend

func (list *List[T]) Extend(items []T)

func (List[T]) Find

func (list List[T]) Find(f func(T) bool) (value T, found bool)

func (List[T]) ForEach

func (list List[T]) ForEach(f func(int, T))

func (List[T]) ForEachRef

func (list List[T]) ForEachRef(f func(int, *T))

func (*List[T]) Insert

func (list *List[T]) Insert(value T, index int)

func (List[T]) Map

func (list List[T]) Map(f func(T) any) []any

func (*List[T]) Pop

func (list *List[T]) Pop() T

func (List[T]) Reduce

func (list List[T]) Reduce(f func(any, T) any, acc any) any

func (*List[T]) Remove

func (list *List[T]) Remove(index int) T

func (*List[T]) Reverse

func (list *List[T]) Reverse()

func (*List[T]) Shift

func (list *List[T]) Shift() T

func (List[T]) Some

func (list List[T]) Some(f func(T) bool) bool

func (*List[T]) UnShift

func (list *List[T]) UnShift(value T)

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

func (set Set[T]) Copy() Set[T]

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

func (set Set[T]) Difference(sets ...Set[T]) Set[T]

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

func (set Set[T]) DifferenceUpdate(sets ...Set[T])

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

func (set Set[T]) Equals(set2 Set[T]) bool

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

func (set Set[T]) Has(value T) bool

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

func (set Set[T]) Intersection(sets ...Set[T]) Set[T]

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

func (set Set[T]) IntersectionUpdate(sets ...Set[T])

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

func (set1 Set[T]) IsDisjoint(set2 Set[T]) bool

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

func (set1 Set[T]) IsSubset(set2 Set[T]) bool

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

func (set1 Set[T]) IsSuperset(set2 Set[T]) bool

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

func (set Set[T]) Pop() (T, error)

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

func (set Set[T]) Remove(value T) bool

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]) String

func (set Set[T]) String() string

func (Set[T]) SymmetricDifference

func (set1 Set[T]) SymmetricDifference(set2 Set[T]) Set[T]

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

func (set Set[T]) SymmetricDifferenceUpdate(sets ...Set[T])

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

func (Set[T]) Union

func (set1 Set[T]) Union(set2 Set[T]) Set[T]

Return a set that contains all items from both sets

Example
set1 := NewSet(1, 2, 3)
set2 := NewSet(3, 4, 5)
union := set1.Union(set2)
expected := NewSet(1, 2, 3, 4, 5)
fmt.Println(expected.Equals(union))
Output:

func (Set[T]) Update

func (set Set[T]) Update(sets ...Set[T])

Adds all the items from the given sets

Example
set1 := NewSet(1, 2, 3)
set2 := NewSet(3, 4, 5)
set1.Update(set2)
expected := NewSet(1, 2, 3, 4, 5)
fmt.Println(expected.Equals(set1))
Output:

Jump to

Keyboard shortcuts

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