Compare commits

...
3 Commits
Author SHA1 Message Date
dudaodong d8505d1a5f doc: update doc for slice package 2023-01-05 14:49:30 +08:00
dudaodong 6498c7d68a test: add examples for slice package 2023-01-05 11:26:56 +08:00
dudaodong 5f0211f0c4 test: add examples for slice package 2023-01-04 14:44:10 +08:00
6 changed files with 1187 additions and 154 deletions

No files matched your search

+161 -53
View File
@@ -586,59 +586,167 @@ import "github.com/duke-git/lancet/v2/slice"
#### Function list:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ContainSubSlice)
- [Chunk](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Chunk)
- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Compact)
- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Concat)
- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Count)
- [CountBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#CountBy)
- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Difference)
- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceBy)
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
- [DeleteAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DeleteAt)
- [Drop](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Drop)
- [Equal](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Equal)
- [EqualWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#EqualWith)
- [Every](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Every)
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Filter)
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Find)
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)
- [Flatten](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)
- [FlattenDeep](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FlattenDeep)
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
- [IntSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
- [InterfaceSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InsertAt)
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IndexOf)
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#LastIndexOf)
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Map)
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Merge)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reverse)
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce)
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Replace)
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReplaceAll)
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Repeat)
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Shuffle)
- [Sort](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Sort)
- [SortBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortBy)
- [SortByField<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
- [StringSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SymmetricDifference)
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlice)
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlicePointer)
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UniqueBy)
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
- [UnionBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UnionBy)
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)
- [KeyBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)
- **<big>AppendIfAbsent</big>** : if the item is absent,append it to the slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#AppendIfAbsent)]
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
- **<big>Contain</big>** : check if the value is in the slice or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Contain)]
[[play](https://go.dev/play/p/_454yEHcNjf)]
- **<big>ContainSubSlice</big>** : check if the slice contain a given subslice or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ContainSubSlice)]
[[play](https://go.dev/play/p/bcuQ3UT6Sev)]
- **<big>Chunk</big>** : creates a slice of elements split into groups the length of size.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Chunk)]
[[play](https://go.dev/play/p/b4Pou5j2L_C)]
- **<big>Compact</big>** : creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Compact)]
[[play](https://go.dev/play/p/pO5AnxEr3TK)]
- **<big>Concat</big>** : creates a new slice concatenating slice with any additional slices.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Concat)]
[[play](https://go.dev/play/p/gPt-q7zr5mk)]
- **<big>Count</big>** : returns the number of occurrences of the given item in the slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Count)]
[[play](https://go.dev/play/p/Mj4oiEnQvRJ)]
- **<big>CountBy</big>** : iterates over elements of slice with predicate function, returns the number of all matched elements.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#CountBy)]
[[play](https://go.dev/play/p/tHOccTMDZCC)]
- **<big>Difference</big>** : creates an slice of whose element in slice but not in compared slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Difference)]
[[play](https://go.dev/play/p/VXvadzLzhDa)]
- **<big>DifferenceBy</big>** : accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceBy)]
[[play](https://go.dev/play/p/DiivgwM5OnC)]
- **<big>DifferenceWith</big>** : accepts comparator which is invoked to compare elements of slice to values.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)]
[[play](https://go.dev/play/p/v2U2deugKuV)]
- **<big>DeleteAt</big>** : delete the element of slice from specific start index to end index - 1.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DeleteAt)]
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
- **<big>Drop</big>** : creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Drop)]
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
- **<big>Equal</big>** : checks if two slices are equal: the same length and all elements' order and value are equal.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Equal)]
[[play](https://go.dev/play/p/WcRQJ37ifPa)]
- **<big>EqualWith</big>** : checks if two slices are equal with comparator func.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#EqualWith)]
[[play](https://go.dev/play/p/b9iygtgsHI1)]
- **<big>Every</big>** : return true if all of the values in the slice pass the predicate function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Every)]
[[play](https://go.dev/play/p/R8U6Sl-j8cD)]
- **<big>Filter</big>** : iterates over elements of slice, returning an slice of all elements pass the predicate function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Filter)]
[[play](https://go.dev/play/p/SdPna-7qK4T)]
- **<big>Find</big>** : iterates over elements of slice, returning the first one that passes a truth test on predicate function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Find)]
[[play](https://go.dev/play/p/CBKeBoHVLgq)]
- **<big>FindLast</big>** : return the last item that passes a truth test on predicate function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)]
[[play](https://go.dev/play/p/FFDPV_j7URd)]
- **<big>Flatten</big>** : flattens slice one level.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)]
[[play](https://go.dev/play/p/hYa3cBEevtm)]
- **<big>FlattenDeep</big>** : flattens slice recursive to one level.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FlattenDeep)]
[[play](https://go.dev/play/p/yjYNHPyCFaF)]
- **<big>ForEach</big>** : iterates over elements of slice and invokes function for each element.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)]
[[play](https://go.dev/play/p/DrPaa4YsHRF)]
- **<big>GroupBy</big>** : iterate over elements of the slice, each element will be group by criteria, returns two slices.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)]
[[play](https://go.dev/play/p/QVkPxzPR0iA)]
- **<big>GroupWith</big>** : return a map composed of keys generated from the resultults of running each element of slice thru iteratee.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)]
[[play](https://go.dev/play/p/ApCvMNTLO8a)]
- **<big>IntSlice<sup>deprecated</sup></big>** : convert param to int slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)]
[[play](https://go.dev/play/p/FdQXF0Vvqs-)]
- **<big>InterfaceSlice<sup>deprecated</sup></big>** : convert param to interface slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)]
[[play](https://go.dev/play/p/FdQXF0Vvqs-)]
- **<big>Intersection</big>** : creates a slice of unique elements that included by all slices.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)]
[[play](https://go.dev/play/p/anJXfB5wq_t)]
- **<big>InsertAt</big>** : insert the value or other slice into slice at index.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InsertAt)]
[[play](https://go.dev/play/p/hMLNxPEGJVE)]
- **<big>IndexOf</big>** : returns the index at which the first occurrence of an item is found in a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IndexOf)]
[[play](https://go.dev/play/p/MRN1f0FpABb)]
- **<big>LastIndexOf</big>** : returns the index at which the last occurrence of the item is found in a slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#LastIndexOf)]
[[play](https://go.dev/play/p/DokM4cf1IKH)]
- **<big>Map</big>** : creates an slice of values by running each element of slice thru iteratee function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Map)]
[[play](https://go.dev/play/p/biaTefqPquw)]
- **<big>Merge</big>** : merge all given slices into one slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Merge)]
[[play](https://go.dev/play/p/lbjFp784r9N)]
- **<big>Reverse</big>** : return slice of element order is reversed to the given slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reverse)]
[[play](https://go.dev/play/p/8uI8f1lwNrQ)]
- **<big>Reduce</big>** : creates an slice of values by running each element of slice thru iteratee function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce)]
[[play](https://go.dev/play/p/_RfXJJWIsIm)]
- **<big>Replace</big>** : returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Replace)]
[[play](https://go.dev/play/p/P5mZp7IhOFo)]
- **<big>ReplaceAll</big>** : returns a copy of the slice with all non-overlapping instances of old replaced by new.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReplaceAll)]
[[play](https://go.dev/play/p/CzqXMsuYUrx)]
- **<big>Repeat</big>** : creates a slice with length n whose elements are passed item.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Repeat)]
[[play](https://go.dev/play/p/1CbOmtgILUU)]
- **<big>Shuffle</big>** : shuffle the slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Shuffle)]
[[play](https://go.dev/play/p/YHvhnWGU3Ge)]
- **<big>Sort</big>** : sorts a slice of any ordered type(number or string).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Sort)]
[[play](https://go.dev/play/p/V9AVjzf_4Fk)]
- **<big>SortBy</big>** : sorts the slice in ascending order as determined by the less function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortBy)]
[[play](https://go.dev/play/p/DAhLQSZEumm)]
- **<big>SortByField<sup>deprecated</sup></big>** : return sorted slice by specific field.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)]
[[play](https://go.dev/play/p/fU1prOBP9p1)]
- **<big>Some</big>** : return true if any of the values in the list pass the predicate function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)]
[[play](https://go.dev/play/p/4pO9Xf9NDGS)]
- **<big>StringSlice<sup>deprecated</sup></big>** : convert param to slice of string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)]
[[play](https://go.dev/play/p/W0TZDWCPFcI)]
- **<big>SymmetricDifference</big>** : the symmetric difference of two slice, also known as the disjunctive union.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#h42nJX5xMln)]
[[play](https://go.dev/play/p/1CbOmtgILUU)]
- **<big>ToSlice</big>** : returns a slices of a variable parameter transformation.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlice)]
[[play](https://go.dev/play/p/YzbzVq5kscN)]
- **<big>ToSlicePointer</big>** : returns a pointer to the slices of a variable parameter transformation.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlicePointer)]
[[play](https://go.dev/play/p/gx4tr6_VXSF)]
- **<big>Unique</big>** : remove duplicate elements in slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)]
[[play](https://go.dev/play/p/AXw0R3ZTE6a)]
- **<big>UniqueBy</big>** : call iteratee func with every item of slice, then remove duplicated.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UniqueBy)]
[[play](https://go.dev/play/p/UR323iZLDpv)]
- **<big>Union</big>** : creates a slice of unique elements, in order, from all given slices.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)]
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
- **<big>UnionBy</big>** : accepts iteratee which is invoked for each element of each slice, then union slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UnionBy)]
[[play](https://go.dev/play/p/HGKHfxKQsFi)]
- **<big>UpdateAt</big>** : update the slice element at index.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)]
[[play](https://go.dev/play/p/f3mh2KloWVm)]
- **<big>Without</big>** : creates a slice excluding all given items.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)]
[[play](https://go.dev/play/p/bwhEXEypThg)]
- **<big>KeyBy</big>** : converts a slice to a map based on a callback function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#KeyBy)]
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
### 17. Strutil package contains some functions to manipulate string.
+160 -52
View File
@@ -584,58 +584,166 @@ import "github.com/duke-git/lancet/v2/slice"
```
#### 函数列表:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ContainSubSlice)
- [Chunk](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Chunk)
- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Compact)
- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Concat)
- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Count)
- [CountBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#CountBy)
- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Difference)
- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DifferenceBy)
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
- [DeleteAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DeleteAt)
- [Drop](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Drop)
- [Every](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Every)
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Filter)
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Find)
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)
- [Flatten](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)
- [FlattenDeep](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FlattenDeep)
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
- [IntSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
- [InterfaceSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InsertAt)
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IndexOf)
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#LastIndexOf)
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Map)
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Merge)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reverse)
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reduce)
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Replace)
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReplaceAll)
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Repeat)
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Shuffle)
- [Sort](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Sort)
- [SortBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortBy)
- [SortByField<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
- [StringSlice<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SymmetricDifference)
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlice)
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlicePointer)
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UniqueBy)
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UniqueBy)
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
- [KeyBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)
- **<big>AppendIfAbsent</big>** : 当前切片中不包含值时,将该值追加到切片中。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#AppendIfAbsent)]
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
- **<big>Contain</big>** : 判断slice是否包含value。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Contain)]
[[play](https://go.dev/play/p/_454yEHcNjf)]
- **<big>ContainSubSlice</big>** : 判断slice是否包含subslice。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ContainSubSlice)]
[[play](https://go.dev/play/p/bcuQ3UT6Sev)]
- **<big>Chunk</big>** : 按照size参数均分slice。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Chunk)]
[[play](https://go.dev/play/p/b4Pou5j2L_C)]
- **<big>Compact</big>** : 去除slice中的假值(false values are false, nil, 0, "")。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Compact)]
[[play](https://go.dev/play/p/pO5AnxEr3TK)]
- **<big>Concat</big>** : 合并多个slices到一个slice中。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Concat)]
[[play](https://go.dev/play/p/gPt-q7zr5mk)]
- **<big>Count</big>** : 返回切片中指定元素的个数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Count)]
[[play](https://go.dev/play/p/Mj4oiEnQvRJ)]
- **<big>CountBy</big>** : 遍历切片,对每个元素执行函数predicate. 返回符合函数返回值为true的元素的个数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#CountBy)]
[[play](https://go.dev/play/p/tHOccTMDZCC)]
- **<big>Difference</big>** : 创建一个切片,其元素不包含在另一个给定切片中。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Difference)]
[[play](https://go.dev/play/p/VXvadzLzhDa)]
- **<big>DifferenceBy</big>** : 将两个slice中的每个元素调用iteratee函数,并比较它们的返回值,如果不相等返回在slice中对应的值。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DifferenceBy)]
[[play](https://go.dev/play/p/DiivgwM5OnC)]
- **<big>DifferenceWith</big>** : 接受比较器函数,该比较器被调用以将切片的元素与值进行比较。 结果值的顺序和引用由第一个切片确定。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DifferenceWith)]
[[play](https://go.dev/play/p/v2U2deugKuV)]
- **<big>DeleteAt</big>** : 删除切片中指定开始索引到结束索引的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DeleteAt)]
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
- **<big>Drop</big>** : 创建一个切片,当n > 0时从开头删除n个元素,或者当n < 0时从结尾删除n个元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Drop)]
[[play](https://go.dev/play/p/pJ-d6MUWcvK)]
- **<big>Equal</big>** : 检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Equal)]
[[play](https://go.dev/play/p/WcRQJ37ifPa)]
- **<big>EqualWith</big>** : 检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#EqualWith)]
[[play](https://go.dev/play/p/b9iygtgsHI1)]
- **<big>Every</big>** : 如果切片中的所有值都通过谓词函数,则返回true。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Every)]
[[play](https://go.dev/play/p/R8U6Sl-j8cD)]
- **<big>Filter</big>** : 返回切片中通过predicate函数真值测试的所有元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Filter)]
[[play](https://go.dev/play/p/SdPna-7qK4T)]
- **<big>Find</big>** : 遍历切片的元素,返回第一个通过predicate函数真值测试的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Find)]
[[play](https://go.dev/play/p/CBKeBoHVLgq)]
- **<big>FindLast</big>** : 从头到尾遍历slice的元素,返回最后一个通过predicate函数真值测试的元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)]
[[play](https://go.dev/play/p/FFDPV_j7URd)]
- **<big>Flatten</big>** : 将多维切片展平一层。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)]
[[play](https://go.dev/play/p/hYa3cBEevtm)]
- **<big>FlattenDeep</big>** : 将多维切片递归展平到一层。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FlattenDeep)]
[[play](https://go.dev/play/p/yjYNHPyCFaF)]
- **<big>ForEach</big>** : 遍历切片的元素并为每个元素调用iteratee函数。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)]
[[play](https://go.dev/play/p/DrPaa4YsHRF)]
- **<big>GroupBy</big>** : 迭代切片的元素,每个元素将按条件分组,返回两个切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)]
[[play](https://go.dev/play/p/QVkPxzPR0iA)]
- **<big>GroupWith</big>** : 创建一个map,key是iteratee遍历slice中的每个元素返回的结果。值是切片元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)]
[[play](https://go.dev/play/p/ApCvMNTLO8a)]
- **<big>IntSlice<sup>deprecated</sup></big>** : 将接口切片转换为int切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)]
[[play](https://go.dev/play/p/FdQXF0Vvqs-)]
- **<big>InterfaceSlice<sup>deprecated</sup></big>** : 将值转换为interface切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)]
[[play](https://go.dev/play/p/FdQXF0Vvqs-)]
- **<big>Intersection</big>** : 返回多个切片的交集。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)]
[[play](https://go.dev/play/p/anJXfB5wq_t)]
- **<big>InsertAt</big>** : 将元素插入到索引处的切片中。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InsertAt)]
[[play](https://go.dev/play/p/hMLNxPEGJVE)]
- **<big>IndexOf</big>** : 返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IndexOf)]
[[play](https://go.dev/play/p/MRN1f0FpABb)]
- **<big>LastIndexOf</big>** : 返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#LastIndexOf)]
[[play](https://go.dev/play/p/DokM4cf1IKH)]
- **<big>Map</big>** : 对slice中的每个元素执行map函数以创建一个新切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Map)]
[[play](https://go.dev/play/p/biaTefqPquw)]
- **<big>Merge</big>** : 合并多个切片(不会消除重复元素)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Merge)]
[[play](https://go.dev/play/p/lbjFp784r9N)]
- **<big>Reverse</big>** : 反转切片中的元素顺序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reverse)]
[[play](https://go.dev/play/p/8uI8f1lwNrQ)]
- **<big>Reduce</big>** : 将切片中的元素依次运行iteratee函数,返回运行结果。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reduce)]
[[play](https://go.dev/play/p/_RfXJJWIsIm)]
- **<big>Replace</big>** : 返回切片的副本,其中前n个不重叠的old替换为new。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Replace)]
[[play](https://go.dev/play/p/P5mZp7IhOFo)]
- **<big>ReplaceAll</big>** : 返回切片的副本,将其中old全部替换为new。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReplaceAll)]
[[play](https://go.dev/play/p/CzqXMsuYUrx)]
- **<big>Repeat</big>** : 创建一个切片,包含n个传入的item。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Repeat)]
[[play](https://go.dev/play/p/1CbOmtgILUU)]
- **<big>Shuffle</big>** : 随机打乱切片中的元素顺序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Shuffle)]
[[play](https://go.dev/play/p/YHvhnWGU3Ge)]
- **<big>Sort</big>** : 对任何有序类型(数字或字符串)的切片进行排序,使用快速排序算法。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Sort)]
[[play](https://go.dev/play/p/V9AVjzf_4Fk)]
- **<big>SortBy</big>** : 按照less函数确定的升序规则对切片进行排序。排序不保证稳定性。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortBy)]
[[play](https://go.dev/play/p/DAhLQSZEumm)]
- **<big>SortByField<sup>deprecated</sup></big>** : 按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)]
[[play](https://go.dev/play/p/fU1prOBP9p1)]
- **<big>Some</big>** : 如果列表中的任何值通过谓词函数,则返回true。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)]
[[play](https://go.dev/play/p/4pO9Xf9NDGS)]
- **<big>StringSlice<sup>deprecated</sup></big>** : 将接口切片转换为字符串切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)]
[[play](https://go.dev/play/p/W0TZDWCPFcI)]
- **<big>SymmetricDifference</big>** : 返回一个切片,其中的元素存在于参数切片中,但不同时存储在于参数切片中(交集取反)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#h42nJX5xMln)]
[[play](https://go.dev/play/p/1CbOmtgILUU)]
- **<big>ToSlice</big>** : 将可变参数转为切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlice)]
[[play](https://go.dev/play/p/YzbzVq5kscN)]
- **<big>ToSlicePointer</big>** : 将可变参数转为指针切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlicePointer)]
[[play](https://go.dev/play/p/gx4tr6_VXSF)]
- **<big>Unique</big>** : 删除切片中的重复元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)]
[[play](https://go.dev/play/p/AXw0R3ZTE6a)]
- **<big>UniqueBy</big>** : 对切片的每个元素调用iteratee函数,然后删除重复元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UniqueBy)]
[[play](https://go.dev/play/p/UR323iZLDpv)]
- **<big>Union</big>** : 合并多个切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)]
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
- **<big>UnionBy</big>** : 对切片的每个元素调用函数后,合并多个切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UnionBy)]
[[play](https://go.dev/play/p/HGKHfxKQsFi)]
- **<big>UpdateAt</big>** : 更新索引处的切片元素。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)]
[[play](https://go.dev/play/p/f3mh2KloWVm)]
- **<big>Without</big>** : 创建一个不包括所有给定值的切片。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)]
[[play](https://go.dev/play/p/bwhEXEypThg)]
- **<big>KeyBy</big>** :将切片每个元素调用函数后转为map。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)]
[[play](https://go.dev/play/p/uXod2LWD1Kg)]
### 17. strutil 包含字符串处理的相关函数。
+4 -4
View File
@@ -319,7 +319,7 @@ func main() {
### <span id="DifferenceBy">DifferenceBy</span>
<p>在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值</p>
<p>将两个slice中的每个元素调用iteratee函数,并比较它们的返回值,如果不相等返回在slice中对应的值</p>
<b>函数签名:</b>
@@ -349,7 +349,7 @@ func main() {
### <span id="DifferenceWith">DifferenceWith</span>
<p>DifferenceWith 接受比较器,该比较器被调用以将切片的元素与值进行比较。 结果值的顺序和引用由第一个切片确定</p>
<p>接受比较器函数,该比较器被调用以将切片的元素与值进行比较。 结果值的顺序和引用由第一个切片确定</p>
<b>函数签名:</b>
@@ -379,7 +379,7 @@ func main() {
### <span id="DeleteAt">DeleteAt</span>
<p>删除切片中从开始索引到结束索引-1的元素</p>
<p>删除切片中指定开始索引到结束索引的元素</p>
<b>函数签名:</b>
@@ -407,7 +407,7 @@ func main() {
### <span id="Drop">Drop</span>
<p>创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素</p>
<p>创建一个切片,当n > 0时从开头删除n个元素,或者当n < 0时从结尾删除n个元素</p>
<b>函数签名:</b>
+103 -44
View File
@@ -10,6 +10,7 @@ import (
"math/rand"
"reflect"
"sort"
"time"
"golang.org/x/exp/constraints"
)
@@ -21,7 +22,8 @@ var (
memoryHashCounter = make(map[string]int)
)
// Contain check if the target value is in the slice or not
// Contain check if the target value is in the slice or not.
// Play: https://go.dev/play/p/_454yEHcNjf
func Contain[T comparable](slice []T, target T) bool {
for _, item := range slice {
if item == target {
@@ -32,7 +34,8 @@ func Contain[T comparable](slice []T, target T) bool {
return false
}
// ContainSubSlice check if the slice contain a given subslice or not
// ContainSubSlice check if the slice contain a given subslice or not.
// Play: https://go.dev/play/p/bcuQ3UT6Sev
func ContainSubSlice[T comparable](slice, subSlice []T) bool {
for _, v := range subSlice {
if !Contain(slice, v) {
@@ -43,7 +46,8 @@ func ContainSubSlice[T comparable](slice, subSlice []T) bool {
return true
}
// Chunk creates a slice of elements split into groups the length of size
// Chunk creates a slice of elements split into groups the length of size.
// Play: https://go.dev/play/p/b4Pou5j2L_C
func Chunk[T any](slice []T, size int) [][]T {
result := [][]T{}
@@ -62,14 +66,15 @@ func Chunk[T any](slice []T, size int) [][]T {
}
return result
}
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey
// Compact creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.
// Play: https://go.dev/play/p/pO5AnxEr3TK
func Compact[T comparable](slice []T) []T {
var zero T
result := []T{}
for _, v := range slice {
if v != zero {
result = append(result, v)
@@ -80,6 +85,7 @@ func Compact[T comparable](slice []T) []T {
}
// Concat creates a new slice concatenating slice with any additional slices.
// Play: https://go.dev/play/p/gPt-q7zr5mk
func Concat[T any](slice []T, slices ...[]T) []T {
result := append([]T{}, slice...)
@@ -90,7 +96,8 @@ func Concat[T any](slice []T, slices ...[]T) []T {
return result
}
// Difference creates an slice of whose element in slice but not in comparedSlice
// Difference creates an slice of whose element in slice but not in comparedSlice.
// Play: https://go.dev/play/p/VXvadzLzhDa
func Difference[T comparable](slice, comparedSlice []T) []T {
result := []T{}
@@ -105,7 +112,8 @@ func Difference[T comparable](slice, comparedSlice []T) []T {
// DifferenceBy it accepts iteratee which is invoked for each element of slice
// and values to generate the criterion by which they're compared.
// like lodash.js differenceBy: https://lodash.com/docs/4.17.15#differenceBy,
// like lodash.js differenceBy: https://lodash.com/docs/4.17.15#differenceBy.
// Play: https://go.dev/play/p/DiivgwM5OnC
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T {
orginSliceAfterMap := Map(slice, iteratee)
comparedSliceAfterMap := Map(comparedSlice, iteratee)
@@ -120,7 +128,10 @@ func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(inde
return result
}
// DifferenceWith accepts comparator which is invoked to compare elements of slice to values. The order and references of result values are determined by the first slice. The comparator is invoked with two arguments: (arrVal, othVal).
// DifferenceWith accepts comparator which is invoked to compare elements of slice to values.
// The order and references of result values are determined by the first slice.
// The comparator is invoked with two arguments: (arrVal, othVal).
// Play: https://go.dev/play/p/v2U2deugKuV
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(item1, item2 T) bool) []T {
result := make([]T, 0)
@@ -145,7 +156,8 @@ func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(item1,
return result
}
// Equal checks if two slices are equal: the same length and all elements' order and value are equal
// Equal checks if two slices are equal: the same length and all elements' order and value are equal.
// Play: https://go.dev/play/p/WcRQJ37ifPa
func Equal[T comparable](slice1, slice2 []T) bool {
if len(slice1) != len(slice2) {
return false
@@ -160,7 +172,8 @@ func Equal[T comparable](slice1, slice2 []T) bool {
return true
}
// EqualWith checks if two slices are equal with comparator func
// EqualWith checks if two slices are equal with comparator func.
// Play: https://go.dev/play/p/b9iygtgsHI1
func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool {
if len(slice1) != len(slice2) {
return false
@@ -176,6 +189,7 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo
}
// Every return true if all of the values in the slice pass the predicate function.
// Play: https://go.dev/play/p/R8U6Sl-j8cD
func Every[T any](slice []T, predicate func(index int, item T) bool) bool {
for i, v := range slice {
if !predicate(i, v) {
@@ -186,7 +200,8 @@ func Every[T any](slice []T, predicate func(index int, item T) bool) bool {
return true
}
// None return true if all the values in the slice mismatch the criteria
// None return true if all the values in the slice mismatch the criteria.
// Play: https://go.dev/play/p/KimdalUlC-T
func None[T any](slice []T, predicate func(index int, item T) bool) bool {
l := 0
for i, v := range slice {
@@ -199,6 +214,7 @@ func None[T any](slice []T, predicate func(index int, item T) bool) bool {
}
// Some return true if any of the values in the list pass the predicate function.
// Play: https://go.dev/play/p/4pO9Xf9NDGS
func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
for i, v := range slice {
if predicate(i, v) {
@@ -209,7 +225,8 @@ func Some[T any](slice []T, predicate func(index int, item T) bool) bool {
return false
}
// Filter iterates over elements of slice, returning an slice of all elements pass the predicate function
// Filter iterates over elements of slice, returning an slice of all elements pass the predicate function.
// Play: https://go.dev/play/p/SdPna-7qK4T
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
result := make([]T, 0)
@@ -222,7 +239,8 @@ func Filter[T any](slice []T, predicate func(index int, item T) bool) []T {
return result
}
// Count returns the number of occurrences of the given item in the slice
// Count returns the number of occurrences of the given item in the slice.
// Play: https://go.dev/play/p/Mj4oiEnQvRJ
func Count[T comparable](slice []T, item T) int {
count := 0
@@ -235,7 +253,8 @@ func Count[T comparable](slice []T, item T) int {
return count
}
// CountBy iterates over elements of slice with predicate function, returns the number of all matched elements
// CountBy iterates over elements of slice with predicate function, returns the number of all matched elements.
// Play: https://go.dev/play/p/tHOccTMDZCC
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int {
count := 0
@@ -248,7 +267,8 @@ func CountBy[T any](slice []T, predicate func(index int, item T) bool) int {
return count
}
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices
// GroupBy iterate over elements of the slice, each element will be group by criteria, returns two slices.
// Play: https://go.dev/play/p/QVkPxzPR0iA
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T) {
if len(slice) == 0 {
return make([]T, 0), make([]T, 0)
@@ -270,6 +290,7 @@ func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
}
// GroupWith return a map composed of keys generated from the resultults of running each element of slice thru iteratee.
// Play: https://go.dev/play/p/ApCvMNTLO8a
func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]T {
result := make(map[U][]T)
@@ -285,7 +306,8 @@ func GroupWith[T any, U comparable](slice []T, iteratee func(item T) U) map[U][]
}
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
// If return T is nil then no items matched the predicate func
// If return T is nil then no items matched the predicate func.
// Play: https://go.dev/play/p/CBKeBoHVLgq
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
index := -1
@@ -303,8 +325,10 @@ func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
return &slice[index], true
}
// FindLast iterates over elements of slice from end to begin, returning the first one that passes a truth test on predicate function.
// If return T is nil then no items matched the predicate func
// FindLast iterates over elements of slice from end to begin,
// return the first one that passes a truth test on predicate function.
// If return T is nil then no items matched the predicate func.
// Play: https://go.dev/play/p/FFDPV_j7URd
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool) {
index := -1
@@ -322,7 +346,8 @@ func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, boo
return &slice[index], true
}
// Flatten flattens slice with one level
// Flatten flattens slice with one level.
// Play: https://go.dev/play/p/hYa3cBEevtm
func Flatten(slice any) any {
sv := sliceValue(slice)
@@ -349,7 +374,8 @@ func Flatten(slice any) any {
return result.Interface()
}
// FlattenDeep flattens slice recursive
// FlattenDeep flattens slice recursive.
// Play: https://go.dev/play/p/yjYNHPyCFaF
func FlattenDeep(slice any) any {
sv := sliceValue(slice)
st := sliceElemType(sv.Type())
@@ -376,7 +402,8 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
return result
}
// ForEach iterates over elements of slice and invokes function for each element
// ForEach iterates over elements of slice and invokes function for each element.
// Play: https://go.dev/play/p/DrPaa4YsHRF
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
for i, v := range slice {
iteratee(i, v)
@@ -384,6 +411,7 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
}
// Map creates an slice of values by running each element of slice thru iteratee function.
// Play: https://go.dev/play/p/biaTefqPquw
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
result := make([]U, len(slice), cap(slice))
@@ -395,6 +423,7 @@ func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
}
// Reduce creates an slice of values by running each element of slice thru iteratee function.
// Play: https://go.dev/play/p/_RfXJJWIsIm
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T {
if len(slice) == 0 {
return initial
@@ -412,7 +441,8 @@ func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initia
return result
}
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
// Play: https://go.dev/play/p/P5mZp7IhOFo
func Replace[T comparable](slice []T, old T, new T, n int) []T {
result := make([]T, len(slice))
copy(result, slice)
@@ -428,11 +458,13 @@ func Replace[T comparable](slice []T, old T, new T, n int) []T {
}
// ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new.
// Play: https://go.dev/play/p/CzqXMsuYUrx
func ReplaceAll[T comparable](slice []T, old T, new T) []T {
return Replace(slice, old, new, -1)
}
// Repeat creates a slice with length n whose elements are param `item`.
// Play: https://go.dev/play/p/1CbOmtgILUU
func Repeat[T any](item T, n int) []T {
result := make([]T, n)
@@ -444,7 +476,8 @@ func Repeat[T any](item T, n int) []T {
}
// InterfaceSlice convert param to slice of interface.
// This function is deprecated, use generics feature of go1.18+ for replacement
// This function is deprecated, use generics feature of go1.18+ for replacement.
// Play: https://go.dev/play/p/FdQXF0Vvqs-
func InterfaceSlice(slice any) []any {
sv := sliceValue(slice)
if sv.IsNil() {
@@ -460,7 +493,8 @@ func InterfaceSlice(slice any) []any {
}
// StringSlice convert param to slice of string.
// This function is deprecated, use generics feature of go1.18+ for replacement
// This function is deprecated, use generics feature of go1.18+ for replacement.
// Play: https://go.dev/play/p/W0TZDWCPFcI
func StringSlice(slice any) []string {
v := sliceValue(slice)
@@ -477,7 +511,8 @@ func StringSlice(slice any) []string {
}
// IntSlice convert param to slice of int.
// This function is deprecated, use generics feature of go1.18+ for replacement
// This function is deprecated, use generics feature of go1.18+ for replacement.
// Play: https://go.dev/play/p/UQDj-on9TGN
func IntSlice(slice any) []int {
sv := sliceValue(slice)
@@ -494,6 +529,7 @@ func IntSlice(slice any) []int {
}
// DeleteAt delete the element of slice from start index to end index - 1.
// Play: https://go.dev/play/p/pJ-d6MUWcvK
func DeleteAt[T any](slice []T, start int, end ...int) []T {
size := len(slice)
@@ -523,7 +559,8 @@ func DeleteAt[T any](slice []T, start int, end ...int) []T {
return slice
}
// Drop creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0
// Drop creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.
// Play: https://go.dev/play/p/pJ-d6MUWcvK
func Drop[T any](slice []T, n int) []T {
size := len(slice)
@@ -543,6 +580,7 @@ func Drop[T any](slice []T, n int) []T {
}
// InsertAt insert the value or other slice into slice at index.
// Play: https://go.dev/play/p/hMLNxPEGJVE
func InsertAt[T any](slice []T, index int, value any) []T {
size := len(slice)
@@ -564,6 +602,7 @@ func InsertAt[T any](slice []T, index int, value any) []T {
}
// UpdateAt update the slice element at index.
// Play: https://go.dev/play/p/f3mh2KloWVm
func UpdateAt[T any](slice []T, index int, value T) []T {
size := len(slice)
@@ -576,6 +615,7 @@ func UpdateAt[T any](slice []T, index int, value T) []T {
}
// Unique remove duplicate elements in slice.
// Play: https://go.dev/play/p/AXw0R3ZTE6a
func Unique[T comparable](slice []T) []T {
result := []T{}
@@ -597,6 +637,7 @@ func Unique[T comparable](slice []T) []T {
}
// UniqueBy call iteratee func with every item of slice, then remove duplicated.
// Play: https://go.dev/play/p/UR323iZLDpv
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
result := []T{}
@@ -609,6 +650,7 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
}
// Union creates a slice of unique elements, in order, from all given slices.
// Play: https://go.dev/play/p/hfXV1iRIZOf
func Union[T comparable](slices ...[]T) []T {
result := []T{}
contain := map[T]struct{}{}
@@ -625,7 +667,8 @@ func Union[T comparable](slices ...[]T) []T {
return result
}
// UnionBy is like Union, what's more it accepts iteratee which is invoked for each element of each slice
// UnionBy is like Union, what's more it accepts iteratee which is invoked for each element of each slice.
// Play: https://go.dev/play/p/HGKHfxKQsFi
func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
result := []T{}
contain := map[V]struct{}{}
@@ -643,7 +686,8 @@ func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T {
return result
}
// Merge all given slices into one slice
// Merge all given slices into one slice.
// Play: https://go.dev/play/p/lbjFp784r9N
func Merge[T any](slices ...[]T) []T {
result := make([]T, 0)
@@ -655,6 +699,7 @@ func Merge[T any](slices ...[]T) []T {
}
// Intersection creates a slice of unique elements that included by all slices.
// Play: https://go.dev/play/p/anJXfB5wq_t
func Intersection[T comparable](slices ...[]T) []T {
if len(slices) == 0 {
return []T{}
@@ -691,7 +736,8 @@ func Intersection[T comparable](slices ...[]T) []T {
return result
}
// SymmetricDifference oppoiste operation of intersection function
// SymmetricDifference oppoiste operation of intersection function.
// Play: https://go.dev/play/p/h42nJX5xMln
func SymmetricDifference[T comparable](slices ...[]T) []T {
if len(slices) == 0 {
return []T{}
@@ -717,25 +763,29 @@ func SymmetricDifference[T comparable](slices ...[]T) []T {
return Unique(result)
}
// Reverse return slice of element order is reversed to the given slice
// Reverse return slice of element order is reversed to the given slice.
// Play: https://go.dev/play/p/8uI8f1lwNrQ
func Reverse[T any](slice []T) {
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
slice[i], slice[j] = slice[j], slice[i]
}
}
// Shuffle creates an slice of shuffled values
// Shuffle the slice.
// Play: https://go.dev/play/p/YHvhnWGU3Ge
func Shuffle[T any](slice []T) []T {
result := make([]T, len(slice))
for i, v := range rand.Perm(len(slice)) {
result[i] = slice[v]
}
rand.Seed(time.Now().UnixNano())
return result
rand.Shuffle(len(slice), func(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
})
return slice
}
// Sort sorts a slice of any ordered type(number or string), use quick sort algrithm.
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`.
// Play: https://go.dev/play/p/V9AVjzf_4Fk
func Sort[T constraints.Ordered](slice []T, sortOrder ...string) {
if len(sortOrder) > 0 && sortOrder[0] == "desc" {
quickSort(slice, 0, len(slice)-1, "desc")
@@ -745,7 +795,8 @@ func Sort[T constraints.Ordered](slice []T, sortOrder ...string) {
}
// SortBy sorts the slice in ascending order as determined by the less function.
// This sort is not guaranteed to be stable
// This sort is not guaranteed to be stable.
// Play: https://go.dev/play/p/DAhLQSZEumm
func SortBy[T any](slice []T, less func(a, b T) bool) {
quickSortBy(slice, 0, len(slice)-1, less)
}
@@ -753,7 +804,8 @@ func SortBy[T any](slice []T, less func(a, b T) bool) {
// SortByField return sorted slice by field
// slice element should be struct, field type should be int, uint, string, or bool
// default sortType is ascending (asc), if descending order, set sortType to desc
// This function is deprecated, use Sort and SortBy for replacement
// This function is deprecated, use Sort and SortBy for replacement.
// Play: https://go.dev/play/p/fU1prOBP9p1
func SortByField(slice any, field string, sortType ...string) error {
sv := sliceValue(slice)
t := sv.Type().Elem()
@@ -823,7 +875,8 @@ func SortByField(slice any, field string, sortType ...string) error {
return nil
}
// Without creates a slice excluding all given items
// Without creates a slice excluding all given items.
// Play: https://go.dev/play/p/bwhEXEypThg
func Without[T comparable](slice []T, items ...T) []T {
if len(items) == 0 || len(slice) == 0 {
return slice
@@ -840,6 +893,7 @@ func Without[T comparable](slice []T, items ...T) []T {
}
// IndexOf returns the index at which the first occurrence of an item is found in a slice or return -1 if the item cannot be found.
// Play: https://go.dev/play/p/MRN1f0FpABb
func IndexOf[T comparable](arr []T, val T) int {
limit := 10
// gets the hash value of the array as the key of the hash table.
@@ -881,6 +935,7 @@ func IndexOf[T comparable](arr []T, val T) int {
}
// LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
// Play: https://go.dev/play/p/DokM4cf1IKH
func LastIndexOf[T comparable](slice []T, item T) int {
for i := len(slice) - 1; i > 0; i-- {
if item == slice[i] {
@@ -891,7 +946,8 @@ func LastIndexOf[T comparable](slice []T, item T) int {
return -1
}
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation
// ToSlicePointer returns a pointer to the slices of a variable parameter transformation.
// Play: https://go.dev/play/p/gx4tr6_VXSF
func ToSlicePointer[T any](items ...T) []*T {
result := make([]*T, len(items))
for i := range items {
@@ -901,7 +957,8 @@ func ToSlicePointer[T any](items ...T) []*T {
return result
}
// ToSlice returns a slices of a variable parameter transformation
// ToSlice returns a slices of a variable parameter transformation.
// Play: https://go.dev/play/p/YzbzVq5kscN
func ToSlice[T any](items ...T) []T {
result := make([]T, len(items))
copy(result, items)
@@ -909,7 +966,8 @@ func ToSlice[T any](items ...T) []T {
return result
}
// AppendIfAbsent only absent append the item
// AppendIfAbsent only absent append the item.
// Play: https://go.dev/play/p/KcC1QXQ-RkL
func AppendIfAbsent[T comparable](slice []T, item T) []T {
if !Contain(slice, item) {
slice = append(slice, item)
@@ -917,7 +975,8 @@ func AppendIfAbsent[T comparable](slice []T, item T) []T {
return slice
}
// KeyBy converts a slice to a map based on a callback function
// KeyBy converts a slice to a map based on a callback function.
// Play: https://go.dev/play/p/uXod2LWD1Kg
func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T {
result := make(map[U]T, len(slice))
+758
View File
@@ -0,0 +1,758 @@
package slice
import (
"fmt"
"math"
"reflect"
)
func ExampleContain() {
result1 := Contain([]string{"a", "b", "c"}, "a")
result2 := Contain([]int{1, 2, 3}, 4)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
func ExampleContainSubSlice() {
result1 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "b"})
result2 := ContainSubSlice([]string{"a", "b", "c"}, []string{"a", "d"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
func ExampleChunk() {
arr := []string{"a", "b", "c", "d", "e"}
result1 := Chunk(arr, 1)
result2 := Chunk(arr, 2)
result3 := Chunk(arr, 3)
result4 := Chunk(arr, 4)
result5 := Chunk(arr, 5)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// [[a] [b] [c] [d] [e]]
// [[a b] [c d] [e]]
// [[a b c] [d e]]
// [[a b c d] [e]]
// [[a b c d e]]
}
func ExampleCompact() {
result1 := Compact([]int{0})
result2 := Compact([]int{0, 1, 2, 3})
result3 := Compact([]string{"", "a", "b", "0"})
result4 := Compact([]bool{false, true, true})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// []
// [1 2 3]
// [a b 0]
// [true true]
}
func ExampleConcat() {
result1 := Concat([]int{1, 2}, []int{3, 4})
result2 := Concat([]string{"a", "b"}, []string{"c"}, []string{"d"})
fmt.Println(result1)
fmt.Println(result2)
// Output:
// [1 2 3 4]
// [a b c d]
}
func ExampleDifference() {
slice1 := []int{1, 2, 3, 4, 5}
slice2 := []int{4, 5, 6}
result := Difference(slice1, slice2)
fmt.Println(result)
// Output:
// [1 2 3]
}
func ExampleDifferenceBy() {
slice1 := []int{1, 2, 3, 4, 5} //after add one: 2 3 4 5 6
slice2 := []int{3, 4, 5} //after add one: 4 5 6
addOne := func(i int, v int) int {
return v + 1
}
result := DifferenceBy(slice1, slice2, addOne)
fmt.Println(result)
// Output:
// [1 2]
}
func ExampleDifferenceWith() {
slice1 := []int{1, 2, 3, 4, 5}
slice2 := []int{4, 5, 6, 7, 8}
isDouble := func(v1, v2 int) bool {
return v2 == 2*v1
}
result := DifferenceWith(slice1, slice2, isDouble)
fmt.Println(result)
// Output:
// [1 5]
}
func ExampleEqual() {
slice1 := []int{1, 2, 3}
slice2 := []int{1, 2, 3}
slice3 := []int{1, 3, 2}
result1 := Equal(slice1, slice2)
result2 := Equal(slice1, slice3)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
func ExampleEqualWith() {
slice1 := []int{1, 2, 3}
slice2 := []int{2, 4, 6}
isDouble := func(a, b int) bool {
return b == a*2
}
result := EqualWith(slice1, slice2, isDouble)
fmt.Println(result)
// Output:
// true
}
func ExampleEvery() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result := Every(nums, isEven)
fmt.Println(result)
// Output:
// false
}
func ExampleNone() {
nums := []int{1, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result := None(nums, isEven)
fmt.Println(result)
// Output:
// true
}
func ExampleSome() {
nums := []int{1, 2, 3, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result := Some(nums, isEven)
fmt.Println(result)
// Output:
// true
}
func ExampleFilter() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result := Filter(nums, isEven)
fmt.Println(result)
// Output:
// [2 4]
}
func ExampleCount() {
nums := []int{1, 2, 3, 3, 4}
result1 := Count(nums, 1)
result2 := Count(nums, 3)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// 2
}
func ExampleCountBy() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result := CountBy(nums, isEven)
fmt.Println(result)
// Output:
// 2
}
func ExampleGroupBy() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
even, odd := GroupBy(nums, isEven)
fmt.Println(even)
fmt.Println(odd)
// Output:
// [2 4]
// [1 3 5]
}
func ExampleGroupWith() {
nums := []float64{6.1, 4.2, 6.3}
floor := func(num float64) float64 {
return math.Floor(num)
}
result := GroupWith(nums, floor) //map[float64][]float64
fmt.Println(result)
// Output:
// map[4:[4.2] 6:[6.1 6.3]]
}
func ExampleFind() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result, ok := Find(nums, isEven)
fmt.Println(*result)
fmt.Println(ok)
// Output:
// 2
// true
}
func ExampleFindLast() {
nums := []int{1, 2, 3, 4, 5}
isEven := func(i, num int) bool {
return num%2 == 0
}
result, ok := FindLast(nums, isEven)
fmt.Println(*result)
fmt.Println(ok)
// Output:
// 4
// true
}
func ExampleFlatten() {
arrs := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
result := Flatten(arrs)
fmt.Println(result)
// Output:
// [[a b] [c d]]
}
func ExampleFlattenDeep() {
arrs := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
result := FlattenDeep(arrs)
fmt.Println(result)
// Output:
// [a b c d]
}
func ExampleForEach() {
nums := []int{1, 2, 3}
var result []int
addOne := func(_ int, v int) {
result = append(result, v+1)
}
ForEach(nums, addOne)
fmt.Println(result)
// Output:
// [2 3 4]
}
func ExampleMap() {
nums := []int{1, 2, 3}
addOne := func(_ int, v int) int {
return v + 1
}
result := Map(nums, addOne)
fmt.Println(result)
// Output:
// [2 3 4]
}
func ExampleReduce() {
nums := []int{1, 2, 3}
sum := func(_ int, v1, v2 int) int {
return v1 + v2
}
result := Reduce(nums, sum, 0)
fmt.Println(result)
// Output:
// 6
}
func ExampleReplace() {
strs := []string{"a", "b", "c", "a"}
result1 := Replace(strs, "a", "x", 0)
result2 := Replace(strs, "a", "x", 1)
result3 := Replace(strs, "a", "x", 2)
result4 := Replace(strs, "a", "x", 3)
result5 := Replace(strs, "a", "x", -1)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// [a b c a]
// [x b c a]
// [x b c x]
// [x b c x]
// [x b c x]
}
func ExampleReplaceAll() {
result := ReplaceAll([]string{"a", "b", "c", "a"}, "a", "x")
fmt.Println(result)
// Output:
// [x b c x]
}
func ExampleRepeat() {
result := Repeat("a", 3)
fmt.Println(result)
// Output:
// [a a a]
}
func ExampleInterfaceSlice() {
strs := []string{"a", "b", "c"}
result := InterfaceSlice(strs) //[]interface{}{"a", "b", "c"}
fmt.Println(result)
// Output:
// [a b c]
}
func ExampleStringSlice() {
strs := []interface{}{"a", "b", "c"}
result := StringSlice(strs) //[]string{"a", "b", "c"}
fmt.Println(result)
// Output:
// [a b c]
}
func ExampleIntSlice() {
nums := []interface{}{1, 2, 3}
result := IntSlice(nums) //[]int{1, 2, 3}
fmt.Println(result)
// Output:
// [1 2 3]
}
func ExampleDeleteAt() {
result1 := DeleteAt([]string{"a", "b", "c"}, -1)
result2 := DeleteAt([]string{"a", "b", "c"}, 0)
result3 := DeleteAt([]string{"a", "b", "c"}, 0, 2)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
// Output:
// [a b c]
// [b c]
// [c]
}
func ExampleDrop() {
result1 := Drop([]string{"a", "b", "c"}, 0)
result2 := Drop([]string{"a", "b", "c"}, 1)
result3 := Drop([]string{"a", "b", "c"}, -1)
result4 := Drop([]string{"a", "b", "c"}, 4)
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
// Output:
// [a b c]
// [b c]
// [a b]
// []
}
func ExampleInsertAt() {
result1 := InsertAt([]string{"a", "b", "c"}, 0, "1")
result2 := InsertAt([]string{"a", "b", "c"}, 1, "1")
result3 := InsertAt([]string{"a", "b", "c"}, 2, "1")
result4 := InsertAt([]string{"a", "b", "c"}, 3, "1")
result5 := InsertAt([]string{"a", "b", "c"}, 0, []string{"1", "2", "3"})
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// [1 a b c]
// [a 1 b c]
// [a b 1 c]
// [a b c 1]
// [1 2 3 a b c]
}
func ExampleUpdateAt() {
result1 := UpdateAt([]string{"a", "b", "c"}, -1, "1")
result2 := UpdateAt([]string{"a", "b", "c"}, 0, "1")
result3 := UpdateAt([]string{"a", "b", "c"}, 1, "1")
result4 := UpdateAt([]string{"a", "b", "c"}, 2, "1")
result5 := UpdateAt([]string{"a", "b", "c"}, 3, "1")
fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
fmt.Println(result5)
// Output:
// [a b c]
// [1 b c]
// [a 1 c]
// [a b 1]
// [a b c]
}
func ExampleUnique() {
result := Unique([]string{"a", "a", "b"})
fmt.Println(result)
// Output:
// [a b]
}
func ExampleUniqueBy() {
nums := []int{1, 2, 3, 4, 5, 6}
result := UniqueBy(nums, func(val int) int {
return val % 3
})
fmt.Println(result)
// Output:
// [1 2 0]
}
func ExampleUnion() {
nums1 := []int{1, 3, 4, 6}
nums2 := []int{1, 2, 5, 6}
result := Union(nums1, nums2)
fmt.Println(result)
// Output:
// [1 3 4 6 2 5]
}
func ExampleUnionBy() {
nums := []int{1, 2, 3, 4}
divideTwo := func(n int) int {
return n / 2
}
result := UnionBy(divideTwo, nums)
fmt.Println(result)
// Output:
// [1 2 4]
}
func ExampleMerge() {
nums1 := []int{1, 2, 3}
nums2 := []int{3, 4}
result := Merge(nums1, nums2)
fmt.Println(result)
// Output:
// [1 2 3 3 4]
}
func ExampleIntersection() {
nums1 := []int{1, 2, 3}
nums2 := []int{2, 3, 4}
result := Intersection(nums1, nums2)
fmt.Println(result)
// Output:
// [2 3]
}
func ExampleSymmetricDifference() {
nums1 := []int{1, 2, 3}
nums2 := []int{1, 2, 4}
result := SymmetricDifference(nums1, nums2)
fmt.Println(result)
// Output:
// [3 4]
}
func ExampleReverse() {
strs := []string{"a", "b", "c", "d"}
Reverse(strs)
fmt.Println(strs)
// Output:
// [d c b a]
}
func ExampleSort() {
nums := []int{1, 4, 3, 2, 5}
Sort(nums)
fmt.Println(nums)
// Output:
// [1 2 3 4 5]
}
func ExampleSortBy() {
type User struct {
Name string
Age uint
}
users := []User{
{Name: "a", Age: 21},
{Name: "b", Age: 15},
{Name: "c", Age: 100}}
SortBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Println(users)
// Output:
// [{b 15} {a 21} {c 100}]
}
func ExampleSortByField() {
type User struct {
Name string
Age uint
}
users := []User{
{Name: "a", Age: 21},
{Name: "b", Age: 15},
{Name: "c", Age: 100}}
SortByField(users, "Age", "desc")
fmt.Println(users)
// Output:
// [{c 100} {a 21} {b 15}]
}
func ExampleWithout() {
result := Without([]int{1, 2, 3, 4}, 1, 2)
fmt.Println(result)
// Output:
// [3 4]
}
func ExampleIndexOf() {
strs := []string{"a", "a", "b", "c"}
result1 := IndexOf(strs, "a")
result2 := IndexOf(strs, "d")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0
// -1
}
func ExampleLastIndexOf() {
strs := []string{"a", "a", "b", "c"}
result1 := LastIndexOf(strs, "a")
result2 := LastIndexOf(strs, "d")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1
// -1
}
func ExampleToSlice() {
result := ToSlice("a", "b", "c")
fmt.Println(result)
// Output:
// [a b c]
}
func ExampleToSlicePointer() {
str1 := "a"
str2 := "b"
result := ToSlicePointer(str1, str2)
expect := []*string{&str1, &str2}
isEqual := reflect.DeepEqual(result, expect)
fmt.Println(isEqual)
// Output:
// true
}
func ExampleAppendIfAbsent() {
result1 := AppendIfAbsent([]string{"a", "b"}, "b")
result2 := AppendIfAbsent([]string{"a", "b"}, "c")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// [a b]
// [a b c]
}
func ExampleKeyBy() {
result := KeyBy([]string{"a", "ab", "abc"}, func(str string) int {
return len(str)
})
fmt.Println(result)
// Output:
// map[1:a 2:ab 3:abc]
}
+1 -1
View File
@@ -257,7 +257,7 @@ func TestFlatten(t *testing.T) {
input := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
expected := [][]string{{"a", "b"}, {"c", "d"}}
assert := internal.NewAssert(t, "TestFlattenDeep")
assert := internal.NewAssert(t, "TestFlatten")
assert.Equal(expected, Flatten(input))
}