跳到主要内容

x

x 库是函数式与集合工具集(funk 风格),提供对切片/映射的 Map/Filter/Reduce、集合运算、聚合统计与常用辅助函数,让数据处理更简洁。

典型使用场景:

  • 遍历变换:x.Map / x.Filter / x.Reduce / x.Foreach / x.Find 处理集合,x.Chunk / x.Reverse / x.Shuffle / x.Zip 重组数据。
  • 集合运算:x.Contains / x.IndexOfx.Intersect / x.Subtract / x.Difference / x.IsSubsetx.RemoveRepeat 去重。
  • 聚合判定:x.Sum / x.Max / x.Minx.All / x.Any / x.Every / x.Somex.Keys / x.Values / x.ToMap
  • 辅助:x.If(三元)、x.Rangex.Retry(重试)、x.Sortx.WaitConnect

与相邻库的关系:x 是通用数据处理工具,无副作用,与 str(字符串)、json(结构化数据)配合,常用于把扫描/分析结果做整理与统计。

共 43 个函数

函数索引

函数参数返回值说明
x.Chunkarr any, size intany将切片按指定大小分组,最后一组可能不足 size 个元素
x.Containsin any, elem anybool判断元素是否存在于集合(切片/数组/map/字符串)中
x.ConvertToMapi anymap[string][]string将传入的对象转换为 map[string][]string 结构,常用于归一化键值数据
x.Differencex any, y anyany, any返回两个集合的差异,分别是仅属于 x 的元素和仅属于 y 的元素
x.Dropin any, n intany从切片开头丢弃 n 个元素,返回剩余元素组成的新切片
x.Equalexpected any, actual anybool判断两个对象是否相等(深度比较)
x.Filteri any, fc func(any) boolany遍历集合,仅保留回调函数返回 true 的元素
x.Findi any, fc func(any) boolany遍历集合,返回第一个使回调函数返回 true 的元素
x.Foreachi any, fc func(any)-从前向后遍历集合,对每个元素执行回调函数(无返回值)
x.ForeachRighti any, fc func(any)-从后向前遍历集合,对每个元素执行回调函数(无返回值)
x.GC--主动触发一次垃圾回收并尽量把空闲内存归还操作系统
x.GCPercentpercent intint设置 GC 触发阈值百分比并返回旧值(导出名为 x.GCPercent)
x.Headarr anyany返回切片的第一个元素
x.Ifi bool, a any, b anyany三元条件选择,当条件为真时返回 a,否则返回 b
x.IndexOfin any, elem anyint返回元素在切片中第一次出现的下标,未找到返回 -1
x.Intersectx any, y anyany返回两个集合的交集(同时存在于两个集合中的元素)
x.IsSubsetx any, y anybool判断集合 x 是否为集合 y 的子集
x.Keysout anyany返回 map 的所有键或结构体的所有字段名组成的切片
x.Mapi any, fc funkGeneralFuncTypeany遍历集合中的每个元素,使用回调函数处理后返回新的切片
x.Maxi anyany返回数值或字符串切片中的最大值
x.Mini anyany返回数值或字符串切片中的最小值
x.NewEventWatcherctx context.Context, triggerTime time.Duration, triggerCount int*utils.EventWatcherManager创建一个事件观察器,按时间间隔或累计事件数触发回调(导出名为 x.NewEventWatcher)
x.NewReducerreduceLimit int, handle reducer.ReduceFunction*reducer.Reducer创建一个归并器,超过 reduceLimit 条数据时用 handle 把较旧的数据合并(导出名为 x.NewReducer)
x.Rangei int[]any创建一个长度为 i 的空接口切片,常用于配合 for-range 生成定长循环
x.Reducei any, fc funkGeneralReduceFuncType, acc anyany对集合中的元素从初始累加器开始依次归并为单一结果
x.RemoveRepeatin anyany对切片去重,返回仅保留首次出现元素的新切片
x.Retryi int, handler func() bool-反复调用 handler,直到 handler 返回 false 或达到最大次数(导出名为 x.Retry)
x.Reversein anyany反转切片中元素的顺序,返回反转后的新切片
x.Shifti anyany返回去掉切片第一个元素后的新切片
x.Shufflein anyany随机打乱切片中元素的顺序,返回打乱后的新切片
x.Sortx any, less func(i, j int) bool-使用自定义的 less 比较函数对切片做稳定原地排序(导出名为 x.Sort)
x.Subtractx any, y anyany返回集合 x 中存在但集合 y 中不存在的元素
x.Sumarr anyfloat64计算切片中所有数值元素之和
x.Tailarr anyany返回切片中除第一个元素外的所有元素
x.ToFloat64x anyfloat64, bool将任意数值类型转换为 float64
x.ToMapin any, pivot stringany将一个结构体切片转换为以指定字段值为键的 map
x.Valuesout anyany返回 map 的所有值或结构体的所有字段值组成的切片
x.WaitConnectaddr string, timeout float64error等待一个地址的端口开放,直到超时,如果超时则返回错误,这通常用于等待并确保一个服务启动
x.Zipslice1 any, slice2 any[]Tuple将两个切片按下标两两组合成元组列表,长度取较短切片

可变参数函数索引

函数参数返回值说明
x.Allobjs ...anybool判断给定的多个值是否全部非空(真值)
x.Anyobjs ...anybool判断给定的多个值中是否至少有一个非空(真值)
x.Everyin any, elements ...anybool判断给定的所有元素是否都存在于集合中
x.Somein any, elements ...anybool判断给定的元素中是否至少有一个存在于集合中

函数详情

Chunk

Chunk(arr any, size int) any

将切片按指定大小分组,最后一组可能不足 size 个元素

参数

参数名类型说明
arrany待分组的切片
sizeint每组的元素个数

返回值

序号类型说明
r1any分组后的二维切片

示例

// VARS: 按每组 2 个分组
result = x.Chunk([1, 2, 3, 4, 5], 2)
// assert: 5 个元素分成 3 组
assert len(result) == 3, "5 elements in groups of 2 yields 3 chunks"

Contains

Contains(in any, elem any) bool

判断元素是否存在于集合(切片/数组/map/字符串)中

参数

参数名类型说明
inany源集合
elemany要查找的元素

返回值

序号类型说明
r1bool元素是否存在

示例

// VARS: 判断元素是否存在
result = x.Contains([1, 2, 3], 2)
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 不存在的元素返回 false
assert x.Contains([1, 2, 3], 9) == false, "9 is not in the slice"

ConvertToMap

ConvertToMap(i any) map[string][]string

将传入的对象转换为 map[string][]string 结构,常用于归一化键值数据

参数

参数名类型说明
iany待转换的对象(map 或结构体)

返回值

序号类型说明
r1map[string][]string转换后的 map[string][]string

示例

// VARS: 转换为字符串列表映射
m = x.ConvertToMap({"k": "v"})
// STDOUT: 打印键对应的值列表
println(m["k"]) // OUT: [v]
// assert: 取出第一个值
assert m["k"][0] == "v", "ConvertToMap should keep the value under its key"

Difference

Difference(x any, y any) (any, any)

返回两个集合的差异,分别是仅属于 x 的元素和仅属于 y 的元素

参数

参数名类型说明
xany第一个集合
yany第二个集合

返回值

序号类型说明
r1any仅属于 x 的元素集合
r2any仅属于 y 的元素集合

示例

// VARS: 求双向差异
left, right = x.Difference([1, 2, 3, 4], [2, 4])
// STDOUT: 打印仅属于第一个集合的元素
println(left) // OUT: [1 3]
// assert: 第二个集合没有独有元素
assert len(right) == 0, "no element is unique to the second set"

Drop

Drop(in any, n int) any

从切片开头丢弃 n 个元素,返回剩余元素组成的新切片

参数

参数名类型说明
inany源切片
nint从开头丢弃的元素个数

返回值

序号类型说明
r1any丢弃后剩余的切片

示例

// VARS: 丢弃开头 2 个元素
result = x.Drop([1, 2, 3, 4], 2)
// STDOUT: 打印结果
println(result) // OUT: [3 4]
// assert: 剩余 2 个元素
assert len(result) == 2, "drop should remove the first n elements"

Equal

Equal(expected any, actual any) bool

判断两个对象是否相等(深度比较)

参数

参数名类型说明
expectedany期望值
actualany实际值

返回值

序号类型说明
r1bool两者是否相等

示例

// VARS: 比较两个切片
result = x.Equal([1, 2], [1, 2])
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 不同内容不相等
assert x.Equal([1, 2], [1, 3]) == false, "different slices should not be equal"

Filter

Filter(i any, fc func(any) bool) any

遍历集合,仅保留回调函数返回 true 的元素

参数

参数名类型说明
iany待过滤的集合(切片/数组)
fcfunc(any) bool过滤回调,接收元素返回布尔值,true 表示保留

返回值

序号类型说明
r1any由保留下来的元素组成的新切片

示例

// VARS: 仅保留偶数
result = x.Filter([1, 2, 3, 4], func(e) { return e % 2 == 0 })
// STDOUT: 打印结果
println(result) // OUT: [2 4]
// assert: 过滤后剩 2 个
assert len(result) == 2, "Filter should keep the two even numbers"

Find

Find(i any, fc func(any) bool) any

遍历集合,返回第一个使回调函数返回 true 的元素

参数

参数名类型说明
iany待查找的集合(切片/数组)
fcfunc(any) bool判定回调,接收元素返回布尔值

返回值

序号类型说明
r1any第一个满足条件的元素,未找到返回 nil

示例

// VARS: 查找第一个大于 1 的元素
result = x.Find([1, 2, 3], func(e) { return e > 1 })
// STDOUT: 打印结果
println(result) // OUT: 2
// assert: 锁定结论
assert result == 2, "Find should return the first element greater than 1"

Foreach

Foreach(i any, fc func(any))

从前向后遍历集合,对每个元素执行回调函数(无返回值)

参数

参数名类型说明
iany待遍历的集合(切片/数组)
fcfunc(any)对每个元素执行的回调函数

示例

// VARS: 遍历累加(用闭包收集结果)
sum = 0
x.Foreach([1, 2, 3], func(e) { sum += e })
// STDOUT: 打印累加结果
println(sum) // OUT: 6
// assert: 锁定结论
assert sum == 6, "Foreach should visit every element"

ForeachRight

ForeachRight(i any, fc func(any))

从后向前遍历集合,对每个元素执行回调函数(无返回值)

参数

参数名类型说明
iany待遍历的集合(切片/数组)
fcfunc(any)对每个元素执行的回调函数

示例

// VARS: 从右向左拼接元素
order = []
x.ForeachRight([1, 2, 3], func(e) { order = append(order, e) })
// STDOUT: 打印访问顺序
println(order) // OUT: [3 2 1]
// assert: 第一个访问的是最后一个元素
assert order[0] == 3, "ForeachRight should visit from the tail"

GC

GC()

主动触发一次垃圾回收并尽量把空闲内存归还操作系统

示例

// 主动触发一次垃圾回收(仅副作用,无返回值)
x.GC()

GCPercent

GCPercent(percent int) int

设置 GC 触发阈值百分比并返回旧值(导出名为 x.GCPercent)

percent 表示相对上次 GC 后存活堆的增长百分比,越小 GC 越频繁;负值可关闭 GC

参数

参数名类型说明
percentint新的 GC 阈值百分比

返回值

序号类型说明
r1int设置前的旧阈值百分比

示例

old = x.GCPercent(150)
println(typeof(old).String()) // OUT: int
assert typeof(old).String() == "int", "GCPercent should return the previous percent as int"
x.GCPercent(old)

Head(arr any) any

返回切片的第一个元素

参数

参数名类型说明
arrany源切片

返回值

序号类型说明
r1any切片的第一个元素

示例

// VARS: 取第一个元素
result = x.Head([1, 2, 3])
// STDOUT: 打印结果
println(result) // OUT: 1
// assert: 锁定结论
assert result == 1, "head should return the first element"

If

If(i bool, a any, b any) any

三元条件选择,当条件为真时返回 a,否则返回 b

参数

参数名类型说明
ibool条件布尔值
aany条件为真时返回的值
bany条件为假时返回的值

返回值

序号类型说明
r1any根据条件选择的值

示例

// VARS: 条件为真时取第一个值
result = x.If(true, "a", "b")
// STDOUT: 打印结果
println(result) // OUT: a
// assert: 条件为假时取第二个值
assert x.If(false, "a", "b") == "b", "If should pick the second value when false"

IndexOf

IndexOf(in any, elem any) int

返回元素在切片中第一次出现的下标,未找到返回 -1

参数

参数名类型说明
inany源切片
elemany要查找的元素

返回值

序号类型说明
r1int元素首次出现的下标,未找到为 -1

示例

// VARS: 查找元素下标
result = x.IndexOf([1, 2, 3], 2)
// STDOUT: 打印下标
println(result) // OUT: 1
// assert: 不存在的元素返回 -1
assert x.IndexOf([1, 2, 3], 9) == -1, "missing element should return -1"

Intersect

Intersect(x any, y any) any

返回两个集合的交集(同时存在于两个集合中的元素)

参数

参数名类型说明
xany第一个集合
yany第二个集合

返回值

序号类型说明
r1any两个集合的交集

示例

// VARS: 求两个切片的交集
result = x.Intersect([1, 2, 3], [2, 3, 4])
// STDOUT: 打印交集
println(result) // OUT: [2 3]
// assert: 锁定结论
assert len(result) == 2, "intersection of the two slices has 2 elements"

IsSubset

IsSubset(x any, y any) bool

判断集合 x 是否为集合 y 的子集

参数

参数名类型说明
xany待判断的子集
yany父集合

返回值

序号类型说明
r1boolx 是否为 y 的子集

示例

// VARS: 判断子集关系
result = x.IsSubset([1, 2], [1, 2, 3])
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 锁定结论
assert result == true, "[1,2] is a subset of [1,2,3]"

Keys

Keys(out any) any

返回 map 的所有键或结构体的所有字段名组成的切片

参数

参数名类型说明
outanymap 或结构体

返回值

序号类型说明
r1any键名/字段名切片

示例

// VARS: 取出 map 的键
result = x.Keys({"a": 1})
// STDOUT: 打印键
println(result) // OUT: [a]
// assert: 单键 map 只有一个键
assert len(result) == 1, "single-key map should have one key"

Map

Map(i any, fc funkGeneralFuncType) any

遍历集合中的每个元素,使用回调函数处理后返回新的切片

参数

参数名类型说明
iany待遍历的集合(切片/数组)
fcfunkGeneralFuncType处理每个元素的回调函数,接收元素返回新值

返回值

序号类型说明
r1any由回调返回值组成的新切片

示例

// VARS: 把每个元素翻倍
result = x.Map([1, 2, 3], func(e) { return e * 2 })
// STDOUT: 打印结果
println(result) // OUT: [2 4 6]
// assert: 元素个数不变
assert len(result) == 3, "Map should keep element count"

Max

Max(i any) any

返回数值或字符串切片中的最大值

参数

参数名类型说明
iany数值或字符串切片

返回值

序号类型说明
r1any切片中的最大元素

示例

// VARS: 求切片最大值
result = x.Max([3, 1, 2])
// STDOUT: 打印最大值
println(result) // OUT: 3
// assert: 锁定结论
assert result == 3, "max of 3,1,2 should be 3"

Min

Min(i any) any

返回数值或字符串切片中的最小值

参数

参数名类型说明
iany数值或字符串切片

返回值

序号类型说明
r1any切片中的最小元素

示例

// VARS: 求切片最小值
result = x.Min([3, 1, 2])
// STDOUT: 打印最小值
println(result) // OUT: 1
// assert: 锁定结论
assert result == 1, "min of 3,1,2 should be 1"

NewEventWatcher

NewEventWatcher(ctx context.Context, triggerTime time.Duration, triggerCount int) *utils.EventWatcherManager

创建一个事件观察器,按时间间隔或累计事件数触发回调(导出名为 x.NewEventWatcher)

参数

参数名类型说明
ctxcontext.Context上下文,用于控制观察器生命周期
triggerTimetime.Duration触发的时间间隔
triggerCountint触发的累计事件数阈值

返回值

序号类型说明
r1*utils.EventWatcherManager事件观察器管理对象

示例

d = time.ParseDuration("1s")~
w = x.NewEventWatcher(context.Background(), d, 10)
assert w != nil, "event watcher should be created"

NewReducer

NewReducer(reduceLimit int, handle reducer.ReduceFunction) *reducer.Reducer

创建一个归并器,超过 reduceLimit 条数据时用 handle 把较旧的数据合并(导出名为 x.NewReducer)

常用于把无限增长的历史数据压缩到有限规模

参数

参数名类型说明
reduceLimitint触发归并的数据条数阈值
handlereducer.ReduceFunction归并函数,接收一组字符串并返回合并后的单条字符串

返回值

序号类型说明
r1*reducer.Reducer归并器对象,可调用 Push 推入数据、GetData 获取当前数据

示例

r = x.NewReducer(2, items => str.Join(items, ","))
r.Push("a"); r.Push("b"); r.Push("c")
data = r.GetData()
println(data)
assert len(data) >= 1, "reducer should keep reduced data"

Range

Range(i int) []any

创建一个长度为 i 的空接口切片,常用于配合 for-range 生成定长循环

参数

参数名类型说明
iint切片长度

返回值

序号类型说明
r1[]any长度为 i 的空接口切片

示例

// VARS: 创建长度为 3 的切片
result = x.Range(3)
// STDOUT: 打印长度
println(len(result)) // OUT: 3
// assert: 锁定结论
assert len(result) == 3, "Range should create a slice of the given length"

Reduce

Reduce(i any, fc funkGeneralReduceFuncType, acc any) any

对集合中的元素从初始累加器开始依次归并为单一结果

参数

参数名类型说明
iany待归并的集合(切片/数组)
fcfunkGeneralReduceFuncType归并回调,接收累加器与当前元素返回新的累加器
accany初始累加器值

返回值

序号类型说明
r1any归并后的最终结果

示例

// VARS: 从 0 开始累加求和
result = x.Reduce([1, 2, 3], func(acc, e) { return acc + e }, 0)
// STDOUT: 打印结果
println(result) // OUT: 6
// assert: 锁定结论
assert result == 6, "Reduce should sum the slice to 6"

RemoveRepeat

RemoveRepeat(in any) any

对切片去重,返回仅保留首次出现元素的新切片

参数

参数名类型说明
inany待去重的切片

返回值

序号类型说明
r1any去重后的切片

示例

// VARS: 去除重复元素
result = x.RemoveRepeat([1, 1, 2, 3, 3])
// STDOUT: 打印结果
println(result) // OUT: [1 2 3]
// assert: 去重后剩 3 个
assert len(result) == 3, "duplicates should be removed"

Retry

Retry(i int, handler func() bool)

反复调用 handler,直到 handler 返回 false 或达到最大次数(导出名为 x.Retry)

handler 返回 true 表示"继续重试",返回 false 表示"停止"

参数

参数名类型说明
iint最大重试次数
handlerfunc() bool每次重试调用的函数,返回 true 继续、false 停止

示例

count = 0
x.Retry(10, () => { count++; return count < 3 })
println(count) // OUT: 3
assert count == 3, "Retry keeps calling while handler returns true"

Reverse

Reverse(in any) any

反转切片中元素的顺序,返回反转后的新切片

参数

参数名类型说明
inany待反转的切片

返回值

序号类型说明
r1any反转顺序后的切片

示例

// VARS: 反转切片
result = x.Reverse([1, 2, 3])
// STDOUT: 打印结果
println(result) // OUT: [3 2 1]
// assert: 首元素变为原末元素
assert result[0] == 3, "reverse should put last element first"

Shift

Shift(i any) any

返回去掉切片第一个元素后的新切片

参数

参数名类型说明
iany源切片

返回值

序号类型说明
r1any去掉首元素后的切片

示例

// VARS: 去掉第一个元素
result = x.Shift([1, 2, 3])
// STDOUT: 打印结果
println(result) // OUT: [2 3]
// assert: 锁定结论
assert len(result) == 2, "Shift should drop the first element"

Shuffle

Shuffle(in any) any

随机打乱切片中元素的顺序,返回打乱后的新切片

参数

参数名类型说明
inany待打乱的切片

返回值

序号类型说明
r1any打乱顺序后的切片(元素不变)

示例

// VARS: 随机打乱(每次顺序不同)
result = x.Shuffle([1, 2, 3, 4, 5])
// assert: 元素个数不变
assert len(result) == 5, "shuffle should preserve length"

Sort

Sort(x any, less func(i, j int) bool)

使用自定义的 less 比较函数对切片做稳定原地排序(导出名为 x.Sort)

less(i, j) 返回 true 表示下标 i 的元素应排在下标 j 之前

参数

参数名类型说明
xany待排序的切片(原地修改)
lessfunc(i, j int) bool比较函数,接收两个下标,返回是否 i 应排在 j 前

示例

arr = [3, 1, 2]
x.Sort(arr, (i, j) => arr[i] < arr[j])
println(arr) // OUT: [1 2 3]
assert arr[0] == 1 && arr[2] == 3, "Sort should sort the slice ascending in place"

Subtract

Subtract(x any, y any) any

返回集合 x 中存在但集合 y 中不存在的元素

参数

参数名类型说明
xany源集合
yany要排除的集合

返回值

序号类型说明
r1any仅属于 x 而不属于 y 的元素集合

示例

// VARS: 求差集
result = x.Subtract([1, 2, 3, 4], [2, 4])
// STDOUT: 打印结果
println(result) // OUT: [1 3]
// assert: 锁定结论
assert len(result) == 2, "Subtract should keep elements only in x"

Sum

Sum(arr any) float64

计算切片中所有数值元素之和

参数

参数名类型说明
arrany数值切片

返回值

序号类型说明
r1float64所有元素之和

示例

// VARS: 求和
result = x.Sum([1, 2, 3])
// STDOUT: 打印结果
println(result) // OUT: 6
// assert: 锁定结论
assert result == 6, "sum of 1,2,3 should be 6"

Tail

Tail(arr any) any

返回切片中除第一个元素外的所有元素

参数

参数名类型说明
arrany源切片

返回值

序号类型说明
r1any除首元素外的切片

示例

// VARS: 取除首元素外的部分
result = x.Tail([1, 2, 3])
// STDOUT: 打印结果
println(result) // OUT: [2 3]
// assert: 锁定结论
assert len(result) == 2, "tail should drop the first element"

ToFloat64

ToFloat64(x any) (float64, bool)

将任意数值类型转换为 float64

参数

参数名类型说明
xany待转换的数值

返回值

序号类型说明
r1float64转换后的 float64 值
r2bool是否转换成功

示例

// VARS: 转换数值
v, ok = x.ToFloat64(3)
// STDOUT: 打印转换结果
println(v) // OUT: 3
// assert: 转换成功
assert ok == true, "integer 3 should convert to float64"

ToMap

ToMap(in any, pivot string) any

将一个结构体切片转换为以指定字段值为键的 map

参数

参数名类型说明
inany结构体切片
pivotstring作为键的结构体字段名

返回值

序号类型说明
r1any以 pivot 字段值为键、结构体为值的 map

示例

// 无法本地验证: ToMap 需要结构体切片作为输入, yak 无法内联构造结构体, 需用真实数据
// 将结构体切片按指定字段转为 map(键为该字段的值, 值为对应结构体)
users = getUsersFromSomewhere() // 假设返回结构体切片, 每个元素含 Id 字段
m = x.ToMap(users, "Id") // map: Id 值 -> 结构体

Values

Values(out any) any

返回 map 的所有值或结构体的所有字段值组成的切片

参数

参数名类型说明
outanymap 或结构体

返回值

序号类型说明
r1any值切片

示例

// VARS: 取出 map 的值
result = x.Values({"a": 1})
// STDOUT: 打印值
println(result) // OUT: [1]
// assert: 单键 map 只有一个值
assert len(result) == 1, "single-key map should have one value"

WaitConnect

WaitConnect(addr string, timeout float64) error

等待一个地址的端口开放,直到超时,如果超时则返回错误,这通常用于等待并确保一个服务启动

参数

参数名类型说明
addrstring目标地址(host:port)
timeoutfloat64最长等待时间(秒)

返回值

序号类型说明
r1error错误信息(超时或连接失败时非空)

示例

timeout, _ = time.ParseDuration("1m")
ctx, cancel = context.WithTimeout(context.New(), timeout)

go func() {
err = tcp.Serve("127.0.0.1", 8888, tcp.serverCallback(func (conn) {
conn.Send("hello world")
conn.Close()
}), tcp.serverContext(ctx))

die(err)
}()

os.WaitConnect("127.0.0.1:8888", 5)~ // 等待tcp服务器启动
conn = tcp.Connect("127.0.0.1", 8888)~
bytes = conn.Recv()~
println(string(bytes))

Zip

Zip(slice1 any, slice2 any) []Tuple

将两个切片按下标两两组合成元组列表,长度取较短切片

参数

参数名类型说明
slice1any第一个切片
slice2any第二个切片

返回值

序号类型说明
r1[]Tuple元组列表,每个元组包含两个切片中同下标的元素

示例

// VARS: 按下标配对
result = x.Zip([1, 2], ["a", "b"])
// assert: 配对数量为较短切片长度
assert len(result) == 2, "zip should pair elements by index"

可变参数函数详情

All

All(objs ...any) bool

判断给定的多个值是否全部非空(真值)

可选参数

参数名类型说明
objs...any一个或多个待判断的值

返回值

序号类型说明
r1bool是否全部为非空(真值)

示例

// VARS: 判断是否全为真值
result = x.All(true, true)
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 含假值时返回 false
assert x.All(true, false) == false, "one empty value makes it false"

Any

Any(objs ...any) bool

判断给定的多个值中是否至少有一个非空(真值)

可选参数

参数名类型说明
objs...any一个或多个待判断的值

返回值

序号类型说明
r1bool是否至少有一个为非空(真值)

示例

// VARS: 判断是否存在真值
result = x.Any(false, true)
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 全为假时返回 false
assert x.Any(false, false) == false, "all-empty input should be false"

Every

Every(in any, elements ...any) bool

判断给定的所有元素是否都存在于集合中

必填参数

参数名类型说明
inany源集合

可选参数

参数名类型说明
elements...any待检查的一个或多个元素

返回值

序号类型说明
r1bool是否所有元素都存在于集合中

示例

// VARS: 判断是否全部存在
result = x.Every([1, 2, 3], 1, 2)
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 含缺失元素时返回 false
assert x.Every([1, 2, 3], 1, 9) == false, "9 is missing so not every element is present"

Some

Some(in any, elements ...any) bool

判断给定的元素中是否至少有一个存在于集合中

必填参数

参数名类型说明
inany源集合

可选参数

参数名类型说明
elements...any待检查的一个或多个元素

返回值

序号类型说明
r1bool是否至少有一个元素存在于集合中

示例

// VARS: 判断是否存在任意一个
result = x.Some([1, 2, 3], 9, 2)
// STDOUT: 打印结果
println(result) // OUT: true
// assert: 都不存在时返回 false
assert x.Some([1, 2, 3], 8, 9) == false, "neither 8 nor 9 is present"