Skip to content
Open
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
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ Type manipulation helpers:
- [ToSlicePtr](#tosliceptr)
- [FromSlicePtr](#fromsliceptr)
- [FromSlicePtrOr](#fromsliceptror)
- [EqualPtr](#equalptr)
- [ComparePtr](#compareptr)
- [ToAnySlice](#toanyslice)
- [FromAnySlice](#fromanyslice)
- [Empty](#empty)
Expand Down Expand Up @@ -3689,6 +3691,67 @@ ptr := lo.FromSlicePtrOr([]*string{&str1, nil, &str2}, "fallback value")

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

### EqualPtr

Checks equality of two pointers by their dereferenced values, handling nil cases.

```go
ptr42 := lo.ToPtr(42)
ptr42_2 := lo.ToPtr(42)
ptr100 := lo.ToPtr(100)
ptrNil := (*int)(nil)

equal1 := lo.EqualPtr(ptr42, ptr42_2)
// equal1: true (both point to 42)

equal2 := lo.EqualPtr(ptr42, ptr100)
// equal2: false (42 != 100)

equal3 := lo.EqualPtr(ptr42, ptrNil)
// equal3: false (one is nil)

equal4 := lo.EqualPtr(ptrNil, ptrNil)
// equal4: true (both are nil)
```

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

### ComparePtr

ComparePtr compares two pointers and returns:
- 0 if both are nil or *a == *b
- -1 if a is nil (and b is not) or *a < *b
- 1 if b is nil (and a is not) or *a > *b

Nil is treated as less than any non-nil value.

```go
ptr42 := lo.ToPtr(42)
ptr42_2 := lo.ToPtr(42)
ptr100 := lo.ToPtr(100)
ptrNil := (*int)(nil)

equal1 := lo.ComparePtr(ptr42, ptr42_2)
// equal1: 0 (42 == 42)

equal2 := lo.ComparePtr(ptr42, ptr100)
// equal2: -1 (42 < 100)

equal3 := lo.ComparePtr(ptr100, ptr42)
// equal3: 1 (100 > 42)

equal4 := lo.ComparePtr(ptr42, ptrNil)
// equal4: 1 (42 > nil)

equal5 := lo.ComparePtr(ptrNil, ptr42)
// equal5: -1 (nil < 42)

equal6 := lo.ComparePtr(ptrNil, ptrNil)
// equal6: 0 (nil == nil)
```

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

### ToAnySlice

Returns a slice with all elements mapped to `any` type.
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-coalesce.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Coalesce
slug: coalesce
sourceRef: type_manipulation.go#L153
sourceRef: type_manipulation.go#L157
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-coalescemap.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: CoalesceMap
slug: coalescemap
sourceRef: type_manipulation.go#L196
sourceRef: type_manipulation.go#L200
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-coalescemaporempty.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: CoalesceMapOrEmpty
slug: coalescemaporempty
sourceRef: type_manipulation.go#L207
sourceRef: type_manipulation.go#L211
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-coalesceorempty.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: CoalesceOrEmpty
slug: coalesceorempty
sourceRef: type_manipulation.go#L167
sourceRef: type_manipulation.go#L171
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-coalesceslice.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: CoalesceSlice
slug: coalesceslice
sourceRef: type_manipulation.go#L174
sourceRef: type_manipulation.go#L178
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-coalescesliceorempty.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: CoalesceSliceOrEmpty
slug: coalescesliceorempty
sourceRef: type_manipulation.go#L185
sourceRef: type_manipulation.go#L189
category: core
subCategory: type
signatures:
Expand Down
48 changes: 48 additions & 0 deletions docs/data/core-compareptr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
name: ComparePtr
slug: compareptr
sourceRef: type_manipulation.go#L229
category: core
subCategory: type
signatures:
- "func ComparePtr[T constraints.Ordered](a, b *T) bool"
variantHelpers:
- core#type#compareptr
similarHelpers:
- core#type#equalptr
- core#type#fromptr
- core#type#toptr
position: 110
---

ComparePtr compares two pointers and returns:
- 0 if both are nil or *a == *b
- -1 if a is nil (and b is not) or *a < *b
- 1 if b is nil (and a is not) or *a > *b

Nil is treated as less than any non-nil value.

```go
ptr42 := lo.ToPtr(42)
ptr42_2 := lo.ToPtr(42)
ptr100 := lo.ToPtr(100)
ptrNil := (*int)(nil)

equal1 := lo.ComparePtr(ptr42, ptr42_2)
// equal1: 0 (42 == 42)

equal2 := lo.ComparePtr(ptr42, ptr100)
// equal2: -1 (42 < 100)

equal3 := lo.ComparePtr(ptr100, ptr42)
// equal3: 1 (100 > 42)

equal4 := lo.ComparePtr(ptr42, ptrNil)
// equal4: 1 (42 > nil)

equal5 := lo.ComparePtr(ptrNil, ptr42)
// equal5: -1 (nil < 42)

equal6 := lo.ComparePtr(ptrNil, ptrNil)
// equal6: 0 (nil == nil)
```
2 changes: 1 addition & 1 deletion docs/data/core-empty.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Empty
slug: empty
sourceRef: type_manipulation.go#L132
sourceRef: type_manipulation.go#L136
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-emptyabletoptr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: EmptyableToPtr
slug: emptyabletoptr
sourceRef: type_manipulation.go#L41
sourceRef: type_manipulation.go#L45
category: core
subCategory: type
signatures:
Expand Down
37 changes: 37 additions & 0 deletions docs/data/core-equalptr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
name: EqualPtr
slug: equalptr
sourceRef: type_manipulation.go#L220
category: core
subCategory: type
signatures:
- "func EqualPtr[T comparable](a, b *T) bool"
variantHelpers:
- core#type#equalptr
similarHelpers:
- core#type#compareptr
- core#type#fromptr
- core#type#toptr
position: 100
---

Compares two pointers: returns true if both are nil or both point to equal values, false if only one is nil or values differ.

```go
ptr42 := lo.ToPtr(42)
ptr42_2 := lo.ToPtr(42)
ptr100 := lo.ToPtr(100)
ptrNil := (*int)(nil)

equal1 := lo.EqualPtr(ptr42, ptr42_2)
// equal1: true (both point to 42)

equal2 := lo.EqualPtr(ptr42, ptr100)
// equal2: false (42 != 100)

equal3 := lo.EqualPtr(ptr42, ptrNil)
// equal3: false (one is nil)

equal4 := lo.EqualPtr(ptrNil, ptrNil)
// equal4: true (both are nil)
```
2 changes: 1 addition & 1 deletion docs/data/core-fromanyslice.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: FromAnySlice
slug: fromanyslice
sourceRef: type_manipulation.go#L118
sourceRef: type_manipulation.go#L122
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-fromptr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: FromPtr
slug: fromptr
sourceRef: type_manipulation.go#L53
sourceRef: type_manipulation.go#L57
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-fromptror.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: FromPtrOr
slug: fromptror
sourceRef: type_manipulation.go#L63
sourceRef: type_manipulation.go#L67
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-fromsliceptr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: FromSlicePtr
slug: fromsliceptr
sourceRef: type_manipulation.go#L85
sourceRef: type_manipulation.go#L89
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-isempty.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: IsEmpty
slug: isempty
sourceRef: type_manipulation.go#L139
sourceRef: type_manipulation.go#L143
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-isnil.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: IsNil
slug: isnil
sourceRef: type_manipulation.go#L7
sourceRef: type_manipulation.go#L11
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-isnotempty.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: IsNotEmpty
slug: isnotempty
sourceRef: type_manipulation.go#L146
sourceRef: type_manipulation.go#L150
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-isnotnil.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: IsNotNil
slug: isnil
sourceRef: type_manipulation.go#L22
sourceRef: type_manipulation.go#L26
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-nil.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: Nil
slug: nil
sourceRef: type_manipulation.go#L34
sourceRef: type_manipulation.go#L38
category: core
subCategory: type
playUrl: https://go.dev/play/p/P2sD0PMXw4F
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-toanyslice.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: ToAnySlice
slug: toanyslice
sourceRef: type_manipulation.go#L107
sourceRef: type_manipulation.go#L111
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-toptr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: ToPtr
slug: toptr
sourceRef: type_manipulation.go#L28
sourceRef: type_manipulation.go#L32
category: core
subCategory: type
signatures:
Expand Down
2 changes: 1 addition & 1 deletion docs/data/core-tosliceptr.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: ToSlicePtr
slug: tosliceptr
sourceRef: type_manipulation.go#L73
sourceRef: type_manipulation.go#L77
category: core
subCategory: type
signatures:
Expand Down
2 changes: 2 additions & 0 deletions docs/static/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ Lo is built on a foundation of pragmatic engineering principles that balance pow
- ToSlicePtr: Convert slice to slice of pointers
- FromSlicePtr: Convert slice of pointers to slice
- FromSlicePtrOr: Convert slice of pointers to slice or fallback
- EqualPtr: Check two pointers to equal values
- ComparePtr: Compare two `constraints.Ordered` pointer values
- ToAnySlice: Convert slice to []any
- FromAnySlice: Convert []any to typed slice
- Empty: Get zero value of type
Expand Down
Loading