list

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce

func Reduce[T any, R any](l *List[T], init R, fn func(acc R, v T, i int) R) R

Reduce 按照 fn 规则聚合 List 元素。

Types

type List

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

List 是一个通用的动态数组容器,模仿 Python 的 list 和 JavaScript 的 Array,支持常用的增删查改等操作。

func From

func From[T any](xs []T) *List[T]

From 根据给定切片创建一个新的 List。

func Map

func Map[T any, R any](l *List[T], fn func(v T, i int) R) *List[R]

Map 返回一个新的 List,元素为 fn 映射结果。

func MapAsync

func MapAsync[T any, R any](
	ctx context.Context,
	l *List[T],
	maxGoroutines int,
	fn func(v T, i int) R,
) (*List[R], error)

MapAsync 并发映射 List,返回新 List。

func New

func New[T any]() *List[T]

New 创建一个空的 List。

func (*List[T]) Append

func (l *List[T]) Append(items ...T)

Append 在 List 末尾添加元素。

func (*List[T]) At

func (l *List[T]) At(i int) (T, bool)

At 获取指定索引的元素,支持负索引,越界返回 false。

func (*List[T]) Cap

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

Cap 返回 List 的容量。

func (*List[T]) Clear

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

Clear 清空 List。

func (*List[T]) Clone

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

Clone 返回 List 的副本。

func (*List[T]) CopyWithin

func (l *List[T]) CopyWithin(target int, start int, endOpt ...int)

CopyWithin 将指定区间的元素复制到目标位置。

func (*List[T]) Count

func (l *List[T]) Count(value T, eq func(a, b T) bool) int

Count 返回等于 value 的元素个数。

func (*List[T]) Every

func (l *List[T]) Every(pred func(v T, i int) bool) bool

Every 判断所有元素是否都满足条件。

func (*List[T]) Extend

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

Extend 扩展 List,添加切片中的所有元素。

func (*List[T]) Fill

func (l *List[T]) Fill(value T, startEnd ...int)

Fill 用指定值填充区间元素。

func (*List[T]) Filter

func (l *List[T]) Filter(pred func(v T, i int) bool) *List[T]

Filter 返回所有满足条件的元素组成的新 List。

func (*List[T]) Find

func (l *List[T]) Find(pred func(v T, i int) bool) (T, bool)

Find 返回第一个满足条件的元素。

func (*List[T]) FindIndex

func (l *List[T]) FindIndex(pred func(v T, i int) bool) int

FindIndex 返回第一个满足条件的元素索引。

func (*List[T]) FindLast

func (l *List[T]) FindLast(pred func(v T, i int) bool) (T, bool)

FindLast 返回最后一个满足条件的元素。

func (*List[T]) FindLastIndex

func (l *List[T]) FindLastIndex(pred func(v T, i int) bool) int

FindLastIndex 返回最后一个满足条件的元素索引。

func (*List[T]) ForEach

func (l *List[T]) ForEach(fn func(v T, i int))

ForEach 只读遍历 List。

func (*List[T]) ForEachAsync

func (l *List[T]) ForEachAsync(
	ctx context.Context,
	maxGoroutines int,
	fn func(v T, i int),
) error

ForEachAsync 并发只读遍历 List,支持最大 goroutine 数。

func (*List[T]) Get

func (l *List[T]) Get(i int) T

Get 获取指定索引的元素,支持负索引,越界会 panic。

func (*List[T]) Includes

func (l *List[T]) Includes(value T, eq func(a, b T) bool) bool

Includes 判断 List 是否包含指定元素,eq 为比较器。

func (*List[T]) IndexOf

func (l *List[T]) IndexOf(value T, eq func(a, b T) bool) int

IndexOf 返回第一个等于 value 的索引,未找到返回 -1。

func (*List[T]) Insert

func (l *List[T]) Insert(i int, v T)

Insert 在指定位置插入元素,支持负索引。

func (*List[T]) Join

func (l *List[T]) Join(sep string, toStr func(v T) string) string

Join 用分隔符连接 List 元素,toStr 为元素转字符串函数。

func (*List[T]) LastIndexOf

func (l *List[T]) LastIndexOf(value T, eq func(a, b T) bool) int

LastIndexOf 返回最后一个等于 value 的索引,未找到返回 -1。

func (*List[T]) Len

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

Len 返回 List 的长度。

func (*List[T]) Map

func (l *List[T]) Map(fn func(v T, i int) any) *List[any]

T -> any, return new Map 返回一个新的 List,元素为 fn 映射结果(类型为 any)。

func (*List[T]) MapInPlace

func (l *List[T]) MapInPlace(fn func(v T, i int) T)

T -> T, in place MapInPlace 原地映射 List 元素。

func (*List[T]) Pop

func (l *List[T]) Pop() (T, bool)

Pop 移除并返回最后一个元素。

func (*List[T]) Push

func (l *List[T]) Push(items ...T)

Push 是 Append 的别名。

func (*List[T]) RemoveAt

func (l *List[T]) RemoveAt(i int) (T, bool)

RemoveAt 移除指定索引的元素,返回被移除的值和是否成功。

func (*List[T]) RemoveFirst

func (l *List[T]) RemoveFirst(value T, eq func(a, b T) bool) bool

RemoveFirst 移除第一个等于 value 的元素,eq 为比较器。

func (*List[T]) Reverse

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

Reverse 原地反转 List。

func (*List[T]) Set

func (l *List[T]) Set(i int, v T)

Set 设置指定索引的元素,支持负索引,越界会 panic。

func (*List[T]) Shift

func (l *List[T]) Shift() (T, bool)

Shift 移除并返回第一个元素。

func (*List[T]) Slice

func (l *List[T]) Slice(startEnd ...int) *List[T]

Slice 返回指定区间的新 List,不修改原 List。

func (*List[T]) Some

func (l *List[T]) Some(pred func(v T, i int) bool) bool

Some 判断是否存在满足条件的元素。

func (*List[T]) Sort

func (l *List[T]) Sort(less func(a, b T) bool)

Sort 使用插入排序原地排序 List。

func (*List[T]) Splice

func (l *List[T]) Splice(start, deleteCount int, items ...T) *List[T]

Splice 删除并插入元素,返回被删除的元素列表,并在当前 List 上做就地修改。

func (*List[T]) String

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

String 返回 List 的字符串表示。

func (*List[T]) ToReversed

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

ToReversed 返回反转后的新 List。

func (*List[T]) ToSlice

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

ToSlice 返回 List 的副本切片。

func (*List[T]) ToSorted

func (l *List[T]) ToSorted(less func(a, b T) bool) *List[T]

ToSorted 返回排序后的新 List。

func (*List[T]) ToSpliced

func (l *List[T]) ToSpliced(start, deleteCount int, items ...T) *List[T]

ToSpliced 返回执行 splice 后的新 List,原 List 不变。

func (*List[T]) Unshift

func (l *List[T]) Unshift(items ...T)

Unshift 在 List 前端插入元素。

func (*List[T]) With

func (l *List[T]) With(i int, v T) *List[T]

With 返回修改某索引后的新 List 副本,原 List 不变。

Jump to

Keyboard shortcuts

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