Documentation ¶
Index ¶
- type Container
- type Deque
- func (d *Deque[T]) Add(t T)
- func (d *Deque[T]) AddAll(t ...T)
- func (d *Deque[T]) AddLeft(t T)
- func (d *Deque[T]) AddRight(t T)
- func (d *Deque[T]) Iterator() Iterator[T]
- func (d *Deque[T]) Len() (length int)
- func (d *Deque[T]) Peek() (value T)
- func (d *Deque[T]) PeekLeft() (value T)
- func (d *Deque[T]) PeekRight() (value T)
- func (d *Deque[T]) Remove() (value T)
- func (d *Deque[T]) RemoveLeft() (value T)
- func (d *Deque[T]) RemoveRight() (value T)
- func (d *Deque[T]) Values() (values GSlice[T])
- type GMap
- func (m GMap[K, V]) All(predicate genfuncs.Function[V, bool]) (ok bool)
- func (m GMap[K, V]) Any(predicate genfuncs.Function[V, bool]) (ok bool)
- func (m GMap[K, V]) Contains(key K) (isTrue bool)
- func (m GMap[K, V]) Delete(key K)
- func (m GMap[K, V]) Filter(predicate genfuncs.Function[V, bool]) (result GMap[K, V])
- func (m GMap[K, V]) FilterKeys(predicate genfuncs.Function[K, bool]) (result GMap[K, V])
- func (m GMap[K, V]) ForEach(action func(k K, v V))
- func (m GMap[K, V]) Get(key K) (v V, ok bool)
- func (m GMap[K, V]) GetOrElse(k K, defaultValue func() V) (value V)
- func (m GMap[K, V]) Iterator() Iterator[V]
- func (m GMap[K, V]) Keys() (keys GSlice[K])
- func (m GMap[K, V]) Len() (length int)
- func (m GMap[K, V]) Put(key K, value V)
- func (m GMap[K, V]) Values() (values GSlice[V])
- type GSlice
- func (s GSlice[T]) Filter(predicate genfuncs.Function[T, bool]) GSlice[T]
- func (s GSlice[T]) ForEach(action func(i int, t T))
- func (s GSlice[T]) Iterator() Iterator[T]
- func (s GSlice[T]) Len() int
- func (s GSlice[T]) Random() (t T)
- func (s GSlice[T]) SortBy(order genfuncs.BiFunction[T, T, bool]) (sorted GSlice[T])
- func (s GSlice[T]) Swap(i, j int)
- func (s GSlice[T]) Values() (values GSlice[T])
- type HasValues
- type Heap
- type Iterator
- type List
- func (l *List[T]) Add(value T)
- func (l *List[T]) AddAll(values ...T)
- func (l *List[T]) AddLeft(value T) (e *ListElement[T])
- func (l *List[T]) AddRight(v T) (e *ListElement[T])
- func (l *List[T]) ForEach(action func(value T))
- func (l *List[T]) Iterator() Iterator[T]
- func (l *List[T]) Len() (length int)
- func (l *List[T]) PeekLeft() (e *ListElement[T])
- func (l *List[T]) PeekRight() (e *ListElement[T])
- func (l *List[T]) Remove(e *ListElement[T]) (t T)
- func (l *List[T]) SortBy(order genfuncs.BiFunction[T, T, bool]) (result *List[T])
- func (l *List[T]) Values() (values GSlice[T])
- type ListElement
- type Map
- type MapSet
- type Queue
- type Sequence
- type Set
- type SyncMap
- func (s *SyncMap[K, V]) Contains(key K) (contains bool)
- func (s *SyncMap[K, V]) Delete(key K)
- func (s *SyncMap[K, V]) ForEach(f func(key K, value V))
- func (s *SyncMap[K, V]) Get(key K) (value V, ok bool)
- func (s *SyncMap[K, V]) GetAndDelete(key K) (value V, ok bool)
- func (s *SyncMap[K, V]) GetOrPut(key K, value V) (actual V, ok bool)
- func (s *SyncMap[K, V]) Iterator() Iterator[V]
- func (s *SyncMap[K, V]) Keys() (keys GSlice[K])
- func (s *SyncMap[K, V]) Len() (length int)
- func (s *SyncMap[K, V]) Put(key K, value V)
- func (s *SyncMap[K, V]) Values() (values GSlice[V])
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶ added in v0.11.1
type Container[T any] interface { HasValues[T] // Add an element to the Container. Add(t T) // AddAll elements to the Container. AddAll(t ...T) }
Container is a minimal container that HasValues and accepts additional elements.
type Deque ¶
type Deque[T any] struct { // contains filtered or unexported fields }
Deque is a doubly ended implementation of Queue with default behavior of a Fifo but provides left and right access. Employs a List for storage.
func (*Deque[T]) AddAll ¶
func (d *Deque[T]) AddAll(t ...T)
AddAll elements to the right of the Deque.
func (*Deque[T]) AddLeft ¶
func (d *Deque[T]) AddLeft(t T)
AddLeft an element to the left of the Deque.
func (*Deque[T]) AddRight ¶
func (d *Deque[T]) AddRight(t T)
AddRight an element to the right of the Deque.
func (*Deque[T]) Peek ¶
func (d *Deque[T]) Peek() (value T)
Peek returns the left most element in the Deque without removing it.
func (*Deque[T]) PeekLeft ¶
func (d *Deque[T]) PeekLeft() (value T)
PeekLeft returns the left most element in the Deque without removing it.
func (*Deque[T]) PeekRight ¶
func (d *Deque[T]) PeekRight() (value T)
PeekRight returns the right most element in the Deque without removing it.
func (*Deque[T]) Remove ¶
func (d *Deque[T]) Remove() (value T)
Remove and return the left most element in the Deque.
func (*Deque[T]) RemoveLeft ¶
func (d *Deque[T]) RemoveLeft() (value T)
RemoveLeft and return the left most element in the Deque.
func (*Deque[T]) RemoveRight ¶
func (d *Deque[T]) RemoveRight() (value T)
RemoveRight and return the right most element in the Deque.
type GMap ¶
type GMap[K comparable, V any] map[K]V
GMap is a generic type employing the standard Go map and implementation Map.
func (GMap[K, V]) All ¶ added in v0.10.0
All returns true if all values in GMap satisfy the predicate.
func (GMap[K, V]) Any ¶ added in v0.10.0
Any returns true if any values in GMap satisfy the predicate.
func (GMap[K, V]) Contains ¶
Contains returns true if the GMap contains the given key.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs/container" ) var wordPositions = container.GMap[string, int]{"hello": 1, "world": 2} func main() { fmt.Println(wordPositions.Contains("hello")) fmt.Println(wordPositions.Contains("no")) }
Output: true false
func (GMap[K, V]) Delete ¶ added in v0.13.0
func (m GMap[K, V]) Delete(key K)
Delete an entry in the GMap.
func (GMap[K, V]) Filter ¶ added in v0.10.0
Filter a GMap by a predicate, returning a new GMap that contains only values that satisfy the predicate.
func (GMap[K, V]) FilterKeys ¶ added in v0.10.0
FilterKeys returns a new GMap that contains only values whose key satisfy the predicate.
func (GMap[K, V]) ForEach ¶ added in v0.11.0
func (m GMap[K, V]) ForEach(action func(k K, v V))
ForEach performs the given action on each entry in the GMap.
func (GMap[K, V]) Get ¶ added in v0.13.0
Get returns an entry from the Map. The returned bool indicates if the key is in the Map.
func (GMap[K, V]) GetOrElse ¶ added in v0.12.1
func (m GMap[K, V]) GetOrElse(k K, defaultValue func() V) (value V)
GetOrElse returns the value at the given key if it exists or returns the result of defaultValue.
func (GMap[K, V]) Iterator ¶ added in v0.18.0
Iterator creates an iterator for the values in the GMap.
func (GMap[K, V]) Keys ¶
Keys return a GSlice containing the keys of the GMap.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs" "github.com/nwillc/genfuncs/container" ) var wordPositions = container.GMap[string, int]{"hello": 1, "world": 2} func main() { fmt.Println(wordPositions.Keys().SortBy(genfuncs.OrderedLess[string])) }
Output: [hello world]
func (GMap[K, V]) Put ¶ added in v0.13.0
func (m GMap[K, V]) Put(key K, value V)
Put a key and value in the Map.
func (GMap[K, V]) Values ¶
Values returns a GSlice of all the values in the GMap.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs/container" ) var wordPositions = container.GMap[string, int]{"hello": 1, "world": 2} func main() { wordPositions.Values().ForEach(func(_, i int) { fmt.Println(i) }) }
Output: 1 2
type GSlice ¶
type GSlice[T any] []T
GSlice is a generic type corresponding to a standard Go slice that implements HasValues.
func (GSlice[T]) Filter ¶
Filter returns a slice containing only elements matching the given predicate.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs" "github.com/nwillc/genfuncs/container" ) var isGreaterThanZero = genfuncs.OrderedGreaterThan(0) func main() { var values container.GSlice[int] = []int{1, -2, 2, -3} values.Filter(isGreaterThanZero).ForEach(func(_, i int) { fmt.Println(i) }) }
Output: 1 2
func (GSlice[T]) ForEach ¶
ForEach element of the GSlice invoke given function with the element. Syntactic sugar for a range that intends to traverse all the elements, i.e. no exiting midway through.
func (GSlice[T]) Iterator ¶ added in v0.18.0
Iterator returns an Iterator that will iterate over the GSlice.
func (GSlice[T]) Random ¶
func (s GSlice[T]) Random() (t T)
Random returns a random element of the GSlice.
func (GSlice[T]) SortBy ¶
func (s GSlice[T]) SortBy(order genfuncs.BiFunction[T, T, bool]) (sorted GSlice[T])
SortBy copies a slice, sorts the copy applying the Ordered and returns it. This is not a pure function, the GSlice is sorted in place, the returned slice is to allow for fluid calls in chains.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs" "github.com/nwillc/genfuncs/container" ) func main() { var numbers container.GSlice[int] = []int{1, 0, 9, 6, 0} fmt.Println(numbers) fmt.Println(numbers.SortBy(genfuncs.OrderedLess[int])) }
Output: [1 0 9 6 0] [0 0 1 6 9]
func (GSlice[T]) Swap ¶
Swap two values in the slice.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs" "github.com/nwillc/genfuncs/container" ) var words container.GSlice[string] = []string{"hello", "world"} func main() { words = words.SortBy(genfuncs.OrderedLess[string]) words.Swap(0, 1) fmt.Println(words) }
Output: [world hello]
type HasValues ¶ added in v0.11.1
type HasValues[T any] interface { // Len returns length of the Container. Len() int // Values returns a copy of the current values in the Container without modifying the contents. Values() GSlice[T] }
HasValues is an interface that indicates a struct contains values that can counted and be retrieved.
type Heap ¶
type Heap[T any] struct { // contains filtered or unexported fields }
Heap implements an ordered heap of any type which can be min heap or max heap depending on the compare provided. Heap implements Queue.
func NewHeap ¶
func NewHeap[T any](compare genfuncs.BiFunction[T, T, bool], values ...T) (heap *Heap[T])
NewHeap return a heap ordered based on the compare and adds any values provided.
Example ¶
package main import ( "fmt" "github.com/nwillc/genfuncs" "github.com/nwillc/genfuncs/container" ) func main() { heap := container.NewHeap[int](genfuncs.OrderedLess[int], 3, 1, 4, 2) for heap.Len() > 0 { fmt.Print(heap.Remove()) } fmt.Println() }
Output: 1234
type Iterator ¶ added in v0.18.0
func NewListIterator ¶ added in v0.18.0
func NewSliceIterator ¶ added in v0.18.0
func NewValuesIterator ¶ added in v0.18.0
type List ¶ added in v0.13.1
type List[T any] struct { // contains filtered or unexported fields }
List is a doubly linked list, inspired by list.List but reworked to be generic. List implements Container.
func NewList ¶ added in v0.13.1
func NewList[T any](values ...T) (l *List[T])
NewList instantiates a new List containing any values provided.
func (*List[T]) Add ¶ added in v0.13.1
func (l *List[T]) Add(value T)
Add a value to the right of the List.
func (*List[T]) AddAll ¶ added in v0.13.1
func (l *List[T]) AddAll(values ...T)
AddAll values to the right of the List.
func (*List[T]) AddLeft ¶ added in v0.13.1
func (l *List[T]) AddLeft(value T) (e *ListElement[T])
AddLeft adds a value to the left of the List.
func (*List[T]) AddRight ¶ added in v0.13.1
func (l *List[T]) AddRight(v T) (e *ListElement[T])
AddRight adds a value to the right of the List.
func (*List[T]) ForEach ¶ added in v0.13.2
func (l *List[T]) ForEach(action func(value T))
ForEach invokes the action for each value in the list.
func (*List[T]) Iterator ¶ added in v0.18.0
func (l *List[T]) Iterator() Iterator[T]
Iterator creates an Iterator for the List.
func (*List[T]) Len ¶ added in v0.13.1
func (l *List[T]) Len() (length int)
Len returns the number of values in the List.
func (*List[T]) PeekLeft ¶ added in v0.13.1
func (l *List[T]) PeekLeft() (e *ListElement[T])
PeekLeft returns the leftmost value in the List or nil if empty.
func (*List[T]) PeekRight ¶ added in v0.13.1
func (l *List[T]) PeekRight() (e *ListElement[T])
PeekRight returns the rightmost value in the List or nil if empty.
func (*List[T]) Remove ¶ added in v0.13.1
func (l *List[T]) Remove(e *ListElement[T]) (t T)
Remove removes a given value from the List.
func (*List[T]) SortBy ¶ added in v0.13.2
func (l *List[T]) SortBy(order genfuncs.BiFunction[T, T, bool]) (result *List[T])
SortBy sorts the List by the order of the order function. This is not a pure function, the List is sorted, the List returned is to allow for fluid call chains. List does not provide efficient indexed access so a Bubble sort is employed.
type ListElement ¶ added in v0.13.2
type ListElement[T any] struct { Value T // contains filtered or unexported fields }
ListElement is an element of List.
func (*ListElement[T]) Next ¶ added in v0.13.2
func (e *ListElement[T]) Next() (next *ListElement[T])
Next returns the next list element or nil.
func (*ListElement[T]) Prev ¶ added in v0.13.2
func (e *ListElement[T]) Prev() (prev *ListElement[T])
Prev returns the previous list element or nil.
func (*ListElement[T]) Swap ¶ added in v0.14.1
func (e *ListElement[T]) Swap(e2 *ListElement[T])
Swap the values of two ListElements.
type Map ¶
type Map[K comparable, V any] interface { HasValues[V] Sequence[V] Contains(key K) bool Delete(key K) Get(key K) (value V, ok bool) Put(key K, value V) ForEach(f func(key K, value V)) Keys() GSlice[K] }
Map interface to provide a polymorphic and generic interface to map implementations.
type MapSet ¶
type MapSet[T comparable] struct { // contains filtered or unexported fields }
MapSet is a Set implementation based on a map. MapSet implements Set.
func (*MapSet[T]) Iterator ¶ added in v0.19.1
Iterator returns an Iterator of the current state of the MapSet. This creates a copy of the data.
type Queue ¶
type Queue[T any] interface { Container[T] // Peek returns the next element without removing it. Peek() T Remove() T }
Queue is a container providing some define order when accessing elements. Queue implements Container.
type Sequence ¶ added in v0.18.0
func NewIteratorSequence ¶ added in v0.18.0
type Set ¶
type Set[T comparable] interface { Container[T] Sequence[T] // Contains returns true if the Set contains a given element. Contains(t T) bool // Remove the element from the Set. Remove(T) }
Set is a Container that contains no duplicate elements.
func NewMapSet ¶
func NewMapSet[T comparable](t ...T) (set Set[T])
NewMapSet returns a new Set containing given values.
type SyncMap ¶ added in v0.13.0
SyncMap is a Map implementation employing sync.Map and is therefore GoRoutine safe.
func NewSyncMap ¶ added in v0.13.0
NewSyncMap creates a new SyncMap instance.
func (*SyncMap[K, V]) Contains ¶ added in v0.13.0
Contains returns true if the Map contains the given key.
func (*SyncMap[K, V]) Delete ¶ added in v0.13.0
func (s *SyncMap[K, V]) Delete(key K)
Delete an entry from the Map.
func (*SyncMap[K, V]) ForEach ¶ added in v0.13.0
func (s *SyncMap[K, V]) ForEach(f func(key K, value V))
ForEach traverses the Map applying the given function to all entries. The sync.Map's any types are cast to the appropriate types.
func (*SyncMap[K, V]) Get ¶ added in v0.13.0
Get the value for the key. The sync.Map any type to cast to the appropriate type. The returned ok value will be false if the map is not contained in the Map.
func (*SyncMap[K, V]) GetAndDelete ¶ added in v0.13.0
GetAndDelete returns the value from the SyncMap corresponding to the key, returning it, and deletes it.
func (*SyncMap[K, V]) GetOrPut ¶ added in v0.13.0
GetOrPut returns the existing value for the key if present. Otherwise, it puts and returns the given value. The ok result is true if the value was present, false if put.
func (*SyncMap[K, V]) Iterator ¶ added in v0.19.1
Iterator returns an iterator over a snapshot of the current values.
func (*SyncMap[K, V]) Keys ¶ added in v0.13.0
Keys returns the keys in the Map by traversing it and casting the sync.Map's any to the appropriate type.
func (*SyncMap[K, V]) Len ¶ added in v0.13.0
Len returns the element count. This requires a traversal of the Map.