shoal

package module
v0.0.0-...-5b634c6 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 4 Imported by: 0

README

Shoal

Shoal is a library written in Go for working with iterable structures, using the new range-over functions.

Why this name?

It's short and reflects the idea of managing collections of elements in a coordinated way, much like how a shoal of fish moves together.

Getting Started

Requirements
  • Go v1.23+
Installation
go get -u github.com/nicolito128/shoal

Then, import it in your code

    import "github.com/nicolito128/shoal"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrReadOnlyCollection = fmt.Errorf("collection is read only")
	ErrIndexOutOfRaange   = fmt.Errorf("index for collection assign is out of range")
)

Functions

func Count

func Count[S ~[]T, T comparable](collection S, value T) (count int)

Count returns the number of elements in the slice that are equal to the specified value.

func CountBy

func CountBy[S ~[]T, T comparable](collection S, predicates ...MorphCond2[int, T]) (count int)

CountBy returns the number of elements in the slice that satisfy the given predicate(s).

It uses MorphCond2 transformations, allowing for operations like:

CountBy([]Type{...}, func(index int, value Type) bool {...})

func Equal

func Equal[S ~[]T, T comparable](c1 *Collection[S, T], c2 *Collection[S, T]) bool

func EqualSlice

func EqualSlice[S ~[]T, T comparable](s1 S, s2 S) bool

func Filter

func Filter[S ~[]T, T comparable](collection S, mapping ...MorphCond2[int, T]) S

Filter returns a new slice containing only the elements that satisfy the given predicate(s).

It uses MorphCond transformations, allowing for operations like:

Filter([]Type{...}, func(value Type) bool {...})

func FilterSeq

func FilterSeq[S ~[]T, T comparable](collection S, mapping ...MorphCond2[int, T]) iter.Seq2[int, T]

FilterSeq returns an iterator (Seq) that yields elements from the input slice that satisfy the given predicate(s).

func ForEach

func ForEach[S ~[]T, T any](collection S, morph Morph0[T])

ForEach applies a function to each element in the input slice.

It uses Morph0 transformations, allowing for operations like:

ForEach([]Type{...}, func(value Type) {...})

func ForEachSeq

func ForEachSeq[S ~[]T, T any](collection S) iter.Seq[T]

ForEachSeq returns an iterator (Seq) that yields each element in the input slice, allowing for operations to be performed on each element.

func Map

func Map[S ~[]T, T, U any](collection S, mapping ...Morph2[int, T, U]) []U

Map transforms a slice of one type into a slice of another type.

It uses Morph2 transformations, allowing for operations like:

Map([]Type{...}, func(index int, value Type) NewType {...})

func MapSeq

func MapSeq[S ~[]T, T, U any](collection S, mapping ...Morph2[int, T, U]) iter.Seq2[int, U]

MapSeq returns an iterator (Seq2) that applies a series of transformations to each element in the input slice.

func Unique

func Unique[S ~[]T, T comparable](collection S, mapping ...Morph[T, T]) S

Unique returns a new slice containing only the unique elements from the input slice, with optional transformations.

func UniqueSeq

func UniqueSeq[S ~[]T, T comparable](collection S, mapping ...Morph[T, T]) iter.Seq[T]

UniqueSeq returns an iterator (Seq) that yields unique elements from the input slice, with optional transformations.

Types

type Collection

type Collection[S ~[]T, T any] struct {
	// contains filtered or unexported fields
}

func NewCollection

func NewCollection[S ~[]T, T any](slice S, readOnly bool) *Collection[S, T]

func (*Collection[S, T]) Append

func (c *Collection[S, T]) Append(value ...T) error

func (*Collection[S, T]) Cap

func (c *Collection[S, T]) Cap() int

func (*Collection[S, T]) Get

func (c *Collection[S, T]) Get(ind int) (T, error)

func (*Collection[S, T]) Len

func (c *Collection[S, T]) Len() int

func (*Collection[S, T]) ReadOnly

func (c *Collection[S, T]) ReadOnly() bool

func (*Collection[S, T]) Set

func (c *Collection[S, T]) Set(ind int, value T) error

func (*Collection[S, T]) Unwrap

func (c *Collection[S, T]) Unwrap() S

type Morph

type Morph[CurrentType, NewType any] func(CurrentType) NewType

Morph is a CurrentType to NewType transformation.

type Morph0

type Morph0[CurrentType any] func(CurrentType)

Morph0 is a static transformation of CurrentType.

type Morph2

type Morph2[KeyType, ValueType, NewValueType any] func(KeyType, ValueType) NewValueType

Morph2 is a KeyType and ValueType to NewValueType transformation.

type MorphCond

type MorphCond[T comparable] func(T) bool

MorphCond is a conditional transformation of T-type.

type MorphCond2

type MorphCond2[K, V comparable] func(K, V) bool

MorphCond2 is a conditional transformation of K-type and T-type.

Jump to

Keyboard shortcuts

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