mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-09 15:12:26 +08:00
docs: add doc for UnionBy and KeyBy
This commit is contained in:
345
docs/slice.md
345
docs/slice.md
@@ -1,16 +1,17 @@
|
||||
# Slice
|
||||
|
||||
Package slice implements some functions to manipulate slice.
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Source:
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
@@ -20,59 +21,63 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
- [AppendIfAbsent](#AppendIfAbsent)
|
||||
- [Contain](#Contain)
|
||||
- [ContainSubSlice](#ContainSubSlice)
|
||||
- [Chunk](#Chunk)
|
||||
- [Compact](#Compact)
|
||||
- [Concat](#Concat)
|
||||
- [Count](#Count)
|
||||
- [Difference](#Difference)
|
||||
- [DifferenceBy](#DifferenceBy)
|
||||
- [DifferenceWith](#DifferenceWith)
|
||||
- [DeleteAt](#DeleteAt)
|
||||
- [Drop](#Drop)
|
||||
- [Equal](#Equal)
|
||||
- [EqualWith](#EqualWith)
|
||||
- [Every](#Every)
|
||||
- [Filter](#Filter)
|
||||
- [Find](#Find)
|
||||
- [FindLast](#FindLast)
|
||||
- [Flatten](#Flatten)
|
||||
- [FlattenDeep](#FlattenDeep)
|
||||
- [ForEach](#ForEach)
|
||||
|
||||
- [GroupBy](#GroupBy)
|
||||
- [GroupWith](#GroupWith)
|
||||
- [IntSlice](#IntSlice)
|
||||
- [InterfaceSlice](#InterfaceSlice)
|
||||
- [Intersection](#Intersection)
|
||||
- [InsertAt](#InsertAt)
|
||||
- [IndexOf](#IndexOf)
|
||||
- [LastIndexOf](#LastIndexOf)
|
||||
- [Map](#Map)
|
||||
- [Reverse](#Reverse)
|
||||
- [Reduce](#Reduce)
|
||||
- [Replace](#Replace)
|
||||
- [ReplaceAll](#ReplaceAll)
|
||||
- [Shuffle](#Shuffle)
|
||||
- [SortByField](#SortByField)
|
||||
- [Some](#Some)
|
||||
- [StringSlice](#StringSlice)
|
||||
- [SymmetricDifference](#SymmetricDifference)
|
||||
- [ToSlice](#ToSlice)
|
||||
- [ToSlicePointer](#ToSlicePointer)
|
||||
- [Unique](#Unique)
|
||||
- [UniqueBy](#UniqueBy)
|
||||
- [Union](#Union)
|
||||
- [UpdateAt](#UpdateAt)
|
||||
- [Without](#Without)
|
||||
- [AppendIfAbsent](#AppendIfAbsent)
|
||||
- [Contain](#Contain)
|
||||
- [ContainSubSlice](#ContainSubSlice)
|
||||
- [Chunk](#Chunk)
|
||||
- [Compact](#Compact)
|
||||
- [Concat](#Concat)
|
||||
- [Count](#Count)
|
||||
- [Difference](#Difference)
|
||||
- [DifferenceBy](#DifferenceBy)
|
||||
- [DifferenceWith](#DifferenceWith)
|
||||
- [DeleteAt](#DeleteAt)
|
||||
- [Drop](#Drop)
|
||||
- [Equal](#Equal)
|
||||
- [EqualWith](#EqualWith)
|
||||
- [Every](#Every)
|
||||
- [Filter](#Filter)
|
||||
- [Find](#Find)
|
||||
- [FindLast](#FindLast)
|
||||
- [Flatten](#Flatten)
|
||||
- [FlattenDeep](#FlattenDeep)
|
||||
- [ForEach](#ForEach)
|
||||
|
||||
- [GroupBy](#GroupBy)
|
||||
- [GroupWith](#GroupWith)
|
||||
- [IntSlice](#IntSlice)
|
||||
- [InterfaceSlice](#InterfaceSlice)
|
||||
- [Intersection](#Intersection)
|
||||
- [InsertAt](#InsertAt)
|
||||
- [IndexOf](#IndexOf)
|
||||
- [LastIndexOf](#LastIndexOf)
|
||||
- [Map](#Map)
|
||||
- [Reverse](#Reverse)
|
||||
- [Reduce](#Reduce)
|
||||
- [Replace](#Replace)
|
||||
- [ReplaceAll](#ReplaceAll)
|
||||
- [Shuffle](#Shuffle)
|
||||
- [SortByField](#SortByField)
|
||||
- [Some](#Some)
|
||||
- [StringSlice](#StringSlice)
|
||||
- [SymmetricDifference](#SymmetricDifference)
|
||||
- [ToSlice](#ToSlice)
|
||||
- [ToSlicePointer](#ToSlicePointer)
|
||||
- [Unique](#Unique)
|
||||
- [UniqueBy](#UniqueBy)
|
||||
- [Union](#Union)
|
||||
- [UnionBy](#UnionBy)
|
||||
- [UpdateAt](#UpdateAt)
|
||||
- [Without](#Without)
|
||||
- [KeyBy](#KeyBy)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Documentation
|
||||
|
||||
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||
|
||||
<p>If slice doesn't contain the value, append it to the slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -80,6 +85,7 @@ import (
|
||||
```go
|
||||
func AppendIfAbsent[T comparable](slice []T, value T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -98,8 +104,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Contain">Contain</span>
|
||||
|
||||
<p>Check if the value is in the slice or not.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -107,6 +113,7 @@ func main() {
|
||||
```go
|
||||
func Contain[T comparable](slice []T, value T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -121,8 +128,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||
|
||||
<p>Check if the slice contain subslice or not.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -130,6 +137,7 @@ func main() {
|
||||
```go
|
||||
func ContainSubSlice[T comparable](slice, subslice []T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -144,10 +152,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Chunk">Chunk</span>
|
||||
|
||||
<p>Creates an slice of elements split into groups the length of `size`.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -155,6 +161,7 @@ func main() {
|
||||
```go
|
||||
func Chunk[T any](slice []T, size int) [][]T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -170,9 +177,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Compact">Compact</span>
|
||||
|
||||
<p>Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -180,6 +186,7 @@ func main() {
|
||||
```go
|
||||
func Compact[T any](slice []T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -194,8 +201,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Concat">Concat</span>
|
||||
|
||||
<p>Creates a new slice concatenating slice with any additional slices and/or values.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -203,6 +210,7 @@ func main() {
|
||||
```go
|
||||
func Concat[T any](slice []T, values ...[]T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -220,9 +228,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Count">Count</span>
|
||||
|
||||
<p>Count iterates over elements of slice, returns a count of all matched elements.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -230,6 +237,7 @@ func main() {
|
||||
```go
|
||||
func Count[T any](slice []T, predicate func(index int, t T) bool) int
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -249,10 +257,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Difference">Difference</span>
|
||||
|
||||
<p>Creates an slice of whose element not included in the other given slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -260,6 +266,7 @@ func main() {
|
||||
```go
|
||||
func Difference[T comparable](slice, comparedSlice []T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -277,10 +284,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="DifferenceBy">DifferenceBy</span>
|
||||
|
||||
<p>DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -288,6 +293,7 @@ func main() {
|
||||
```go
|
||||
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -308,8 +314,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="DifferenceWith">DifferenceWith</span>
|
||||
|
||||
<p>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.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -317,6 +323,7 @@ func main() {
|
||||
```go
|
||||
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -338,6 +345,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="DeleteAt">DeleteAt</span>
|
||||
|
||||
<p>Delete the element of slice from start index to end index - 1.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -345,6 +353,7 @@ func main() {
|
||||
```go
|
||||
func DeleteAt[T any](slice []T, start int, end ...int)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -363,10 +372,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Drop">Drop</span>
|
||||
|
||||
<p>Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -374,6 +381,7 @@ func main() {
|
||||
```go
|
||||
func Drop[T any](slice []T, n int) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -394,9 +402,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Equal">Equal</span>
|
||||
|
||||
<p>Check if two slices are equal: the same length and all elements' order and value are equal.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -404,6 +411,7 @@ func main() {
|
||||
```go
|
||||
func Equal[T comparable](slice1, slice2 []T) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -425,9 +433,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="EqualWith">EqualWith</span>
|
||||
|
||||
<p>Check if two slices are equal with comparator func.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -435,6 +442,7 @@ func main() {
|
||||
```go
|
||||
func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -457,9 +465,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Every">Every</span>
|
||||
|
||||
<p>Return true if all of the values in the slice pass the predicate function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -467,6 +474,7 @@ func main() {
|
||||
```go
|
||||
func Every[T any](slice []T, predicate func(index int, item T) bool) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -486,10 +494,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Filter">Filter</span>
|
||||
|
||||
<p>Return all elements which match the function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -497,6 +503,7 @@ func main() {
|
||||
```go
|
||||
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -516,9 +523,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Find">Find</span>
|
||||
|
||||
<p>Iterates over elements of slice, returning the first one that passes a truth test on function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -526,6 +532,7 @@ func main() {
|
||||
```go
|
||||
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -546,10 +553,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="FindLast">FindLast</span>
|
||||
|
||||
<p>iterates over elements of slice from end to begin, returning the last one that passes a truth test on function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -557,6 +562,7 @@ func main() {
|
||||
```go
|
||||
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -577,9 +583,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Flatten">Flatten</span>
|
||||
|
||||
<p>Flatten slice with one level.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -587,6 +592,7 @@ func main() {
|
||||
```go
|
||||
func Flatten(slice any) any
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -602,9 +608,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="FlattenDeep">FlattenDeep</span>
|
||||
|
||||
<p>flattens slice recursive.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -612,6 +617,7 @@ func main() {
|
||||
```go
|
||||
func FlattenDeep(slice any) any
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -627,11 +633,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="ForEach">ForEach</span>
|
||||
|
||||
<p>Iterates over elements of slice and invokes function for each element.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -639,6 +642,7 @@ func main() {
|
||||
```go
|
||||
func ForEach[T any](slice []T, iteratee func(index int, item T))
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -657,10 +661,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="GroupBy">GroupBy</span>
|
||||
|
||||
<p>Iterates over elements of the slice, each element will be group by criteria, returns two slices.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -668,6 +670,7 @@ func main() {
|
||||
```go
|
||||
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -688,17 +691,16 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="GroupWith">GroupWith</span>
|
||||
|
||||
<p>Return a map composed of keys generated from the results of running each element of slice thru iteratee.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -717,9 +719,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="IntSlice">IntSlice</span>
|
||||
|
||||
<p>Convert interface slice to int slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -727,6 +728,7 @@ func main() {
|
||||
```go
|
||||
func IntSlice(slice any) []int
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -742,10 +744,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="InterfaceSlice">InterfaceSlice</span>
|
||||
|
||||
<p>Convert value to interface slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -753,6 +753,7 @@ func main() {
|
||||
```go
|
||||
func InterfaceSlice(slice any) []any
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -768,10 +769,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Intersection">Intersection</span>
|
||||
|
||||
<p>Creates a slice of unique values that included by all slices.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -779,6 +778,7 @@ func main() {
|
||||
```go
|
||||
func Intersection[T comparable](slices ...[]T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -796,10 +796,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="InsertAt">InsertAt</span>
|
||||
|
||||
<p>insert the element into slice at index.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -807,6 +805,7 @@ func main() {
|
||||
```go
|
||||
func InsertAt[T any](slice []T, index int, value any) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -817,7 +816,7 @@ import (
|
||||
|
||||
func main() {
|
||||
s := []string{"a", "b", "c"}
|
||||
|
||||
|
||||
res1, _ := slice.InsertAt(s, 0, "1")
|
||||
fmt.Println(res1) //[]string{"1", "a", "b", "c"}
|
||||
|
||||
@@ -826,10 +825,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="IndexOf">IndexOf</span>
|
||||
|
||||
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -837,6 +834,7 @@ func main() {
|
||||
```go
|
||||
func IndexOf[T comparable](slice []T, value T) int
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -855,9 +853,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="LastIndexOf">LastIndexOf</span>
|
||||
|
||||
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -865,6 +862,7 @@ func main() {
|
||||
```go
|
||||
func LastIndexOf[T comparable](slice []T, value T) int
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -883,10 +881,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Map">Map</span>
|
||||
|
||||
<p>Creates an slice of values by running each element in slice thru function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -894,6 +890,7 @@ func main() {
|
||||
```go
|
||||
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -912,10 +909,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Reverse">Reverse</span>
|
||||
|
||||
<p>Reverse the elements order in slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -923,6 +918,7 @@ func main() {
|
||||
```go
|
||||
func Reverse[T any](slice []T)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -938,9 +934,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Reduce">Reduce</span>
|
||||
|
||||
<p>Reduce slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -948,6 +943,7 @@ func main() {
|
||||
```go
|
||||
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -966,9 +962,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Replace">Replace</span>
|
||||
|
||||
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -976,6 +971,7 @@ func main() {
|
||||
```go
|
||||
func Replace[T comparable](slice []T, old T, new T, n int) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -995,9 +991,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="ReplaceAll">ReplaceAll</span>
|
||||
|
||||
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1005,6 +1000,7 @@ func main() {
|
||||
```go
|
||||
func ReplaceAll[T comparable](slice []T, old T, new T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1022,8 +1018,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Shuffle">Shuffle</span>
|
||||
|
||||
<p>Creates an slice of shuffled values.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1031,6 +1027,7 @@ func main() {
|
||||
```go
|
||||
func Shuffle[T any](slice []T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1046,9 +1043,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="SortByField">SortByField</span>
|
||||
|
||||
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1056,6 +1052,7 @@ func main() {
|
||||
```go
|
||||
func SortByField(slice any, field string, sortType ...string) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1079,7 +1076,7 @@ func main() {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(students)
|
||||
fmt.Println(students)
|
||||
// []students{
|
||||
// {"b", 15},
|
||||
// {"a", 10},
|
||||
@@ -1089,9 +1086,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Some">Some</span>
|
||||
|
||||
<p>Return true if any of the values in the list pass the predicate function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1099,6 +1095,7 @@ func main() {
|
||||
```go
|
||||
func Some[T any](slice []T, predicate func(index int, item T) bool) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1118,9 +1115,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="StringSlice">StringSlice</span>
|
||||
|
||||
<p>Convert interface slice to string slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1128,6 +1124,7 @@ func main() {
|
||||
```go
|
||||
func StringSlice(slice any) []string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1143,10 +1140,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="SymmetricDifference">SymmetricDifference</span>
|
||||
|
||||
<p>Create a slice whose element is in given slices, but not in both slices.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1154,6 +1149,7 @@ func main() {
|
||||
```go
|
||||
func SymmetricDifference[T comparable](slices ...[]T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1173,9 +1169,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="ToSlice">ToSlice</span>
|
||||
|
||||
<p>Returns a slices of a variable parameter transformation</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1183,6 +1178,7 @@ func main() {
|
||||
```go
|
||||
func ToSlice[T any](value ...T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1197,9 +1193,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||
|
||||
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1207,6 +1202,7 @@ func main() {
|
||||
```go
|
||||
func ToSlicePointer[T any](value ...T) []*T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1223,8 +1219,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Unique">Unique</span>
|
||||
|
||||
<p>Remove duplicate elements in slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1232,6 +1228,7 @@ func main() {
|
||||
```go
|
||||
func Unique[T comparable](slice []T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1246,9 +1243,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="UniqueBy">UniqueBy</span>
|
||||
|
||||
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1256,6 +1252,7 @@ func main() {
|
||||
```go
|
||||
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1272,9 +1269,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="Union">Union</span>
|
||||
|
||||
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1282,6 +1278,7 @@ func main() {
|
||||
```go
|
||||
func Union[T comparable](slices ...[]T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1299,9 +1296,35 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="UnionBy">UnionBy</span>
|
||||
|
||||
<p>UnionBy is like Union, what's more it accepts iteratee which is invoked for each element of each slice.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
testFunc := func(i int) int {
|
||||
return i / 2
|
||||
}
|
||||
result := slice.UnionBy(testFunc, []int{0, 1, 2, 3, 4, 5}, []int{0, 2, 10})
|
||||
fmt.Println(result) //[]int{0, 2, 4, 10}
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="UpdateAt">UpdateAt</span>
|
||||
|
||||
<p>Update the slice element at index. if param index < 0 or index >= len(slice), will return error. </p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1309,6 +1332,7 @@ func main() {
|
||||
```go
|
||||
func UpdateAt[T any](slice []T, index int, value T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1319,16 +1343,14 @@ import (
|
||||
|
||||
func main() {
|
||||
s := []string{"a", "b", "c"}
|
||||
|
||||
|
||||
res1, _ := slice.UpdateAt(s, 0, "1")
|
||||
fmt.Println(res1) //[]string{"1", "b", "c"}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="Without">Without</span>
|
||||
|
||||
<p>Creates a slice excluding all given values. </p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -1336,6 +1358,7 @@ func main() {
|
||||
```go
|
||||
func Without[T comparable](slice []T, values ...T) []T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -1350,13 +1373,29 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="KeyBy">KeyBy</span>
|
||||
|
||||
<p>Converts a slice to a map based on a callback function.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
func main() {
|
||||
res := slice.KeyBy([]string{"a", "ab", "abc"}, func(str string) int {
|
||||
return len(str)
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
fmt.Println(res) //map[int]string{1: "a", 2: "ab", 3: "abc"}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user