slices

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2023 License: MIT Imports: 3 Imported by: 2

Documentation

Overview

Package slices provides utilities to work with slices

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.3.0

func All[T any](arr []T, predicate func(t T) bool) bool

func Any added in v0.3.0

func Any[T any](arr []T, predicate func(t T) bool) bool

func Append added in v0.3.0

func Append[T any](arr []T, item T) []T

func AppendVector added in v0.3.0

func AppendVector[T any](arr, items []T) []T

func Contains

func Contains[T any](arr []T, predicate func(t T) bool) bool

func Cut

func Cut[T any](arr []T, from, to int) []T

Cut removes a sector from slice given lower and upper bounds. Bounds are represented as indices of the slice. E.g: Cut([1, 2, 3, 4], 1, 2) -> [1, 4] Cut([4], 0, 0) -> [] Cut will returned the original slice without the cut subslice.

func Delete added in v0.3.0

func Delete[T any](arr []T, idx int) []T

Delete removes the element in `idx` position, without preserving array order. In case `idx` is out of bounds, noop.

func DeleteOrder added in v0.3.0

func DeleteOrder[T any](arr []T, idx int) []T

DeleteOrder removes the element in `idx` position, preserving array order. In case `idx` is out of bounds, noop.

func Equals

func Equals[T any](one, other []T, predicate func(x, y T) bool) (res bool)

func Extract added in v0.3.0

func Extract[T any](arr []T, predicate func(t T) bool) ([]T, T, bool)

Extract gets and deletes the element than matches predicate. Returned values are the modified slice, the item or zero value if not found, and whether item was found

func ExtractIdx added in v0.3.0

func ExtractIdx[T any](arr []T, idx int) (res []T, item T, ok bool)

ExtractIdx gets and deletes the element at the given position. Returned values are the modified slice, the item or zero value if not found, and whether item was found

func Filter

func Filter[T any](arr []T, predicate func(t T) bool) []T

func FilterInPlace

func FilterInPlace[T any](arr []T, predicate func(t T) bool) []T

func FilterInPlaceCopy

func FilterInPlaceCopy[T any](arr []T, predicate func(t T) bool) []T

func FilterMap

func FilterMap[T, U any](arr []T, predicate func(t T) fp.Option[U]) []U

func FilterMapTuple

func FilterMapTuple[T, U any](arr []T, predicate func(t T) (U, bool)) []U

func Find added in v0.3.0

func Find[T any](arr []T, predicate func(t T) bool) (res T, ok bool)

Find returns the first element that matches predicate

func FindIdx added in v0.3.0

func FindIdx[T any](arr []T, predicate func(t T) bool) (res T, idx int)

FindIdx returns the first element that matches predicate as well as the position on the slice.

func Fold

func Fold[T, U any](arr []T, p func(U, T) U, initial U) U

func FoldSame

func FoldSame[T any](arr []T, p func(T, T) T, initial T) T

func IndexOf

func IndexOf[T any](arr []T, predicate func(t T) bool) (pos int)

func Insert added in v0.3.0

func Insert[T any](arr []T, item T, idx int) []T

Insert places the given item at the position `idx` for the given slice

func InsertVector added in v0.3.0

func InsertVector[T any](arr, items []T, idx int) (res []T)

InsertVector places the given vector at the position `idx` for the given slice, moving existing elements to the right.

func Map

func Map[T, U any](arr []T, predicate func(t T) U) []U

func MapInPlace

func MapInPlace[T any](arr []T, predicate func(t T) T) []T

func Peek added in v0.3.0

func Peek[T any](arr []T, idx int) (item T, ok bool)

Peek returns the item corresponding to idx

func Pop added in v0.3.0

func Pop[T any](arr []T) (res []T, item T, ok bool)

Pop deletes and returns the last item from the slice, starting from the end.

func PopFront added in v0.3.0

func PopFront[T any](arr []T) (res []T, item T, ok bool)

PopFront retrieves and deletes the element at the head of the slice

func PushFront added in v0.3.0

func PushFront[T any](arr []T, item T) []T

PushFront inserts the item at the head of the slice

func Reduce

func Reduce[T, U any](arr []T, p func(T, T) T) (res T)

func ReduceSame

func ReduceSame[T any](arr []T, p func(T, T) T) T

func Shift added in v0.3.0

func Shift[T any](arr []T) ([]T, T, bool)

Shift inserts the item at the head of the slice

func Some added in v0.3.0

func Some[T any](arr []T, predicate func(t T) bool) bool

func ToMap

func ToMap[V any, K comparable](arr []V, predicate func(x V) K) map[K]V

func ToMapIdx

func ToMapIdx[V any, K comparable](arr []V, predicate func(x V) K) map[K]WrappedIdx[V]

func Unshift added in v0.3.0

func Unshift[T any](arr []T, item T) []T

Unshift inserts the item at the head of the slice

Types

type Slice

type Slice[T any] []T

func (*Slice[T]) Append added in v0.3.0

func (s *Slice[T]) Append(item T) Slice[T]

func (*Slice[T]) AppendVector added in v0.3.0

func (s *Slice[T]) AppendVector(items []T) Slice[T]

func (Slice[T]) Clone

func (s Slice[T]) Clone() Slice[T]

func (Slice[T]) Contains

func (s Slice[T]) Contains(fn func(t T) bool) bool

func (*Slice[T]) Delete added in v0.3.0

func (s *Slice[T]) Delete(idx int) Slice[T]

func (Slice[T]) Equals

func (s Slice[T]) Equals(other Slice[T], predicate func(x, y T) bool) (res bool)

func (Slice[T]) Filter

func (s Slice[T]) Filter(predicate func(x T) bool) Slice[T]

func (Slice[T]) FilterInPlace

func (s Slice[T]) FilterInPlace(predicate func(x T) bool) Slice[T]

func (Slice[T]) FilterInPlaceCopy

func (s Slice[T]) FilterInPlaceCopy(predicate func(x T) bool) Slice[T]

func (Slice[T]) FilterMap

func (s Slice[T]) FilterMap(predicate func(x T) fp.Option[T]) Slice[T]

func (Slice[T]) FilterMapTuple

func (s Slice[T]) FilterMapTuple(predicate func(x T) (T, bool)) Slice[T]

func (Slice[T]) Fold

func (s Slice[T]) Fold(predicate func(x, y T) T, initial T) T

func (Slice[T]) Get

func (s Slice[T]) Get(i int) (res T, ok bool)

func (Slice[T]) IndexOf

func (s Slice[T]) IndexOf(fn func(t T) bool) int

func (Slice[T]) Len

func (s Slice[T]) Len() int

func (Slice[T]) Map

func (s Slice[T]) Map(predicate func(T) T) Slice[T]

func (Slice[T]) MapInPlace

func (s Slice[T]) MapInPlace(predicate func(T) T) Slice[T]

func (*Slice[T]) Push added in v0.3.0

func (s *Slice[T]) Push(item T) Slice[T]

func (Slice[T]) Range

func (s Slice[T]) Range(fn func(t T, i int) bool)

func (Slice[T]) Reduce

func (s Slice[T]) Reduce(predicate func(x, y T) T) T

func (Slice[T]) String added in v0.3.0

func (s Slice[T]) String() string

type WrappedIdx

type WrappedIdx[T any] struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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