Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
samber authored Apr 11, 2022
2 parents 0cae74a + c496bf0 commit 356f3bd
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 7 deletions.
123 changes: 123 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Supported helpers for slices:

- Filter
- Map
- FilterMap
- FlatMap
- Reduce
- ForEach
Expand All @@ -70,6 +71,8 @@ Supported helpers for slices:
- DropWhile
- DropRightWhile
- Reject
- Count
- CountBy
- Range / RangeFrom / RangeWithSteps

Supported helpers for maps:
Expand Down Expand Up @@ -101,8 +104,12 @@ Supported search helpers:
- IndexOf
- LastIndexOf
- Find
- FindIndexOf
- FindLastIndexOf
- Min
- MinBy
- Max
- MaxBy
- Last
- Nth
- Sample
Expand All @@ -115,6 +122,7 @@ Other functional programming helpers:
- Switch / Case / Default
- ToPtr
- ToSlicePtr
- Empty

Time based helpers:

Expand Down Expand Up @@ -170,6 +178,22 @@ lo.FlatMap[int, string]([]int{0, 1, 2}, func(x int, _ int) []string {
// []string{"0", "0", "1", "1", "2", "2"}
```

### FilterMap

Returns a slice which obtained after both filtering and mapping using the given callback function.

The callback function should return two values: the result of the mapping operation and whether the result element should be included or not.

```go
matching := lo.FilterMap[string, string]([]string{"cpu", "gpu", "mouse", "keyboard"}, func(x string, _ int) (string, bool) {
if strings.HasSuffix(x, "pu") {
return "xpu", true
}
return "", false
})
// []string{"xpu", "xpu"}
```

### Filter

Iterates over a collection and returns an array of all the elements the predicate function returns `true` for.
Expand Down Expand Up @@ -492,6 +516,26 @@ odd := lo.Reject[int]([]int{1, 2, 3, 4}, func(x int, _ int) bool {
// []int{1, 3}
```

### Count

Counts the number of elements in the collection that compare equal to value.

```go
count := Count[int]([]int{1, 5, 1}, 1)
// 2
```

### CountBy

Counts the number of elements in the collection for which predicate is true.

```go
count := CountBy[int]([]int{1, 5, 1}, func(i int) bool {
return i < 4
})
// 2
```

### Range / RangeFrom / RangeWithSteps

Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.
Expand Down Expand Up @@ -725,6 +769,38 @@ str, ok := lo.Find[string]([]string{"foobar"}, func(i string) bool {
// "", false
```

### FindIndexOf

FindIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.

```go
str, index, ok := lo.FindIndexOf[string]([]string{"a", "b", "a", "b"}, func(i string) bool {
return i == "b"
})
// "b", 1, true

str, index, ok := lo.FindIndexOf[string]([]string{"foobar"}, func(i string) bool {
return i == "b"
})
// "", -1, false
```

### FindLastIndexOf

FindLastIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.

```go
str, index, ok := lo.FindLastIndexOf[string]([]string{"a", "b", "a", "b"}, func(i string) bool {
return i == "b"
})
// "b", 4, true

str, index, ok := lo.FindLastIndexOf[string]([]string{"foobar"}, func(i string) bool {
return i == "b"
})
// "", -1, false
```

### Min

Search the minimum value of a collection.
Expand All @@ -737,6 +813,23 @@ min := lo.Min[int]([]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.

```go
min := lo.MinBy[string]([]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 {
return len(item) < len(min)
})
// ""
```

### Max

Search the maximum value of a collection.
Expand All @@ -749,6 +842,23 @@ max := lo.Max[int]([]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.

```go
max := lo.MaxBy[string]([]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 {
return len(item) > len(max)
})
// ""
```

### Last

Returns the last element of a collection or error if empty.
Expand Down Expand Up @@ -878,6 +988,19 @@ ptr := lo.ToSlicePtr[string]([]string{"hello", "world"})
// []*string{"hello", "world"}
```

### Empty

Returns an empty value.

```go
lo.Empty[int]()
// 0
lo.Empty[string]()
// ""
lo.Empty[bool]()
// false
```

### Attempt

Invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned.
Expand Down
80 changes: 76 additions & 4 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package lo

import (
"fmt"
"math/rand"
"math"
"math/rand"

"golang.org/x/exp/constraints"
)

Expand All @@ -21,7 +22,7 @@ func IndexOf[T comparable](collection []T, element T) int {
return -1
}

// IndexOf returns the index at which the last occurrence of a value is found in an array or return -1
// LastIndexOf returns the index at which the last occurrence of a value is found in an array or return -1
// if the value cannot be found.
func LastIndexOf[T comparable](collection []T, element T) int {
length := len(collection)
Expand All @@ -47,6 +48,34 @@ func Find[T any](collection []T, predicate func(T) bool) (T, bool) {
return result, false
}

// FindIndexOf searches an element in a slice based on a predicate and returns the index and true.
// It returns -1 and false if the element is not found.
func FindIndexOf[T any](collection []T, predicate func(T) bool) (T, int, bool) {
for i, item := range collection {
if predicate(item) {
return item, i, true
}
}

var result T
return result, -1, false
}

// FindLastIndexOf searches last element in a slice based on a predicate and returns the index and true.
// It returns -1 and false if the element is not found.
func FindLastIndexOf[T any](collection []T, predicate func(T) bool) (T, int, bool) {
length := len(collection)

for i := length - 1; i >= 0; i-- {
if predicate(collection[i]) {
return collection[i], i, true
}
}

var result T
return result, -1, false
}

// Min search the minimum value of a collection.
func Min[T constraints.Ordered](collection []T) T {
var min T
Expand All @@ -60,7 +89,6 @@ func Min[T constraints.Ordered](collection []T) T {
for i := 1; i < len(collection); i++ {
item := collection[i]

// if item.Less(min) {
if item < min {
min = item
}
Expand All @@ -69,7 +97,29 @@ func Min[T constraints.Ordered](collection []T) T {
return min
}

// Max search the maximum value of a collection.
// 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.
func MinBy[T any](collection []T, comparison func(T, T) bool) T {
var min T

if len(collection) == 0 {
return min
}

min = collection[0]

for i := 1; i < len(collection); i++ {
item := collection[i]

if comparison(item, min) {
min = item
}
}

return min
}

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

Expand All @@ -90,6 +140,28 @@ func Max[T constraints.Ordered](collection []T) T {
return max
}

// 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.
func MaxBy[T any](collection []T, comparison func(T, T) bool) T {
var max T

if len(collection) == 0 {
return max
}

max = collection[0]

for i := 1; i < len(collection); i++ {
item := collection[i]

if comparison(item, max) {
max = item
}
}

return max
}

// Last returns the last element of a collection or error if empty.
func Last[T any](collection []T) (T, error) {
length := len(collection)
Expand Down
Loading

0 comments on commit 356f3bd

Please sign in to comment.