Documentation
¶
Overview ¶
Package bag defines the Bag struct, an implementation of the Collection interface, and supporting functions. A Bag is an unordered collection of comparable items.
Index ¶
- func Map[S comparable, T comparable](mapper result.FlatMapper[S, T], source *Bag[S]) result.Result[*Bag[T]]
- type Bag
- func (bag *Bag[T]) Add(items ...T) collections.Collection[T]
- func (bag *Bag[T]) Contains(item T) bool
- func (bag *Bag[T]) Detect(filter filters.Filter[T]) result.Result[maybe.Maybe[T]]
- func (bag *Bag[T]) Len() int
- func (bag *Bag[T]) Remove(item T) collections.Collection[T]
- func (bag *Bag[T]) Select(filter filters.Filter[T]) result.Result[collections.Collection[T]]
- func (bag *Bag[T]) String() string
- func (bag *Bag[T]) ToCollection() collections.Collection[T]
- func (bag *Bag[T]) ToSlice() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Map ¶
func Map[S comparable, T comparable]( mapper result.FlatMapper[S, T], source *Bag[S], ) result.Result[*Bag[T]]
Map the given Bag using the given mapper, returning a Bag of mapped items, never nil.
Types ¶
type Bag ¶
type Bag[T comparable] struct { // contains filtered or unexported fields }
Bag defines the structure of a bag collection, an implementation of the collections.Collection interface. A Bag is a hashed collection of comparable items; duplicates are allowed. This type is not thread-safe.
func NewFrom ¶
func NewFrom[T comparable](items ...T) *Bag[T]
NewFrom creates a Bag containing the given items.
func NewFromCollection ¶
func NewFromCollection[T comparable](source collections.Collection[T]) *Bag[T]
NewFromCollection creates a Bag containing the items in the given Collection.
func NewWithAccessor ¶
func NewWithAccessor[T comparable](size int) (*Bag[T], func() map[T]int)
NewWithAccessor creates a Bag of the given size, along with an accessor function. The accessor function is intended for use by composite types that need direct access to the Bag's implementation; the accessor function should not be shared.
func (*Bag[T]) Add ¶
func (bag *Bag[T]) Add(items ...T) collections.Collection[T]
Add the given items to the receiver. Returns the receiver as a collections.Collection.
func (*Bag[T]) Contains ¶
Contains returns true if the given item is contained in the receiver, otherwise false.
func (*Bag[T]) Detect ¶
Detect queries the receiver for an item matching the given filter. Returns the detected item, and true when found, otherwise the zero value and false; an error is returned if the filter fails.
func (*Bag[T]) Len ¶
Len returns the length of the receiver; an empty receiver has a length of zero.
func (*Bag[T]) Remove ¶
func (bag *Bag[T]) Remove(item T) collections.Collection[T]
Remove the given item from the receiver. If the item does not exist the receiver is unchanged.Returns the receiver as a collections.Collection.
func (*Bag[T]) Select ¶
func (bag *Bag[T]) Select(filter filters.Filter[T]) result.Result[collections.Collection[T]]
Select returns the receiver's items that match the given filter; an error is returned if the filter fails. The returned collection is the same species as the receiver, and never nil.
func (*Bag[T]) ToCollection ¶
func (bag *Bag[T]) ToCollection() collections.Collection[T]
ToCollection returns the receiver as a collections.Collection.