Documentation ¶
Overview ¶
Package hashset implements an immutable Set datastructure on top of hashmap
A note about Value equality. If you would like to override the default go equality operator for values in this library implement the Equal(other interface{}) bool function for the type. Otherwise '==' will be used with all its restrictions.
Index ¶
- type Set
- func (s *Set) Add(elem interface{}) *Set
- func (s *Set) Apply(args ...interface{}) interface{}
- func (s *Set) AsTransient() *TSet
- func (s *Set) At(elem interface{}) interface{}
- func (s *Set) Conj(elem interface{}) interface{}
- func (s *Set) Contains(elem interface{}) bool
- func (s *Set) Delete(elem interface{}) *Set
- func (s *Set) Equal(o interface{}) bool
- func (s *Set) Find(elem interface{}) (interface{}, bool)
- func (s *Set) Length() int
- func (s *Set) MakeTransient() interface{}
- func (s *Set) Range(do interface{})
- func (s *Set) Reduce(fn interface{}, init interface{}) interface{}
- func (s *Set) Seq() seq.Sequence
- func (s *Set) String() string
- func (s *Set) Transform(xfrms ...func(*TSet) *TSet) *Set
- type TSet
- func (s *TSet) Add(elem interface{}) *TSet
- func (s *TSet) Apply(args ...interface{}) interface{}
- func (s *TSet) AsPersistent() *Set
- func (s *TSet) At(elem interface{}) interface{}
- func (s *TSet) Conj(elem interface{}) interface{}
- func (s *TSet) Contains(elem interface{}) bool
- func (s *TSet) Delete(elem interface{}) *TSet
- func (s *TSet) Equal(o interface{}) bool
- func (s *TSet) Length() int
- func (s *TSet) MakePersistent() interface{}
- func (s *TSet) Range(do interface{})
- func (s *TSet) Reduce(fn interface{}, init interface{}) interface{}
- func (s *TSet) String() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
Set is a persistent unordered set implementation.
func From ¶
func From(value interface{}) *Set
From will convert many different go types to an immutable map. Converting some types is more efficient than others and the mechanisms are described below.
*Set:
Returned directly as it is already immutable.
*TSet:
AsPersistent is called on it and the result is returned.
map[interface{}]struct{}:
Converted directly by looping over the map and calling Add starting with an empty transient set. The transient set is the converted to a persistent one and returned.
[]interface{}:
The elements are passed to New.
map[kT]vT:
Reflection is used to loop over the keys of the map and add them to an empty transient set. The transient set is converted to a persistent map and then returned.
[]T:
Reflection is used to convert the slice to add the elements to the set.
seq.Sequence:
The sequence is reduced into a transient set that is made persistent on return.
seq.Sequable:
A sequence is obtained using Seq() and then the sequence is reduced into a transient set that is made persistent on return.
func (*Set) Apply ¶
func (s *Set) Apply(args ...interface{}) interface{}
Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows map to be called as a function by the 'dyn' library.
func (*Set) AsTransient ¶
AsTransient returns a mutable copy on write version of the set.
func (*Set) At ¶
func (s *Set) At(elem interface{}) interface{}
At returns the elem if it exists in the set otherwise it returns nil.
func (*Set) Conj ¶
func (s *Set) Conj(elem interface{}) interface{}
Conj adds an element to the set. Conj implements a generic mechanism for building collections.
func (*Set) Delete ¶
Delete removes an element from the set returning a new Set without the element.
func (*Set) Equal ¶
Equal tests if two sets are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons when there are sets of sets
func (*Set) Find ¶
Find will return the key if it exists in the set and whether the key exists in the set. If the key is not in the set, (nil, false) is returned.
func (*Set) MakeTransient ¶
func (s *Set) MakeTransient() interface{}
MakeTransient is a generic version of AsTransient.
func (*Set) Range ¶
func (s *Set) Range(do interface{})
Range calls the passed in function on each element of the set. The function passed in may be of many types:
func(value interface{}) bool:
Takes a value of any type and returns if the loop should continue. Useful to avoid reflection where not needed and to support heterogenous sets.
func(value interface{})
Takes a value of any type. Useful to avoid reflection where not needed and to support heterogenous sets.
func(value T) bool:
Takes a value of the type of element stored in the set and returns if the loop should continue. Useful for homogeneous sets. Is called with reflection and will panic if the type is incorrect.
func(value T)
Takes a value of the type of element stored in the set and returns if the loop should continue. Useful for homogeneous sets. Is called with reflection and will panic if the type is incorrect.
Range will panic if passed anything that doesn't match one of these signatures
func (*Set) Reduce ¶
func (s *Set) Reduce(fn interface{}, init interface{}) interface{}
Reduce is a fast mechanism for reducing a Map. Reduce can take the following types as the fn:
func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT
Reduce will panic if given any other function type.
func (*Set) Seq ¶
Seq returns a seralized sequence of interface{} corresponding to the set's elements.
type TSet ¶
type TSet struct {
// contains filtered or unexported fields
}
TSet is a transient copy on write version of Set. Changes made to a transient set will not effect the original persistent structure. Changes to a transient set occur as mutations. These mutations are then made persistent when the transient is transformed into a persistent structure. These are useful when appling multiple transforms to a persistent set where the intermediate results will not be seen or stored anywhere.
func (*TSet) Apply ¶
func (s *TSet) Apply(args ...interface{}) interface{}
Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows map to be called as a function by the 'dyn' library.
func (*TSet) AsPersistent ¶
AsPersistent will transform this transient set into a persistent set. Once this occurs any additional actions on the transient set will fail.
func (*TSet) At ¶
func (s *TSet) At(elem interface{}) interface{}
At returns the elem if it exists in the set otherwise it returns nil.
func (*TSet) Conj ¶
func (s *TSet) Conj(elem interface{}) interface{}
Conj adds an element to the set. Conj implements a generic mechanism for building collections.
func (*TSet) Delete ¶
Delete removes an element from the set as a mutation returning the original TSet.
func (*TSet) Equal ¶
Equal tests if two sets are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons when there are sets of sets
func (*TSet) MakePersistent ¶
func (s *TSet) MakePersistent() interface{}
MakePersistent is a generic version of AsTransient.
func (*TSet) Range ¶
func (s *TSet) Range(do interface{})
Range calls the passed in function on each element of the set. The function passed in may be of many types:
func(value interface{}) bool:
Takes a value of any type and returns if the loop should continue. Useful to avoid reflection where not needed and to support heterogenous sets.
func(value interface{})
Takes a value of any type. Useful to avoid reflection where not needed and to support heterogenous sets.
func(value T) bool:
Takes a value of the type of element stored in the set and returns if the loop should continue. Useful for homogeneous sets. Is called with reflection and will panic if the type is incorrect.
func(value T)
Takes a value of the type of element stored in the set and returns if the loop should continue. Useful for homogeneous sets. Is called with reflection and will panic if the type is incorrect.
Range will panic if passed anything that doesn't match one of these signatures
func (*TSet) Reduce ¶
func (s *TSet) Reduce(fn interface{}, init interface{}) interface{}
Reduce is a fast mechanism for reducing a Map. Reduce can take the following types as the fn:
func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT
Reduce will panic if given any other function type.