collection

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package collections implements support for generic Collections of data. A collection wraps an underlying Go slice and provides convenience methods and syntatic sugar on top of it.

Index

Constants

This section is empty.

Variables

View Source
var (
	EmptyCollectionError = &CollectionError{
		code: 100, msg: "invalid operation on an empty collection",
	}
	ValueNotFoundError = &CollectionError{
		code: 101, msg: "value not found",
	}
	IndexOutOfBoundsError = &CollectionError{
		code: 102, msg: "index out of bounds",
	}
)

Functions

func Corresponds

func Corresponds[T, K any](s1 OrderedCollection[T], s2 OrderedCollection[K], f func(T, K) bool) bool

Corresponds tests whether every element of this sequence relates to the corresponding element of another sequence by satisfying a test predicate.

example usage:

c1 := NewSequence([]int{1,2,3,4,5,6})
c2 := NewSequence([]int{2,4,6,8,10,12})
Corresponds(c1, c2, func(i int, j int) bool { return i*2 == j })

output:

true

func Count

func Count[T any](s Collection[T], f func(T) bool) int

Count returns the number of elements in the collection that satisfy the predicate function.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
Count(c, func(i int) bool { return i % 2 == 0 })

output:

3

func Find

func Find[T any](s OrderedCollection[T], f func(T) bool) (index int, value T)

Find returns the index and value of the first element that satisfies a predicate, otherwise returns -1 and the zero value.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
Find(c, func(i int) bool {
  return (i + 3) > 5
})

output

3

func FindLast

func FindLast[T any](s OrderedCollection[T], f func(T) bool) (index int, value T)

FindLast returns the index and value of the last element that satisfies a predicate, otherwise returns -1 and the zero value.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
FindLast(c, func(i int) bool { return i < 6 })

output:

4, 5

func ForAll

func ForAll[T any](s Collection[T], f func(T) bool) bool

ForAll tests whether a predicate holds for all elements of this sequence.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
ForAll(c, func(i int) bool { return i < 10 })

output:

true

func GroupBy

func GroupBy[T any, K comparable](s Collection[T], f func(T) K) map[K]Collection[T]

GroupBy takes a collection and a grouping function as input and returns a map where the key is the result of the grouping function and the value is a collection of elements that satisfy the predicate.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
GroupBy(c, func(i int) int { return i % 2 })

output:

{0:[2,4,6], 1:[1,3,5]}
func Head[T any](s OrderedCollection[T]) (T, error)

Head returns the first element in a Sequence and a nil error. If the sequence is empty, it returns the zero value and an error.

example usage:

c := NewSequence([]string{"A","B","C"})
c.Head()

output:

"A", nil

func Last

func Last[T any](s OrderedCollection[T]) (T, error)

Last returns the last element in the Sequence and a nil error. If the sequence is empty, it returns the zero value and an error.

example usage:

c := NewSequence([]string{"A","B","C"})
c.Last()

output:

"C", nil

func Map

func Map[T, K any](s Collection[T], f func(T) K) []K

Map takes a collection of type T and a mapping function func(T) K, applies the mapping function to each element and returns a slice of type K.

example usage:

names := NewCollection([]string{"Alice", "Bob", "Charlie"})
Map(names, func(name string) int {
  return len(name)
})

output:

[5,3,6]

func MaxBy

func MaxBy[T any, K cmp.Ordered](s Collection[T], f func(T) K) (T, error)

MaxBy returns the element in the collection that has the maximum value according to a comparison function.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
MaxBy(c, func(a int, b int) int { return a - b })

output:

6

func MinBy

func MinBy[T any, K cmp.Ordered](s Collection[T], f func(T) K) (T, error)

MinBy returns the element in the collection that has the minimum value according to a comparison function.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
MinBy(c, func(a int, b int) int { return a - b })

output:

1

func Partition

func Partition[T any](s Collection[T], f func(T) bool) (Collection[T], Collection[T])

Partition takes a partitioning function as input and returns two collections, the first one contains the elements that match the partitioning condition, the second one contains the rest of the elements.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
Partition(c, func(i int) bool {
  return i%2==0
})

output:

[2,4,6], [1,3,5]

func Reduce

func Reduce[T, K any](s Collection[T], f func(K, T) K, init K) K

Reduce takes a collection of type T, a reducing function func(K, T) K, and an initial value of type K as parameters. It applies the reducing function to each element and returns the resulting value K.

example usage:

numbers := NewCollection([]int{1,2,3,4,5,6})

Reduce(numbers, func(accumulator int, number int) int {
  return accumulator + number
}, 0)

output:

21

func ReduceRight

func ReduceRight[T, K any](s OrderedCollection[T], f func(K, T) K, init K) K

ReduceRight takes a collection of type T, a reducing function func(K, T) K, and an initial value of type K as parameters. It applies the reducing function to each element in reverse order and returns the resulting value K.

example usage:

c := NewSequence([]string{"A","B","C"})
ReduceRight(c, func(acc string, i string) string { return acc + i }, "")

output:

"CBA"

func SplitAt

func SplitAt[T any](s OrderedCollection[T], n int) (OrderedCollection[T], OrderedCollection[T])

SplitAt returns two new sequences containing the first n elements and the rest of the elements.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
SplitAt(c, 3)

output:

[1,2,3], [4,5,6]

Types

type Collection

type Collection[T any] interface {
	Add(T)
	Length() int
	New(s ...[]T) Collection[T]
	Random() T
	Values() iter.Seq[T]
}

Collection is a generic interface that must be implemented by all collection sub-types. At a minimum, collections must support the methods defined below.

func Diff

func Diff[T comparable](s1 Collection[T], s2 Collection[T]) Collection[T]

Diff returns a new collection containing elements that are present in the first collection but not in the second.

example usage:

c1 := NewSequence([]int{1,2,3,4,5,6})
c2 := NewSequence([]int{2,4,6,8,10,12})
Diff(c1, c2)

output:

[1,3,5]

func Distinct

func Distinct[T any](s Collection[T], f func(T, T) bool) Collection[T]

Distinct returns a new collection containing only the unique elements of the collection.

example usage:

c := NewSequence([]int{1,1,1,4,5,1,2,2})
Distinct(c, func(i int, i2 int) bool { return i == i2 })

output:

[1,4,5,2]

func Filter

func Filter[T any](s Collection[T], f func(T) bool) Collection[T]

Filter returns a new collection containing only the elements that satisfy the predicate function.

example usage:

numbers := NewSequence([]int{1,2,3,4,5,6})
Filter(numbers, func(t int) bool { return t % 2 == 0 })

output:

[2,4,6]

func FilterNot

func FilterNot[T any](s Collection[T], f func(T) bool) Collection[T]

FilterNot returns the complement of the Filter function.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
FilterNot(c, func(t int) bool { return t % 2 == 0 })

output:

[1,3,5]

func Intersect

func Intersect[T comparable](s1 Collection[T], s2 Collection[T]) Collection[T]

Intersect returns a new collection containing elements that are present in both input collections.

example usage:

c1 := NewSequence([]int{1,2,3,4,5,6})
c2 := NewSequence([]int{2,4,6,8,10,12})
Intersect(c1, c2)

output:

[2,4,6]

type CollectionError

type CollectionError struct {
	// contains filtered or unexported fields
}

func (*CollectionError) Error

func (e *CollectionError) Error() string

type OrderedCollection

type OrderedCollection[T any] interface {
	Collection[T]
	At(index int) T
	All() iter.Seq2[int, T]
	Backward() iter.Seq2[int, T]
	Slice(start, end int) OrderedCollection[T]
	NewOrdered(s ...[]T) OrderedCollection[T]
}

OrderedCollection is a generic interface for collections who's underlying data structure is index-based, and the order of elements matters.

func Drop

func Drop[T any](s OrderedCollection[T], n int) OrderedCollection[T]

Drop returns a new sequence with the first n elements removed.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.Drop(2)

output:

[3,4,5,6]

func DropRight

func DropRight[T any](s OrderedCollection[T], n int) OrderedCollection[T]

DropRight returns a sequence with the last n elements removed.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.DropRight(2)

output:

[1,2,3,4]

func DropWhile

func DropWhile[T any](s OrderedCollection[T], f func(T) bool) OrderedCollection[T]

DropWhile returns a sequence with the first n elements that satisfy a predicate removed.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.DropWhile(func(i int) bool { return i < 4 })

output:

[4,5,6]

func Init

func Init[T any](s OrderedCollection[T]) OrderedCollection[T]

Init returns a collection containing all elements excluding the last one.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.Init()

output:

[1,2,3,4,5]

func Reverse

func Reverse[T any](s OrderedCollection[T]) OrderedCollection[T]

Reverse returns a new sequence with all elements in reverse order.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.Reverse()

output:

[6,5,4,3,2,1]

func ReverseMap

func ReverseMap[T, K any](s OrderedCollection[T], f func(T) K) OrderedCollection[K]

ReverseMap takes a collection of type T and a mapping function func(T) K, applies the mapping function to each element in reverseand returns a collection of type K.

example usage:

names := NewCollection([]string{"Alice", "Bob", "Charlie"})
Map(names, func(name string) int {
  return len(name)
})

output:

[6, 3, 5]

func Tail

func Tail[T any](s OrderedCollection[T]) OrderedCollection[T]

Tail returns a new sequence containing all elements excluding the first one.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.Tail()

output:

[2,3,4,5,6]

func Take

func Take[T any](s OrderedCollection[T], n int) OrderedCollection[T]

Take returns a new sequence containing the first n elements.

example usage:

[c := NewSequence([]int{1,2,3,4,5,6})
c.Take(3)

output:

[1,2,3]

func TakeRight

func TakeRight[T any](s OrderedCollection[T], n int) OrderedCollection[T]

TakeRight returns a new sequence containing the last n elements.

example usage:

c := NewSequence([]int{1,2,3,4,5,6})
c.TakeRight(3)

output:

[4,5,6]

Jump to

Keyboard shortcuts

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