hs

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

package hs present some helper type and func for slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v0.21.0

func As[T any](values ...T) []T

func At

func At[T any](values []T, at int) T

func Contains added in v0.19.9

func Contains[T comparable](value T, values ...T) bool

Contains Contains reports whether value is present in values.

func Distinct added in v0.21.0

func Distinct[T comparable](v []T) []T

func Eq

func Eq[T comparable](a, b T) bool

func EqTo added in v0.19.5

func EqTo[T comparable](a T) func(T) bool

func Loc

func Loc[T any](values []T, start int, end int) []T

Loc return a slice from values, it accept negative index for start and end.

Loc([1,2,3], 1, -1) --> return [2]

if end is too large, it is equivlent to the len(values).

func Map added in v0.17.0

func Map[T any, R any](values []T, fn func(T) R) []R

Map apply fn on each element of values []T and return the transformed []R.

func Pipe added in v0.17.0

func Pipe[T any, R any](values []T, fn func(T) (R, bool)) []R

Pipe is filter + map from []T to []R.

Pipe does NOT removes unused capacity, call slices.Clip by yourself if needed.

Types

type Vec

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

Vec is simpler slice, mostly you don't need this.

Use *Vec instead of Vec

The zero value is hard to use thus...

func Make

func Make[T any](len, cap int) *Vec[T]

func New

func New[T any](data []T) *Vec[T]

func NewWith deprecated

func NewWith[T any](data ...T) *Vec[T]

Deprecated: use Of

func Of added in v0.19.8

func Of[T any](data ...T) *Vec[T]

func PipeVec added in v0.19.7

func PipeVec[T any, R any](values *Vec[T], fn func(T) (R, bool)) *Vec[R]

PipeVec is filter + map from *Vec[T] to *Vec[R].

PipeVec does NOT removes unused capacity, call [Clip] by yourself if needed.

func Repeat

func Repeat[T any](v T, count int) *Vec[T]

func (*Vec[T]) Append

func (v *Vec[T]) Append(data ...T) *Vec[T]

Append append data to v. see slices.Concat.

func (*Vec[T]) At

func (v *Vec[T]) At(index int) T

At is similar to Get but accept negative index.

-1 will locate the last element.

func (*Vec[T]) BinarySearch

func (v *Vec[T]) BinarySearch(target T, cmp func(a, b T) int) (pos int, ok bool)

BinarySearch searches for target in a sorted slice and returns the earliest position where target is found.

For more detail see: slices.BinarySearch

use cmp.Compare for convenience.

func (*Vec[T]) Clip added in v0.19.7

func (v *Vec[T]) Clip() *Vec[T]

Clip removes unused capacity from the (v *Vec[T]), underlying data become data[:len(s):len(s)]. return itself for convenience.

see slices.Clip.

func (*Vec[T]) Clone

func (v *Vec[T]) Clone() *Vec[T]

Clone create shallow clone of v.

underlying is slices.Clone.

func (*Vec[T]) Contains added in v0.17.0

func (v *Vec[T]) Contains(fn func(elem T) bool) bool

Contains reports whether at least one element elem of v satisfies eq(elem, input).

use EqTo for convenience. see github.com/hauntedness/std/hs.EqTo.

func (*Vec[T]) Data

func (v *Vec[T]) Data() []T

func (*Vec[T]) Distinct added in v0.21.0

func (v *Vec[T]) Distinct(eq func(a T, b T) bool) *Vec[T]

Distinct return distincted values based on func eq.

it doesn't modify the original slice.

func (*Vec[T]) Equal

func (v *Vec[T]) Equal(other *Vec[T], eq func(a T, b T) bool) bool

Equal compare each element, and return true if all the same.

use Eq for convenience. see github.com/hauntedness/std/hs.Eq.

func (*Vec[T]) Get

func (v *Vec[T]) Get(index int) T

func (*Vec[T]) Index added in v0.17.0

func (v *Vec[T]) Index(fn func(elem T) bool) int

Index IndexFunc returns the first index i satisfying eq(elem, input), or -1 if none do.

use EqTo for convenience. see github.com/hauntedness/std/hs.EqTo.

func (*Vec[T]) IsSorted

func (v *Vec[T]) IsSorted(cmp func(a T, b T) int) bool

func (*Vec[T]) Len

func (v *Vec[T]) Len() int

func (*Vec[T]) Loc

func (v *Vec[T]) Loc(start, end int) *Vec[T]

Loc return the sub slice from the original vec.

func (*Vec[T]) Pipe

func (v *Vec[T]) Pipe(fn func(T) (T, bool)) *Vec[T]

Pipe create a new Vec, the new element depends on the results of fn.

func (*Vec[T]) Reduce

func (v *Vec[T]) Reduce(initial T, fn func(a, b T) T) T

func (*Vec[T]) Reverse

func (v *Vec[T]) Reverse() *Vec[T]

Reverse reverses the elements of the slice in place.

func (*Vec[T]) Seq

func (v *Vec[T]) Seq() iter.Seq[T]

func (*Vec[T]) Set

func (v *Vec[T]) Set(i int, value T)

func (*Vec[T]) Slice deprecated

func (v *Vec[T]) Slice(start, end int) *Vec[T]

Slice is just an alias to Loc.

Deprecated: use Loc as it is shorter.

func (*Vec[T]) Sort

func (v *Vec[T]) Sort(cmp func(a T, b T) int) *Vec[T]

Sort sorts the slice x in ascending order as determined by the cmp function. This sort is not guaranteed to be stable.

func (*Vec[T]) SortStable added in v0.19.6

func (v *Vec[T]) SortStable(cmp func(a T, b T) int) *Vec[T]

SortStable sorts the slice x in ascending order as determined by the cmp function.

SortStable keeping the original order of equal elements.

func (*Vec[T]) String added in v0.15.0

func (v *Vec[T]) String() string

Jump to

Keyboard shortcuts

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