Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Supported helpers for slices:
- [Times](#times)
- [Uniq](#uniq)
- [UniqBy](#uniqby)
- [IsUniq](#isuniq)
- [IsUniqBy](#isuniqby)
- [GroupBy](#groupby)
- [GroupByMap](#groupbymap)
- [Chunk](#chunk)
Expand Down Expand Up @@ -699,6 +701,37 @@ result, err := lo.UniqByErr([]int{0, 1, 2, 3, 4, 5}, func(i int) (int, error) {

[[play](https://go.dev/play/p/g42Z3QSb53u)]

### IsUniq

Returns true if all elements in the slice are unique, false otherwise. Returns true for nil and empty slices.

```go
lo.IsUniq([]int{1, 2, 3})
// true

lo.IsUniq([]int{1, 2, 1})
// false
```

### IsUniqBy

Returns true if all elements in the slice are unique based on a custom criterion. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed. Returns true for nil and empty slices.

```go
type User struct {
ID int
Name string
}

users := []User{{ID: 1, Name: "Alice"}, {ID: 2, Name: "Bob"}, {ID: 1, Name: "Charlie"}}

lo.IsUniqBy(users, func(u User) int { return u.ID })
// false

lo.IsUniqBy(users, func(u User) string { return u.Name })
// true
```

### GroupBy

Returns an object composed of keys generated from the results of running each element of collection through iteratee.
Expand Down
27 changes: 27 additions & 0 deletions docs/data/core-isuniq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: IsUniq
slug: isuniq
sourceRef: slice.go#L290
category: core
subCategory: slice
variantHelpers:
- core#slice#isuniq
similarHelpers:
- core#slice#isuniqby
- core#slice#uniq
position: 120
signatures:
- "func IsUniq[T comparable, Slice ~[]T](collection Slice) bool"
---

Returns true if all elements in the slice are unique, false otherwise. Returns true for nil and empty slices.

```go
lo.IsUniq([]int{1, 2, 3})
// true

lo.IsUniq([]int{1, 2, 1})
// false
```


36 changes: 36 additions & 0 deletions docs/data/core-isuniqby.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
name: IsUniqBy
slug: isuniqby
sourceRef: slice.go#L307
category: core
subCategory: slice
variantHelpers:
- core#slice#isuniqby
similarHelpers:
- core#slice#isuniq
- core#slice#uniqby
position: 130
signatures:
- "func IsUniqBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) bool"
---

Returns true if all elements in the slice are unique based on a custom criterion. The `iteratee` is invoked for each element to generate the key by which uniqueness is computed. Returns true for nil and empty slices.

```go
type User struct {
ID int
Name string
}

lo.IsUniqBy([]User{{ID: 1}, {ID: 2}, {ID: 3}}, func(u User) int {
return u.ID
})
// true

lo.IsUniqBy([]User{{ID: 1}, {ID: 2}, {ID: 1}}, func(u User) int {
return u.ID
})
// false
```


1 change: 1 addition & 0 deletions docs/data/core-uniq.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ variantHelpers:
- core#slice#uniq
similarHelpers:
- core#slice#uniqby
- core#slice#isuniq
- core#slice#uniqmap
- core#slice#uniqkeys
- core#slice#uniqvalues
Expand Down
1 change: 1 addition & 0 deletions docs/data/core-uniqby.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ variantHelpers:
similarHelpers:
- core#slice#uniqbyerr
- core#slice#uniq
- core#slice#isuniqby
- core#slice#uniqmap
- core#slice#partitionby
position: 110
Expand Down
1 change: 1 addition & 0 deletions docs/data/core-uniqbyerr.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ variantHelpers:
similarHelpers:
- core#slice#uniqby
- core#slice#uniq
- core#slice#isuniq
- core#slice#uniqmap
- core#slice#partitionby
position: 111
Expand Down
1 change: 1 addition & 0 deletions docs/data/core-uniqmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ variantHelpers:
similarHelpers:
- core#slice#uniq
- core#slice#uniqby
- core#slice#isuniq
- core#slice#map
- core#slice#filtermap
position: 20
Expand Down
2 changes: 2 additions & 0 deletions docs/static/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ Lo is built on a foundation of pragmatic engineering principles that balance pow
- Times: Execute function n times and collect results
- Uniq: Remove duplicate elements
- UniqBy: Remove duplicates by key function
- IsUniq: Check if all elements are unique
- IsUniqBy: Check if all elements are unique by key function
- GroupBy: Group elements by key function
- GroupByMap: Group elements by key function into map
- Chunk: Split slice into chunks of specified size
Expand Down
1 change: 1 addition & 0 deletions it/seq.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ func Reverse[T any, I ~func(func(T) bool)](collection I) I {
slice := slices.Collect(iter.Seq[T](collection))

return I(func(yield func(T) bool) {
//nolint:modernize // backward loop can't use slices.Backward (Go 1.24+), lo supports Go 1.18+
for i := len(slice) - 1; i >= 0; i-- {
if !yield(slice[i]) {
return
Expand Down
Loading