Skip to content

Commit

Permalink
doc: update documentation for First methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza-Kiani committed Jun 27, 2024
1 parent c7babfa commit 7159a8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ Supported search helpers:
- [Latest](#latest)
- [Last](#last)
- [First](#first)
- [FirstOrZeroValue](#firstorzerovalue)
- [FirstOrEmpty](#FirstOrEmpty)
- [FirstOr](#FirstOr)
- [Nth](#nth)
- [Sample](#sample)
- [Samples](#samples)
Expand Down Expand Up @@ -2246,23 +2247,37 @@ last, err := lo.Last([]int{1, 2, 3})
```
### First

Returns the first element of a collection or error if empty.
Returns the first element of a collection and check for availability of the first element.

```go
first, err := lo.First([]int{1, 2, 3})
// 1
first, ok := lo.First([]int{1, 2, 3})
// 1, true

first, ok := lo.First([]int{})
// 0, false
```
### FirstOrZeroValue
### FirstOrEmpty

Returns the first element of a collection or zero value if empty.

```go
first := lo.FirstOrZeroValue([]int{1, 2, 3})
first := lo.FirstOrEmpty([]int{1, 2, 3})
// 1

first := lo.FirstOrZeroValue([]int{})
first := lo.FirstOrEmpty([]int{})
// 0
```
### FirstOr

Returns the first element of a collection or the fallback value if empty.

```go
first := lo.FirstOr([]int{1, 2, 3}, 245)
// 1

first := lo.FirstOr([]int{}, 31)
// 31
```

### Nth

Expand Down
6 changes: 3 additions & 3 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func Last[T any](collection []T) (T, error) {
return collection[length-1], nil
}

// First returns the first element of a collection or zero if empty.
// Returns the first element of a collection and check for availability of the first element.
func First[T any](collection []T) (T, bool) {
length := len(collection)

Expand All @@ -379,13 +379,13 @@ func First[T any](collection []T) (T, bool) {
return collection[0], true
}

// FirstOrEmpty returns the first element of a collection or zero value if empty.
// Returns the first element of a collection or zero value if empty.
func FirstOrEmpty[T any](collection []T) T {
i, _ := First(collection)
return i
}

// FirstOr returns the first element of a collection or the fallback value that is provided as the second argument.
// Returns the first element of a collection or the fallback value if empty.
func FirstOr[T any](collection []T, fallback T) T {
i, ok := First(collection)
if !ok {
Expand Down

0 comments on commit 7159a8c

Please sign in to comment.