collections

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package collections defines the collection interface types Collection and OrderedCollection. The subpackages bag, list, orderedlist, and set, define implementations of the collection interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](c Collection[T], item T) bool

Contains returns true if the given item is contained in the collection, otherwise false.

func Detect

func Detect[T comparable](c Collection[T], detect filters.Filter[T]) result.Result[maybe.Maybe[T]]

Detect queries the collection 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 First

func First[T cmp.Ordered](c OrderedCollection[T]) result.Result[T]

First returns the first item in the collection.

func Get

func Get[T cmp.Ordered](c OrderedCollection[T], index int) result.Result[T]

Get returns the item in the collection at the given index.

func Index

func Index[T cmp.Ordered](c OrderedCollection[T], item T) int

Index returns the index of the given item in the collection, or -1 if the given item is not contained in the collection.

func Insert

func Insert[T cmp.Ordered](c OrderedCollection[T], index int, item T) result.Result[OrderedCollection[T]]

Insert at the given index the given item. Returns the modified collection.

func Largest

func Largest[T cmp.Ordered](c OrderedCollection[T]) result.Result[T]

Largest returns the lexicographical largest item contained in the collection.

func Last

func Last[T cmp.Ordered](c OrderedCollection[T]) result.Result[T]

Last returns the last item in the collection.

func Len

func Len[T comparable](c Collection[T]) int

Len returns the length of the collection; an empty collection has a length of zero.

func Select

func Select[T comparable](c Collection[T], filter filters.Filter[T]) result.Result[Collection[T]]

Select returns the collection's items that match the given filter; an error is returned if the filter fails. The returned collection is the same species as the collection, and never nil.

func Set

func Set[T cmp.Ordered](c OrderedCollection[T], index int, item T) result.Result[T]

Set the given item at the given index in the collection, returning the item that previously occupied the given index, or the zero value, if the index is previously unoccupied.

func Smallest

func Smallest[T cmp.Ordered](c OrderedCollection[T]) result.Result[T]

Smallest returns the lexicographical smallest item contained in the collection.

func ToSlice

func ToSlice[T comparable](c Collection[T]) []T

ToSlice returns a new slice of items contained in the collection. Changes to the slice do not affect the contents of the collection.

Types

type Collection

type Collection[T comparable] interface {
	// Add the given items to the receiver. Returns the receiver as a
	// collections.Collection.
	Add(item ...T) Collection[T]
	// Contains returns true if the given item is contained in the receiver,
	// otherwise false.
	Contains(item T) bool
	// 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.
	Detect(detect filters.Filter[T]) result.Result[maybe.Maybe[T]]
	// Len returns the length of the receiver; an empty receiver has a length of
	// zero.
	Len() int
	// Remove the given item from the receiver. If the item does not exist the
	// receiver is unchanged.Returns the receiver as a collections.Collection.
	Remove(item T) 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.
	Select(filter filters.Filter[T]) result.Result[Collection[T]]
	// ToSlice returns a new slice of items contained in the receiver. Changes to the
	// slice do not affect the contents of the receiver.
	ToSlice() []T
}

Collection defines the API for all collection types, regardless of implementation.

func Add

func Add[T comparable](c Collection[T], item T) Collection[T]

Add the given items to the collection. Returns the collection as a collections.Collection.

func Remove

func Remove[T comparable](c Collection[T], item T) Collection[T]

Remove the given item from the collection. If the item does not exist the collection is unchanged.Returns the collection as a collections.Collection.

type OrderedCollection

type OrderedCollection[T cmp.Ordered] interface {
	// Collection is a composed type.
	Collection[T]
	// First returns the first item in the receiver.
	First() result.Result[T]
	// Get returns the item in the receiver at the given index.
	Get(index int) result.Result[T]
	// Index returns the index of the given item in the receiver, or -1 if the
	// given item is not contained in the receiver.
	Index(item T) int
	// Insert at the given index the given item.  Returns an error if index is out of
	// bounds.
	Insert(index int, item T) error
	// Largest returns the lexicographical largest item contained in the
	// receiver.
	Largest() result.Result[T]
	// Last returns the last item in the receiver.
	Last() result.Result[T]
	// Set the given item at the given index in the receiver, returning the item
	// that previously occupied the given index, or the zero value, if the index
	// is previously unoccupied.
	Set(index int, item T) result.Result[T]
	// Smallest returns the lexicographical smallest item contained in the receiver.
	Smallest() result.Result[T]
	// SortAscending sorts the content of the receiver in ascending lexical order.
	SortAscending() OrderedCollection[T]
	// SortDescending sorts the content of the receiver in descending lexical order.
	SortDescending() OrderedCollection[T]
}

OrderedCollection defines the API for all constraints.Ordered collection types, regardless of implementation. OrderedCollection is composed of the Collection interface.

func SortAscending

func SortAscending[T cmp.Ordered](c OrderedCollection[T]) OrderedCollection[T]

SortAscending sorts the content of the collection in ascending lexical order.

func SortDescending

func SortDescending[T cmp.Ordered](c OrderedCollection[T]) OrderedCollection[T]

SortDescending sorts the content of the collection in descending lexical order.

Directories

Path Synopsis
Package bag defines the Bag struct, an implementation of the Collection interface, and supporting functions.
Package bag defines the Bag struct, an implementation of the Collection interface, and supporting functions.
Package list defines the List struct, an implementation of the Collection interface, and supporting functions.
Package list defines the List struct, an implementation of the Collection interface, and supporting functions.
Package orderedlist defines the OrderedList struct, an implementation of the OrderedCollection interface, and supporting functions.
Package orderedlist defines the OrderedList struct, an implementation of the OrderedCollection interface, and supporting functions.
Package set defines the Set struct, an implementation of the Collection interface, and supporting functions.
Package set defines the Set struct, an implementation of the Collection interface, and supporting functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳