Skip to content

Commit

Permalink
doc: improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Oct 10, 2022
1 parent eb5207b commit 330382f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Adding:
- lo.CountValues
- lo.CountValuesBy
- lo.MapEntries
- TupleX.Unpack()

## 1.31.0 (2022-10-06)

Expand Down
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1786,26 +1786,31 @@ duplicatedValues := lo.FindDuplicatesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i i

Search the minimum value of a collection.

Returns zero value when collection is empty.

```go
min := lo.Min[int]([]int{1, 2, 3})
min := lo.Min([]int{1, 2, 3})
// 1

min := lo.Min[int]([]int{})
min := lo.Min([]int{})
// 0
```

### MinBy

Search the minimum value of a collection using the given comparison function.

If several values of the collection are equal to the smallest value, returns the first such value.

Returns zero value when collection is empty.

```go
min := lo.MinBy[string]([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
min := lo.MinBy([]string{"s1", "string2", "s3"}, func(item string, min string) bool {
return len(item) < len(min)
})
// "s1"

min := lo.MinBy[string]([]string{}, func(item string, min string) bool {
min := lo.MinBy([]string{}, func(item string, min string) bool {
return len(item) < len(min)
})
// ""
Expand All @@ -1815,26 +1820,31 @@ min := lo.MinBy[string]([]string{}, func(item string, min string) bool {

Search the maximum value of a collection.

Returns zero value when collection is empty.

```go
max := lo.Max[int]([]int{1, 2, 3})
max := lo.Max([]int{1, 2, 3})
// 3

max := lo.Max[int]([]int{})
max := lo.Max([]int{})
// 0
```

### MaxBy

Search the maximum value of a collection using the given comparison function.

If several values of the collection are equal to the greatest value, returns the first such value.

Returns zero value when collection is empty.

```go
max := lo.MaxBy[string]([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
max := lo.MaxBy([]string{"string1", "s2", "string3"}, func(item string, max string) bool {
return len(item) > len(max)
})
// "string1"

max := lo.MaxBy[string]([]string{}, func(item string, max string) bool {
max := lo.MaxBy([]string{}, func(item string, max string) bool {
return len(item) > len(max)
})
// ""
Expand Down
4 changes: 4 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(T) U) [
}

// Min search the minimum value of a collection.
// Returns zero value when collection is empty.
func Min[T constraints.Ordered](collection []T) T {
var min T

Expand All @@ -243,6 +244,7 @@ func Min[T constraints.Ordered](collection []T) T {

// MinBy search the minimum value of a collection using the given comparison function.
// If several values of the collection are equal to the smallest value, returns the first such value.
// Returns zero value when collection is empty.
func MinBy[T any](collection []T, comparison func(T, T) bool) T {
var min T

Expand All @@ -264,6 +266,7 @@ func MinBy[T any](collection []T, comparison func(T, T) bool) T {
}

// Max searches the maximum value of a collection.
// Returns zero value when collection is empty.
func Max[T constraints.Ordered](collection []T) T {
var max T

Expand All @@ -286,6 +289,7 @@ func Max[T constraints.Ordered](collection []T) T {

// MaxBy search the maximum value of a collection using the given comparison function.
// If several values of the collection are equal to the greatest value, returns the first such value.
// Returns zero value when collection is empty.
func MaxBy[T any](collection []T, comparison func(T, T) bool) T {
var max T

Expand Down
68 changes: 68 additions & 0 deletions tuples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func TestUnpack(t *testing.T) {

is.Equal("a", r1)
is.Equal(1, r2)

r1, r2 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
}

{
Expand All @@ -50,6 +55,12 @@ func TestUnpack(t *testing.T) {
is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)

r1, r2, r3 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
}

{
Expand All @@ -61,6 +72,13 @@ func TestUnpack(t *testing.T) {
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)

r1, r2, r3, r4 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)
}

{
Expand All @@ -73,6 +91,14 @@ func TestUnpack(t *testing.T) {
is.Equal(1.0, r3)
is.Equal(true, r4)
is.Equal("b", r5)

r1, r2, r3, r4, r5 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)
is.Equal("b", r5)
}

{
Expand All @@ -86,6 +112,15 @@ func TestUnpack(t *testing.T) {
is.Equal(true, r4)
is.Equal("b", r5)
is.Equal(2, r6)

r1, r2, r3, r4, r5, r6 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)
is.Equal("b", r5)
is.Equal(2, r6)
}

{
Expand All @@ -100,6 +135,16 @@ func TestUnpack(t *testing.T) {
is.Equal("b", r5)
is.Equal(2, r6)
is.Equal(3.0, r7)

r1, r2, r3, r4, r5, r6, r7 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)
is.Equal("b", r5)
is.Equal(2, r6)
is.Equal(3.0, r7)
}

{
Expand All @@ -115,6 +160,17 @@ func TestUnpack(t *testing.T) {
is.Equal(2, r6)
is.Equal(3.0, r7)
is.Equal(true, r8)

r1, r2, r3, r4, r5, r6, r7, r8 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)
is.Equal("b", r5)
is.Equal(2, r6)
is.Equal(3.0, r7)
is.Equal(true, r8)
}

{
Expand All @@ -131,6 +187,18 @@ func TestUnpack(t *testing.T) {
is.Equal(3.0, r7)
is.Equal(true, r8)
is.Equal("c", r9)

r1, r2, r3, r4, r5, r6, r7, r8, r9 = tuple.Unpack()

is.Equal("a", r1)
is.Equal(1, r2)
is.Equal(1.0, r3)
is.Equal(true, r4)
is.Equal("b", r5)
is.Equal(2, r6)
is.Equal(3.0, r7)
is.Equal(true, r8)
is.Equal("c", r9)
}
}

Expand Down

0 comments on commit 330382f

Please sign in to comment.