Documentation
¶
Overview ¶
Package generic provides types, interfaces, and functions to support generic programming use cases.
Index ¶
- func AllMatch[T any](s []T, p Predicate1[T]) bool
- func AnyMatch[T any](s []T, p Predicate1[T]) bool
- func Collect1[T any](seq iter.Seq[T]) []T
- func Contains[T any](s []T, eq EqualFunc[T], vals ...T) bool
- func Find[T any](s []T, eq EqualFunc[T], val T) int
- func FirstMatch[T any](s []T, p Predicate1[T]) (T, bool)
- func PartitionMatch[T any](s []T, p Predicate1[T]) ([]T, []T)
- func SelectMatch[T any](s []T, p Predicate1[T]) []T
- func Transform[T, U any](s []T, f func(T) U) []U
- type Cloner
- type Collection1
- type Collection2
- type CompareFunc
- type Comparer
- type EqualFunc
- type Equaler
- type KeyValue
- type Predicate1
- type Predicate2
- type TraverseOrder
- type Tree1
- type Tree2
- type VisitFunc1
- type VisitFunc2
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllMatch ¶ added in v0.10.3
func AllMatch[T any](s []T, p Predicate1[T]) bool
AllMatch returns true if all elements in a slice satisfy the provided predicate function. If a slice is empty, it returns true.
func AnyMatch ¶ added in v0.10.3
func AnyMatch[T any](s []T, p Predicate1[T]) bool
AnyMatch returns true if at least one element in a slice satisfies the provided predicate function. If no elements satisfy the predicate or a slice is empty, it returns false.
func Collect1 ¶ added in v0.10.1
Collect1 collects items in a collection from seq into a new slice and returns it.
func Contains ¶ added in v0.10.3
Contains checks if a slice contains all the specified values. It returns true if all values are found in the slice, otherwise returns false.
func Find ¶ added in v0.10.3
Find searches for the index of the first occurrence of a value in a slice. If the value exists in the slice, it returns its index; otherwise, it returns -1.
func FirstMatch ¶ added in v0.10.6
func FirstMatch[T any](s []T, p Predicate1[T]) (T, bool)
FirstMatch returns the first element in a slice that satisfies the given predicate. If no match is found, it returns the zero value of T and false.
func PartitionMatch ¶ added in v0.11.0
func PartitionMatch[T any](s []T, p Predicate1[T]) ([]T, []T)
PartitionMatch partitions the elements in a slice into two separate slices based on the provided predicate. The first slice contains the elements that satisfy the predicate (matched elements), while the second slice contains those that do not satisfy the predicate (unmatched elements). Both slices are of the same type as the original slice.
func SelectMatch ¶ added in v0.10.3
func SelectMatch[T any](s []T, p Predicate1[T]) []T
SelectMatch selects a subset of elements from a slice that satisfy the given predicate. It returns a new slice containing the matching elements, of the same type as the original slice.
Types ¶
type Cloner ¶ added in v0.9.2
type Cloner[T any] interface { Clone() T }
Cloner is an interface that defines a method for cloning an object (the prototype pattern).
type Collection1 ¶ added in v0.8.0
type Collection1[T any] interface { // Size returns the number of items in the collection. Size() int // IsEmpty returns true if the collection contains no items. IsEmpty() bool // Add adds the specified items to the collection. // The behavior when adding an existing item depends on the implementation. Add(...T) // Remove removes the specified items from the collection. // If an item does not exist, it is ignored. Remove(...T) // RemoveAll removes all items from the collection, leaving it empty. RemoveAll() // Contains returns true if the collection includes all the specified items. Contains(...T) bool // All returns an iterator sequence containing all the items in the collection. // This allows for iterating over the entire collection using the range keyword. All() iter.Seq[T] // AnyMatch returns true if at least one item in the collection satisfies the provided predicate function. // If no items satisfy the predicate or the collection is empty, it returns false. AnyMatch(Predicate1[T]) bool // AllMatch returns true if all items in the collection satisfy the provided predicate function. // If the collection is empty, it returns true. AllMatch(Predicate1[T]) bool // FirstMatch returns the first item in the collection that satisfies the given predicate. // If no match is found, it returns the zero value of T and false. FirstMatch(Predicate1[T]) (T, bool) // SelectMatch selects a subset of items from the collection that satisfy the given predicate. // It returns a new collection containing the matching items, of the same type as the original collection. SelectMatch(Predicate1[T]) Collection1[T] // PartitionMatch partitions the items in the collection // into two separate collections based on the provided predicate. // The first collection contains the items that satisfy the predicate (matched items), // while the second collection contains those that do not satisfy the predicate (unmatched items). // Both collections are of the same type as the original collection. PartitionMatch(Predicate1[T]) (Collection1[T], Collection1[T]) }
Collection1 is a generic interface for a collection of items.
type Collection2 ¶ added in v0.8.0
type Collection2[K, V any] interface { // Size returns the number of key-values in the collection. Size() int // IsEmpty returns true if the collection contains no key-values. IsEmpty() bool // Put adds a new key-value to the collection. // If the key already exists, its value is updated. Put(K, V) // Get returns the value associated with the given key in the collection. // If the key does not exist, the second return value will be false. Get(K) (V, bool) // Delete deletes the key-value specified by the given key from the collection. // If the key does not exist, the second return value will be false. Delete(K) (V, bool) // DeleteAll deletes all key-values from the collection, leaving it empty. DeleteAll() // All returns an iterator sequence containing all the key-values in the collection. // This allows for iterating over the entire collection using the range keyword. All() iter.Seq2[K, V] // AnyMatch returns true if at least one key-value in the collection satisfies the provided predicate function. // If no key-values satisfy the predicate or the collection is empty, it returns false. AnyMatch(Predicate2[K, V]) bool // AllMatch returns true if all key-values in the collection satisfy the provided predicate function. // If the collection is empty, it returns true. AllMatch(Predicate2[K, V]) bool // FirstMatch returns the first key-value in the collection that satisfies the given predicate. // If no match is found, it returns the zero values of K and V, along with false. FirstMatch(Predicate2[K, V]) (K, V, bool) // SelectMatch selects a subset of key-values from the collection that satisfy the given predicate. // It returns a new collection containing the matching key-values, of the same type as the original collection. SelectMatch(Predicate2[K, V]) Collection2[K, V] // PartitionMatch partitions the key-values in the collection // into two separate collections based on the provided predicate. // The first collection contains the key-values that satisfy the predicate (matched key-values), // while the second collection contains those that do not satisfy the predicate (unmatched key-values). // Both collections are of the same type as the original collection. PartitionMatch(Predicate2[K, V]) (Collection2[K, V], Collection2[K, V]) }
Collection2 is a generic interface for a collection of key-values.
type CompareFunc ¶
CompareFunc defines a generic function type for comparing two values of the same type. The function takes two arguments of type T and returns:
- A negative integer if the first value is less than the second,
- Zero if the two values are equal,
- A positive integer if the first value is greater than the second.
func NewCompareFunc ¶
func NewCompareFunc[T constraints.Ordered]() CompareFunc[T]
NewCompareFunc returns a generic comparison function for any type that satisfies the constraints.Ordered interface. The returned function compares two values of type T and returns:
- -1 if lhs is less than rhs,
- 1 if lhs is greater than rhs,
- 0 if lhs is equal to rhs.
This is useful for implementing custom sorting or comparison logic.
func NewReverseCompareFunc ¶ added in v0.8.0
func NewReverseCompareFunc[T constraints.Ordered]() CompareFunc[T]
NewReverseCompareFunc returns a generic reverse comparison function for any type that satisfies the constraints.Ordered interface. The returned function compares two values of type T and returns:
- 1 if lhs is less than rhs,
- -1 if lhs is greater than rhs,
- 0 if lhs is equal to rhs.
This is useful for implementing reverse sorting or inverted comparison logic.
type Comparer ¶ added in v0.10.3
Comparer is a generic interface for comparing two objects of the same type and establishing an order between them. The Compare method returns a negative value if the current object is less than the given object, zero if they are equal, and a positive value if the current object is greater.
type EqualFunc ¶
EqualFunc defines a generic function type for checking equality between two values of the same type. The function takes two arguments of type T and returns true if they are considered equal, or false otherwise.
func NewEqualFunc ¶
func NewEqualFunc[T comparable]() EqualFunc[T]
NewEqualFunc returns a generic equality function for any type that satisfies the comparable constraint.
type Equaler ¶ added in v0.8.0
Equaler is a generic interface for determining equality two objects of the same type.
type KeyValue ¶ added in v0.8.0
type KeyValue[K, V any] struct { Key K Val V }
KeyValue is a generic struct that holds a key-value pair. K and V represent the types of the key and value, respectively
type Predicate1 ¶ added in v0.8.0
Predicate1 is a generic function type that takes a value
and returns a boolean value, used for evaluating a condition.
type Predicate2 ¶ added in v0.8.0
Predicate2 is a generic function type that takes a key-value
and returns a boolean value, used for evaluating a condition.
type TraverseOrder ¶ added in v0.8.0
type TraverseOrder int
TraverseOrder represents the order in which nodes are traversed in a tree.
const ( // VLR is a pre-order traversal from left to right. VLR TraverseOrder = iota // VLR is a pre-order traversal from right to left. VRL // VLR is an in-order traversal from left to right. LVR // RVL is an in-order traversal from right to left. RVL // LRV is a post-order traversal from left to right. LRV // LRV is a post-order traversal from right to left. RLV // Ascending is an ascending traversal. Ascending // Descending is a descending traversal. Descending )
type Tree1 ¶ added in v0.8.0
type Tree1[T any] interface { // Traverse performs a traversal of the tree using the specified traversal order // and yields the value of each node to the provided VisitFunc1 function. // If the function returns false, the traversal is halted. Traverse(TraverseOrder, VisitFunc1[T]) // DOT generates and returns a representation of the tree in DOT format. // This format is commonly used for visualizing graphs with Graphviz tools. DOT() string }
Tree1 represents a generic tree structure where nodes contain a single value.
type Tree2 ¶ added in v0.8.0
type Tree2[K, V any] interface { // Traverse performs a traversal of the tree using the specified traversal order // and yields the key-value pair of each node to the provided VisitFunc2 function. // If the function returns false, the traversal is halted. Traverse(TraverseOrder, VisitFunc2[K, V]) // DOT generates and returns a representation of the tree in DOT format. // This format is commonly used for visualizing graphs with Graphviz tools. DOT() string }
Tree2 represents a generic tree structure where nodes contain key-value pairs.
type VisitFunc1 ¶ added in v0.8.0
VisitFunc1 is a generic function type used during tree traversal
for processing nodes with a single value.
type VisitFunc2 ¶ added in v0.8.0
VisitFunc2 is a generic function type used during tree traversal
for processing nodes with key-value pairs.