用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
[TOC] # append Append挂载一个元素到当前Collection,如果挂载的元素类型不一致,则会在Collection中产生Error ~~~ sSlice := []int{1, 2} //没变 intColl := NewIntCollection(sSlice) intColl.Append(3) if intColl.Err() == nil { intColl.DD() } ~~~ # IsEmpty `IsEmpty() bool` 判断一个Collection是否为空,为空返回true, 否则返回false ~~~go intColl := NewIntCollection([]int{1,2}) println(intColl.IsEmpty()) // false ~~~ # IsNotEmpty `IsNotEmpty() bool` 判断一个Collection是否为空,为空返回false,否则返回true ~~~go intColl := NewIntCollection([]int{1,2}) println(intColl.IsNotEmpty()) // true ~~~ # Filter根据过滤函数获取Collection过滤后的元素 `Filter(func(item interface{}, key int) bool) ICollection` ~~~go intColl := NewIntCollection([]int{1, 2, 2, 3}) intColl.Filter(func(obj interface{}, index int) bool { val := obj.(int) if val == 2 { return true } return false }).DD() ~~~ # Reject将满足过滤条件的元素删除 `Reject(func(item interface{}, key int) bool) ICollection` ~~~go intColl := NewIntCollection([]int{1, 2, 3, 4, 5}) retColl := intColl.Reject(func(item interface{}, key int) bool { i := item.(int) return i > 3 }) if retColl.Count() != 3 { t.Error("Reject 重复错误") } retColl.DD() ~~~ # Each对Collection中的每个函数都进行一次函数调用,传入的参数是回调函数 `Each(func(item interface{}, key int))` 如果希望在某次调用的时候中止,在此次调用的时候设置Collection的Error,就可以中止调用。 ~~~go intColl := NewIntCollection([]int{1, 2, 3, 4}) sum := 0 intColl.Each(func(item interface{}, key int) { v := item.(int) sum = sum + v }) if intColl.Err() != nil { t.Error(intColl.Err()) } if sum != 10 { t.Error("Each 错误") } sum = 0 intColl.Each(func(item interface{}, key int) { v := item.(int) sum = sum + v if sum > 4 { intColl.SetErr(errors.New("stop the cycle")) return } }) if sum != 6 { t.Error("Each 错误") } ~~~ # Every判断Collection中的每个元素是否都符合某个条件,只有当每个元素都符合条件,才整体返回true,否则返回false `Every(func(item interface{}, key int) bool) bool` ~~~go intColl := NewIntCollection([]int{1, 2, 3, 4}) if intColl.Every(func(item interface{}, key int) bool { i := item.(int) return i > 1 }) != false { t.Error("Every错误") } if intColl.Every(func(item interface{}, key int) bool { i := item.(int) return i > 0 }) != true { t.Error("Every错误") } ~~~ # ForPage将Collection函数进行分页,按照每页第二个参数的个数,获取第一个参数的页数数据 `ForPage(page int, perPage int) ICollection` ~~~go intColl := NewIntCollection([]int{1, 2, 3, 4, 5, 6}) ret := intColl.ForPage(1, 2) ret.DD() if ret.Count() != 2 { t.Error("For page错误") } /* IntCollection(2):{ 0: 3 1: 4 } ~~~ # 展示 业务开发最核心的也就是对数组的处理,Collection封装了多种数据数组类型。 Collection包目前支持的元素类型:int, int64, float32, float64, string, struct。除了struct数组使用了反射之外,其他的数组并没有使用反射机制,效率和易用性得到一定的平衡。 使用下列几个方法进行初始化Collection: ~~~ NewIntCollection(objs []int) *IntCollection NewInt64Collection(objs []int64) *Int64Collection NewFloat64Collection(objs []float64) *Float64Collection NewFloat32Collection(objs []float32) *Float32Collection NewStrCollection(objs []string) *StrCollection NewObjCollection(objs interface{}) *ObjCollection ~~~ 所有的初始化函数都是很方便的将要初始化的slice传递进入,返回了一个实现了ICollection的具体对象 ## 格式展示 首先业务是很需要进行代码调试的,这里封装了一个 DD 方法,能按照友好的格式展示这个 Collection ~~~ a1 := Foo{A: "a1"} a2 := Foo{A: "a2"} objColl := NewObjCollection([]Foo{a1, a2}) objColl.DD() ~~~ # 查找功能 在一个数组中查找对应的元素,这个是非常常见的功能 ~~~ Search(item interface{}) int ~~~ 查找Collection中第一个匹配查询元素的下标,如果存在,**返回下标**;如果不存在,返回-1 注意 此函数要求设置compare方法,基础元素数组(int, int64, float32, float64, string)可直接调用! ~~~ intColl := collection.NewIntCollection([]int{1,2}) fmt.Println(intColl.Search(2)) intColl = collection.NewIntCollection([]int{1,2, 3, 3, 2}) fmt.Println(intColl.Search(3)) ~~~ # 排重功能Unique 将Collection中重复的元素进行合并,返回唯一的一个数组。 ~~~ intColl := NewIntCollection([]int{1,2, 3, 3, 2}) uniqColl := intColl.Unique() if uniqColl.Count() != 3 { t.Error("Unique 重复错误") } uniqColl.DD() ~~~ # 获取最后一个 获取该Collection中满足过滤的最后一个元素,如果没有填写过滤条件,默认返回最后一个元素 ~~~ intColl := NewIntCollection([]int{1, 2, 3, 4, 3, 2}) last, err := intColl.Last().ToInt() if err != nil { t.Error("last get error") } if last != 2 { t.Error("last 获取错误") } last, err = intColl.Last(func(item interface{}, key int) bool { i := item.(int) return i > 2 }).ToInt() if err != nil { t.Error("last get error") } if last != 3 { t.Error("last 获取错误") } ~~~ # Map/reduce ## Map `Map(func(item interface{}, key int) interface{}) ICollection` 对Collection中的每个函数都进行一次函数调用,并将返回值组装成ICollection 这个回调函数形如:`func(item interface{}, key int) interface{}` 如果希望在某此调用的时候中止,就在此次调用的时候设置Collection的Error,就可以中止,且此次回调函数生成的结构不合并到最终生成的ICollection ~~~ intColl := collection.NewIntCollection([]int{1, 2, 3, 4}) newIntColl := intColl.Map(func(item interface{}, key int) interface{} { v := item.(int) return v * 2 }) newIntColl.DD() if newIntColl.Count() != 4 { fmt.Println("Map错误") } ~~~ **中止map** ~~~ intColl := collection.NewIntCollection([]int{1, 2, 3, 4}) newIntColl2 := intColl.Map(func(item interface{}, key int) interface{} { v := item.(int) if key > 2 { intColl.SetErr(errors.New("break")) return nil } return v * 2 }) newIntColl2.DD() ~~~ ## Reduce `Reduce(func(carry IMix, item IMix) IMix) IMix` 对Collection中的所有元素进行聚合计算。 如果希望在某次调用的时候中止,在此次调用的时候设置Collection的Error,就可以中止调用 ~~~ intColl := collection.NewIntCollection([]int{1, 2, 3, 4}) sumMix := intColl.Reduce(func(carry collection.IMix, item collection.IMix) collection.IMix { carryInt, _ := carry.ToInt() itemInt, _ := item.ToInt() return collection.NewMix(carryInt + itemInt) }) sumMix.DD() sum, err := sumMix.ToInt() if err != nil { fmt.Println(err.Error()) } if sum != 10 { fmt.Println("Reduce计算错误") } ~~~ # sort排列 将Collection中的元素进行升序排列输出 ~~~ intColl := NewIntCollection([]int{2, 4, 3}) intColl2 := intColl.Sort() if intColl2.Err() != nil { fmt.Println(intColl2.Err()) } intColl2.DD() ~~~ # SortDesc降序 `SortDesc() ICollection` 将Collection中的元素按照降序排列输出,必须设置compare函数 ~~~go intColl := NewIntCollection([]int{2, 4, 3}) intColl2 := intColl.SortDesc() if intColl2.Err() != nil { t.Error(intColl2.Err()) } intColl2.DD() ~~~ # SortBy `SortBy(key string) ICollection` 根据对象数组中的某个元素进行Collection升序排列。这个元素必须是Public元素 注:这个函数只对ObjCollection生效。这个对象数组的某个元素必须是基础类型。 ~~~go type Foo struct { A string B int } func TestObjCollection_SortBy(t *testing.T) { a1 := Foo{A: "a1", B: 3} a2 := Foo{A: "a2", B: 2} objColl := NewObjCollection([]Foo{a1, a2}) newObjColl := objColl.SortBy("B") newObjColl.DD() obj, err := newObjColl.Index(0).ToInterface() if err != nil { t.Error(err) } foo := obj.(Foo) if foo.B != 2 { t.Error("SortBy error") } } /* ObjCollection(2)(collection.Foo):{ 0: {A:a2 B:2} 1: {A:a1 B:3} } */ ~~~ # SortByDesc `SortByDesc(key string) ICollection` 根据对象数组中的某个元素进行Collection降序排列。这个元素必须是Public元素 注:这个函数只对ObjCollection生效。这个对象数组的某个元素必须是基础类型。 ~~~ type Foo struct { A string B int } func collection_SortByDesc() { a1 := Foo{A: "a1", B: 2} a2 := Foo{A: "a2", B: 3} objColl := NewObjCollection([]Foo{a1, a2}) newObjColl := objColl.SortByDesc("B") newObjColl.DD() obj, _ := newObjColl.Index(0).ToInterface() foo := obj.(Foo) fmt.Println(foo) } ~~~ # 合并join `Join(split string, format ...func(item interface{}) string) string` 将Collection中的元素按照某种方式聚合成字符串。该函数接受一个或者两个参数,第一个参数是聚合字符串的分隔符号,第二个参数是聚合时候每个元素的格式化函数,如果没有设置第二个参数,则使用fmt.Sprintf("%v")来该格式化 ~~~ intColl := NewIntCollection([]int{2, 4, 3}) out := intColl.Join(",") fmt.Println(out) out = intColl.Join(",", func(item interface{}) string { return fmt.Sprintf("'%d'", item.(int)) }) fmt.Println(out) ~~~