From 99faeccb0549ec7a7729b6d89d32abaa8e29329f Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 27 Dec 2021 19:54:04 +0800 Subject: [PATCH] feat: add Intersection, Union, Without func for slice/slice.go --- README.md | 5 ++++- README_zh-CN.md | 5 ++++- slice/slice.go | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5551771..d5c2b6e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.0.10-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.0-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) @@ -391,6 +391,7 @@ func Filter(slice, function interface{}) interface{} //filter slice, function si func Find(slice, function interface{}) interface{} //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool . func IntSlice(slice interface{}) ([]int, error) //convert value to int slice func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice +func Intersection(slices ...interface{}) interface{} //creates a slice of unique values that included by all slices. func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //insert the element into slice at index. func Map(slice, function interface{}) interface{} //map lisce, function signature should be func(index int, value interface{}) interface{} func ReverseSlice(slice interface{}) //revere slice @@ -399,7 +400,9 @@ func SortByField(slice interface{}, field string, sortType ...string) error //so func Some(slice, function interface{}) bool //return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool func StringSlice(slice interface{}) []string //convert value to string slice func Unique(slice interface{}) interface{} //remove duplicate elements in slice +func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons. func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index. +func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values ``` #### 10. strutil is for processing string diff --git a/README_zh-CN.md b/README_zh-CN.md index e999965..cc5bab8 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -6,7 +6,7 @@
![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.0.10-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.1.0-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![codecov](https://codecov.io/gh/duke-git/lancet/branch/main/graph/badge.svg?token=FC48T1F078)](https://codecov.io/gh/duke-git/lancet) @@ -392,6 +392,7 @@ func Find(slice, function interface{}) interface{} //查找slice中第一个符 func Filter(slice, function interface{}) interface{} //过滤slice, 函数签名:func(index int, value interface{}) bool func IntSlice(slice interface{}) ([]int, error) //转成int切片 func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片 +func Intersection(slices ...interface{}) interface{} //slice交集,去重 func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置插入value func Map(slice, function interface{}) interface{} //遍历切片, 函数签名:func(index int, value interface{}) interface{} func ReverseSlice(slice interface{}) //反转切片 @@ -400,7 +401,9 @@ func Some(slice, function interface{}) bool //slice中任意一个元素都符 func SortByField(slice interface{}, field string, sortType ...string) error //对struct切片进行排序 func StringSlice(slice interface{}) []string //转为string切片 func Unique(slice interface{}) interface{} //去重切片 +func Union(slices ...interface{}) interface{} //slice并集, 去重 func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value +func Without(slice interface{}, values ...interface{}) interface{} //slice去除values ``` #### 10. strutil字符串处理包 diff --git a/slice/slice.go b/slice/slice.go index 99376e2..321efea 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -417,7 +417,7 @@ func Unique(slice interface{}) interface{} { } -// Union creates an slice of unique values, in order, from all given slices. using == for equality comparisons. +// Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons. func Union(slices ...interface{}) interface{} { if len(slices) == 0 { return nil @@ -443,7 +443,7 @@ func Union(slices ...interface{}) interface{} { return Unique(res.Interface()) } -// Intersection creates an slice of unique values that included by all slices. +// Intersection creates a slice of unique values that included by all slices. func Intersection(slices ...interface{}) interface{} { if len(slices) == 0 { return nil @@ -541,7 +541,7 @@ func SortByField(slice interface{}, field string, sortType ...string) error { return nil } -// Without creates an array excluding all given values +// Without creates a slice excluding all given values func Without(slice interface{}, values ...interface{}) interface{} { sv := sliceValue(slice) if sv.Len() == 0 {