sliceutil

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2025 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

sliceutil/generic.go

sliceutil/numeric.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Average

func Average[T types.Numeric](slice []T) (float64, error)

Average 函數,計算算術平均值

func Contains

func Contains[T comparable](slice []T, target T) bool

Contains 函數,檢查切片中是否包含特定元素

func Drt_PopFrom added in v0.6.4

func Drt_PopFrom[T any](slice *[]T) (T, error)

Drt_PopFrom 直接從切片中移除並返回最後一個元素, 這個函數會修改原切片!

func FindAll

func FindAll[T comparable](slice []T, target T) []int

FindAll 函數,查找所有匹配的元素,返回索引切片,未找到則返回空切片

func FindFirst

func FindFirst[T comparable](slice []T, target T) int

FindFirst 函數,查找第一個匹配的元素,返回索引值,未找到則返回 -1

func Flatten

func Flatten[T any](input any) ([]T, error)

Flatten 函數,將多層嵌套的切片展平成單層切片

func InsertAt

func InsertAt[T any](slice []T, index int, values ...T) ([]T, error)

InsertAt 函數,允許在指定位置插入元素,支持負索引 插入後,指定索引處的元素是新插入的元素,負索引也適用

func Max

func Max[T types.Numeric](slice []T) (T, error)

Max 函數,返回切片中的最大值

func Min

func Min[T types.Numeric](slice []T) (T, error)

Min 函數,返回切片中的最小值

func PopFrom added in v0.6.4

func PopFrom[T any](slice []T) (T, []T, error)

PopFrom 函數,移除並返回切片中的最後一個元素

func Remove

func Remove[T any](slice []T, index int) ([]T, error)

Remove 函數,移除指定索引處的元素,若索引超出範圍則返回錯誤,支持負索引

func RemoveAll

func RemoveAll[T comparable](slice []T, targets ...T) []T

RemoveAll 函數,移除切片中所有等於任何一個 target 的元素

func Replace added in v0.7.10

func Replace[T comparable](slice []T, target T, replacement T) []T

Replace 函數,將切片中所有等於 target 的元素替換為 replacement

func ReplaceAt added in v0.7.10

func ReplaceAt[T any](slice []T, index int, replacement T) ([]T, error)

ReplaceAt 函數,將切片中指定索引處的元素替換為 replacement

func ReplaceWithSlice added in v0.7.10

func ReplaceWithSlice[T any](slice []T, startIndex int, endIndex int, replacement []T) ([]T, error)

ReplaceWithSlice 函數,將切片中指定範圍的元素替換為另一個切片

func Reverse

func Reverse[T any](slice []T)

Reverse 函數,反轉切片中元素的順序

func Sort

func Sort[T types.Numeric](slice []T, ascending ...bool) error

Sort 函數,對數字切片排序,根據 ascending 參數決定升序或降序,默認為升序

func Sum added in v0.5.1

func Sum[T types.Numeric](slice []T) (T, error)

Sum 函數,計算數字切片中所有元素的總和

func Unique

func Unique[T comparable](slice []T) []T

Unique 函數,去除切片中的重複元素,適用於所有類型

Types

This section is empty.

Jump to

Keyboard shortcuts

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