Skip to content

Commit

Permalink
feat: follow convention for return items and reuse base First functio…
Browse files Browse the repository at this point in the history
…n and add FirstOr
  • Loading branch information
Alireza-Kiani committed Jun 27, 2024
1 parent 94555a8 commit c7babfa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
26 changes: 15 additions & 11 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,28 +367,32 @@ func Last[T any](collection []T) (T, error) {
return collection[length-1], nil
}

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

if length == 0 {
var t T
return t, fmt.Errorf("first: cannot extract the first element of an empty slice")
return t, false
}

return collection[0], nil
return collection[0], true
}

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

if length == 0 {
var t T
return t
// FirstOr returns the first element of a collection or the fallback value that is provided as the second argument.
func FirstOr[T any](collection []T, fallback T) T {
i, ok := First(collection)
if !ok {
return fallback
}

return collection[0]
return i
}

// Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element
Expand Down
29 changes: 22 additions & 7 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,24 +376,39 @@ func TestFirst(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1, err1 := First([]int{1, 2, 3})
result2, err2 := First([]int{})
result1, ok1 := First([]int{1, 2, 3})
result2, ok2 := First([]int{})

is.Equal(result1, 1)
is.Equal(err1, nil)
is.Equal(ok1, true)
is.Equal(result2, 0)
is.Equal(err2, fmt.Errorf("first: cannot extract the first element of an empty slice"))
is.Equal(ok2, false)
}

func TestFirstOrZeroValue(t *testing.T) {
func TestFirstOrEmpty(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := FirstOrZeroValue([]int{1, 2, 3})
result2 := FirstOrZeroValue([]int{})
result1 := FirstOrEmpty([]int{1, 2, 3})
result2 := FirstOrEmpty([]int{})
result3 := FirstOrEmpty([]string{})

is.Equal(result1, 1)
is.Equal(result2, 0)
is.Equal(result3, "")
}

func TestFirstOr(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := FirstOr([]int{1, 2, 3}, 63)
result2 := FirstOr([]int{}, 23)
result3 := FirstOr([]string{}, "test")

is.Equal(result1, 1)
is.Equal(result2, 23)
is.Equal(result3, "test")
}

func TestNth(t *testing.T) {
Expand Down

0 comments on commit c7babfa

Please sign in to comment.