Skip to content

Commit

Permalink
doc: adding IsSorted and IsSortedByKey
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jul 24, 2022
1 parent add5ee4 commit 7a82347
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 75 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Adding:
- lo.ReduceRight
- lo.FromPtrOr
- lo.MapToSlice
- lo.IsSorted
- lo.IsSortedByKey

## 1.25.0 (2022-07-04)

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,26 @@ slice := lo.Compact[string](in)
// []string{"foo", "bar"}
```

### IsSorted

Checks if a slice is sorted.

```go
slice := lo.IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
// true
```

### IsSortedByKey

Checks if a slice is sorted by iteratee.

```go
slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
return len(s)
})
// true
```

### Keys

Creates an array of the map keys.
Expand Down
33 changes: 16 additions & 17 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,20 @@ func ReplaceAll[T comparable](collection []T, old T, new T) []T {
return Replace(collection, old, new, -1)
}

// Compact returns a slice of all non-zero elements.
func Compact[T comparable](collection []T) []T {
var zero T

result := []T{}

for _, item := range collection {
if item != zero {
result = append(result, item)
}
}

return result
}

// IsSorted checks if a slice is sorted.
func IsSorted[T constraints.Ordered](collection []T) bool {
Expand All @@ -460,8 +474,8 @@ func IsSorted[T constraints.Ordered](collection []T) bool {
return true
}

// IsSortedBy checks if a slice is sorted by iteratee.
func IsSortedByKey[K constraints.Ordered, V any](collection []V, iteratee func(V) K) bool {
// IsSortedByKey checks if a slice is sorted by iteratee.
func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(V) K) bool {
size := len(collection)

for i := 0; i < size-1; i++ {
Expand All @@ -472,18 +486,3 @@ func IsSortedByKey[K constraints.Ordered, V any](collection []V, iteratee func(V

return true
}

// Compact returns a slice of all non-zero elements.
func Compact[T comparable](collection []T) []T {
var zero T

result := []T{}

for _, item := range collection {
if item != zero {
result = append(result, item)
}
}

return result
}
116 changes: 58 additions & 58 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,39 @@ func TestKeyBy(t *testing.T) {
is.Equal(result1, map[int]string{1: "a", 2: "aa", 3: "aaa"})
}

func TestAssociate(t *testing.T) {
type foo struct {
baz string
bar int
}
transform := func(f *foo) (string, int) {
return f.baz, f.bar
}
testCases := []struct {
in []*foo
expect map[string]int
}{
{
in: []*foo{{baz: "apple", bar: 1}},
expect: map[string]int{"apple": 1},
},
{
in: []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}},
expect: map[string]int{"apple": 1, "banana": 2},
},
{
in: []*foo{{baz: "apple", bar: 1}, {baz: "apple", bar: 2}},
expect: map[string]int{"apple": 2},
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
is := assert.New(t)
is.Equal(Associate(testCase.in, transform), testCase.expect)
})
}
}

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

Expand Down Expand Up @@ -503,33 +536,6 @@ func TestReplaceAll(t *testing.T) {
is.Equal([]int{0, 1, 0, 1, 2, 3, 0}, out2)
}

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

is.True(IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
is.True(IsSorted([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}))

is.False(IsSorted([]int{0, 1, 4, 3, 2, 5, 6, 7, 8, 9, 10}))
is.False(IsSorted([]string{"a", "b", "d", "c", "e", "f", "g", "h", "i", "j"}))
}

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

is.True(IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
return len(s)
}))

is.False(IsSortedByKey([]string{"aa", "b", "ccc"}, func(s string) int {
return len(s)
}))

is.True(IsSortedByKey([]string{"1", "2", "3", "11"}, func(s string) int {
ret, _ := strconv.Atoi(s)
return ret
}))
}

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

Expand Down Expand Up @@ -571,35 +577,29 @@ func TestCompact(t *testing.T) {
is.Equal(r5, []*foo{&e1, &e2, &e3})
}

func TestAssociate(t *testing.T) {
type foo struct {
baz string
bar int
}
transform := func(f *foo) (string, int) {
return f.baz, f.bar
}
testCases := []struct {
in []*foo
expect map[string]int
}{
{
in: []*foo{{baz: "apple", bar: 1}},
expect: map[string]int{"apple": 1},
},
{
in: []*foo{{baz: "apple", bar: 1}, {baz: "banana", bar: 2}},
expect: map[string]int{"apple": 1, "banana": 2},
},
{
in: []*foo{{baz: "apple", bar: 1}, {baz: "apple", bar: 2}},
expect: map[string]int{"apple": 2},
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
is := assert.New(t)
is.Equal(Associate(testCase.in, transform), testCase.expect)
})
}
func TestIsSorted(t *testing.T) {
is := assert.New(t)

is.True(IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}))
is.True(IsSorted([]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}))

is.False(IsSorted([]int{0, 1, 4, 3, 2, 5, 6, 7, 8, 9, 10}))
is.False(IsSorted([]string{"a", "b", "d", "c", "e", "f", "g", "h", "i", "j"}))
}

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

is.True(IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
return len(s)
}))

is.False(IsSortedByKey([]string{"aa", "b", "ccc"}, func(s string) int {
return len(s)
}))

is.True(IsSortedByKey([]string{"1", "2", "3", "11"}, func(s string) int {
ret, _ := strconv.Atoi(s)
return ret
}))
}

0 comments on commit 7a82347

Please sign in to comment.