collections

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 3 Imported by: 0

README

go-collections

License Codecov

Basic collections with Go using generics.

Table of Contents

Installation

go get github.com/gopher-utils/go-collections

Usage

Below is an example of how you can use go-collections in your project.

package main

import (
	"fmt"

	"github.com/gopher-utils/go-collections/collections"
)

func main() {
	// Create a new empty list of integers with capacity 10
	emptyList := collections.NewList[int](10)
	// Insert a number
	emptyList.Add(1)
	fmt.Println(emptyList) // Prints [1]

	// Create a list of integers with repeating ones of size 5
	repeatingList := collections.RepeatingList(1, 5)
	fmt.Println(repeatingList) // Prints [1,1,1,1,1]

	// Create a list from array
	fromArrayList := collections.ToList([]int{2, 3, 4})
	fmt.Println(fromArrayList) // Prints [2,3,4]

	// Concatenate two lists together
	emptyList.Extend(fromArrayList)
	fmt.Println(emptyList) // Prints [1,2,3,4]
}

Please check the documentation for more comprehensive usage.

Contributing

Any contributions to this repo are always welcome. Please check the Contribution Guidelines for details.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrIndexOutOfRange = errors.New("linq: index out of range")
	ErrItemNotFound    = errors.New("linq: item not found")
)

Functions

func Avg added in v1.0.1

func Avg[V Number](c Collection[V]) V

Returns the average of all elements present in the given collection. This method supports only numerical types.

func Max added in v1.0.1

func Max[V Number](c Collection[V]) V

Returns maximum element present in the given collection. This method supports only numerical types.

func Min added in v1.0.1

func Min[V Number](c Collection[V]) V

Returns minimum element present in the given collection. This method supports only numerical types.

func Reduce added in v1.0.1

func Reduce[T CollectionElement](l *List[T], callback listReduceFunction[T], initialValue T) T

Use Reduce to reduce the given list elements to a single element of same type T based on a callback function.

func Sum added in v1.0.1

func Sum[V Number](c Collection[V]) V

Returns the sum of all elements present in the given collection. This method supports only numerical types.

Types

type Collection

type Collection[T CollectionElement] interface {
	// Returns the type of the given collection.
	Type() CollectionType
	// Returns the size of the given collection.
	Size() int
	// Returns the description of the given collection.
	String() string
}

Generic interface that all collections must implement.

type CollectionElement

type CollectionElement interface {
	Equatable
}

A Generic interface that must be implemented by collection elements. By default, all primitive types and structs containing primitive types implement this interface.

type CollectionType

type CollectionType int

Typed constant that helps in determining the collection type.

const (
	TypeList    CollectionType = 0
	TypeSet     CollectionType = 1
	TypeHashmap CollectionType = 2
)

type Equatable

type Equatable interface {
	comparable
}

Interface with comparable constraint (== & != operator supportable).

type GenericType

type GenericType interface {
	Number | string
}

type List added in v1.0.1

type List[T CollectionElement] struct {
	// contains filtered or unexported fields
}

Collection that stores homogenous elements in a fixed order.

func Map added in v1.0.1

func Map[T CollectionElement, E CollectionElement](l *List[T], callback listMapFunction[T, E]) *List[E]

Use Map method to transform a list of a given type to another type.

func NewList added in v1.0.1

func NewList[T CollectionElement](capacity int) *List[T]

Factory method to create an empty list with predefined capacity.

func RepeatingList added in v1.0.1

func RepeatingList[T CollectionElement](element T, times int) *List[T]

Factory method to create a list with repeating values

func ToList added in v1.0.1

func ToList[T CollectionElement](array []T) *List[T]

Factory method to create a list from an array.

func (*List[T]) Add added in v1.0.1

func (l *List[T]) Add(item T)

Add element to list.

func (*List[T]) Contains added in v1.0.1

func (l *List[T]) Contains(item T) bool

Checks whether an element is present in the list.

func (*List[T]) CountOf added in v1.0.1

func (l *List[T]) CountOf(item T) (count int)

Returns number of occurences of given element in the list.

func (*List[T]) Distinct added in v1.0.1

func (l *List[T]) Distinct() *List[T]

Returns a new list containing unique elements.

func (*List[T]) Extend added in v1.0.1

func (l *List[T]) Extend(l2 *List[T])

Concatenate a list with another list.

func (*List[T]) Get added in v1.0.1

func (l *List[T]) Get(index int) (item T, err error)

Returns list element for a valid index. Returns error for an invalid index.

func (*List[T]) IndexOf added in v1.0.1

func (l *List[T]) IndexOf(item T) int

Returns the index of the first occurence of the element. If the element is not present in the list, it returns -1.

func (*List[T]) RemoveAll added in v1.0.1

func (l *List[T]) RemoveAll(item T) error

Removes all occurences of the given element from the list. Returns an error if the element is not present in the list.

func (*List[T]) RemoveDuplicates added in v1.0.1

func (l *List[T]) RemoveDuplicates()

Removes duplicates of elements in the list.

func (*List[T]) RemoveFirst added in v1.0.1

func (l *List[T]) RemoveFirst(item T) error

Removes first occurence of the given element from the list. Returns an error if the element is not present in the list.

func (*List[T]) Size added in v1.0.1

func (l *List[T]) Size() int

Returns the number of elements in the list.

func (*List[T]) String added in v1.0.1

func (l *List[T]) String() string

Returns a string description of the list.

func (*List[T]) ToArray added in v1.0.1

func (l *List[T]) ToArray() []T

Returns an slice containing elements of the list.

func (List[T]) Type added in v1.0.1

func (_ List[T]) Type() CollectionType

func (*List[T]) Where added in v1.0.1

func (l *List[T]) Where(f func(T) bool) *List[T]

Returns a filtered list based on the provided boolean function f.

type Number

type Number interface {
	int64 | float64 | int32 | int16 | int8 | int | float32
}

A Generic number interface that supports all basic number types.

type Set added in v1.0.1

type Set[T CollectionElement] struct {
	// contains filtered or unexported fields
}

Collection that stores a set of homogenous elements.

func NewSet added in v1.0.1

func NewSet[T CollectionElement]() *Set[T]

Factory method to create an empty set with predefined capacity.

func ToSet added in v1.0.1

func ToSet[T CollectionElement](array []T) *Set[T]

Factory method to create a set from an array.

func (*Set[T]) Add added in v1.0.1

func (s *Set[T]) Add(item T)

Add element to set.

func (*Set[T]) Clear added in v1.0.1

func (s *Set[T]) Clear()

Removes all elements from the set.

func (*Set[T]) Contains added in v1.0.1

func (s *Set[T]) Contains(item T) bool

Checks whether an element is present in the set.

func (*Set[T]) Difference added in v1.0.1

func (s *Set[T]) Difference(s2 *Set[T]) *Set[T]

Returns a new set instance containing elements that exists in the first set, but not in the second.

func (*Set[T]) Extend added in v1.0.1

func (s *Set[T]) Extend(s2 *Set[T])

Updates the current set to be it's union with another set.

func (*Set[T]) Intersection added in v1.0.1

func (s *Set[T]) Intersection(s2 *Set[T]) *Set[T]

Returns a new set instance containing the elements that exists in both sets.

func (*Set[T]) Remove added in v1.0.1

func (s *Set[T]) Remove(item T) error

Removes occurence of the given element from the set. Returns an error if the element is not present in the set.

func (*Set[T]) Size added in v1.0.1

func (s *Set[T]) Size() int

Returns the number of elements in the set.

func (*Set[T]) String added in v1.0.1

func (s *Set[T]) String() string

Returns a string description of the set.

func (*Set[T]) ToArray added in v1.0.1

func (s *Set[T]) ToArray() []T

Returns an slice containing elements of the set.

func (Set[T]) Type added in v1.0.1

func (_ Set[T]) Type() CollectionType

func (*Set[T]) Union added in v1.0.1

func (s *Set[T]) Union(s2 *Set[T]) *Set[T]

Returns a new set instance as the Union of both sets. This is the same as Extend, but without updating the rhs set.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL