Skip to content

Commit

Permalink
feat: add Slice (samber#171)
Browse files Browse the repository at this point in the history
feat: add `Slice`
  • Loading branch information
nonua authored Jul 3, 2022
1 parent 05d69d7 commit 1493ead
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
31 changes: 27 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,26 @@ sub := lo.Subset(in, -2, math.MaxUint)
// []int{3, 4}
```

### Slice

Slice returns a copy of a slice by specifying two indices (from `start` up to, but not including `end`).

```go
in := []int{0, 1, 2, 3, 4}

slice := lo.Slice(in, 0, 5)
// []int{0, 1, 2, 3, 4}

slice := lo.Slice(in, 2, 3)
// []int{2}

slice := lo.Slice(in, 2, 6)
// []int{2, 3, 4}

slice := lo.Slice(in, 4, 3)
// []int{}
```

### Replace

Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
Expand Down Expand Up @@ -852,7 +872,7 @@ r3 := lo.Clamp(42, -10, 10)
// 10
```

### SumBy
### SumBy

Summarizes the values in a collection using the given return value from the iteration function.
If collection is empty 0 is returned.
Expand Down Expand Up @@ -973,7 +993,7 @@ ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})

### SomeBy

Returns true if the predicate returns true for any of the elements in the collection.
Returns true if the predicate returns true for any of the elements in the collection.
If the collection is empty SomeBy returns false.

```go
Expand Down Expand Up @@ -1539,7 +1559,9 @@ val := lo.Must(time.Parse("2006-01-02", "bad-value"))
```

### Must{0->6}
Must* has the same behavior than Must, but returns multiple values.

Must\* has the same behavior than Must, but returns multiple values.

```go
func example0() (error)
func example1() (int, error)
Expand All @@ -1558,7 +1580,8 @@ val1, val2, val3, val4, val5 := lo.Must5(example5())
val1, val2, val3, val4, val5, val6 := lo.Must6(example6())
```

You can wrap functions like `func (...) (..., ok bool)`.
You can wrap functions like `func (...) (..., ok bool)`.

```go
// math.Signbit(float64) bool
lo.Must0(math.Signbit(v))
Expand Down
19 changes: 19 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,25 @@ func Subset[T any](collection []T, offset int, length uint) []T {
return collection[offset : offset+int(length)]
}

// Slice returns a copy of a slice by specifying two indices (from `start` up to, but not including `end`).
func Slice[T comparable](collection []T, start int, end int) []T {
size := len(collection)

if start >= end {
return []T{}
}

if start > size {
start = size
}

if end > size {
end = size
}

return collection[start:end]
}

// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
func Replace[T comparable](collection []T, old T, new T, n int) []T {
size := len(collection)
Expand Down
36 changes: 36 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,42 @@ func TestSubset(t *testing.T) {
is.Equal([]int{1, 2, 3, 4}, out12)
}

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

in := []int{0, 1, 2, 3, 4}

out1 := Slice(in, 0, 0)
out2 := Slice(in, 0, 1)
out3 := Slice(in, 0, 5)
out4 := Slice(in, 0, 6)
out5 := Slice(in, 1, 1)
out6 := Slice(in, 1, 5)
out7 := Slice(in, 1, 6)
out8 := Slice(in, 4, 5)
out9 := Slice(in, 5, 5)
out10 := Slice(in, 6, 5)
out11 := Slice(in, 6, 6)
out12 := Slice(in, 1, 0)
out13 := Slice(in, 5, 0)
out14 := Slice(in, 6, 4)

is.Equal([]int{}, out1)
is.Equal([]int{0}, out2)
is.Equal([]int{0, 1, 2, 3, 4}, out3)
is.Equal([]int{0, 1, 2, 3, 4}, out4)
is.Equal([]int{}, out5)
is.Equal([]int{1, 2, 3, 4}, out6)
is.Equal([]int{1, 2, 3, 4}, out7)
is.Equal([]int{4}, out8)
is.Equal([]int{}, out9)
is.Equal([]int{}, out10)
is.Equal([]int{}, out11)
is.Equal([]int{}, out12)
is.Equal([]int{}, out13)
is.Equal([]int{}, out14)
}

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

Expand Down

0 comments on commit 1493ead

Please sign in to comment.