-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from jsrdxzw/master
Add Range
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package lo | ||
|
||
import "golang.org/x/exp/constraints" | ||
|
||
// Range creates an array of numbers (positive and/or negative) with given length. | ||
func Range(elementNum int) []int { | ||
length := If(elementNum < 0, -elementNum).Else(elementNum) | ||
result := make([]int, length) | ||
step := If(elementNum < 0, -1).Else(1) | ||
for i, j := 0, 0; i < length; i, j = i+1, j+step { | ||
result[i] = j | ||
} | ||
return result | ||
} | ||
|
||
// RangeFrom creates an array of numbers from start with specified length. | ||
func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []T { | ||
length := If(elementNum < 0, -elementNum).Else(elementNum) | ||
result := make([]T, length) | ||
step := If(elementNum < 0, -1).Else(1) | ||
for i, j := 0, start; i < length; i, j = i+1, j+T(step) { | ||
result[i] = j | ||
} | ||
return result | ||
} | ||
|
||
// RangeWithSteps creates an array of numbers (positive and/or negative) progressing from start up to, but not including end. | ||
// step set to zero will return empty array. | ||
func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T { | ||
result := []T{} | ||
if start == end || step == 0 { | ||
return result | ||
} | ||
if start < end { | ||
if step < 0 { | ||
return result | ||
} | ||
for i := start; i < end; i += step { | ||
result = append(result, i) | ||
} | ||
return result | ||
} | ||
if step > 0 { | ||
return result | ||
} | ||
for i := start; i > end; i += step { | ||
result = append(result, i) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package lo | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRange(t *testing.T) { | ||
is := assert.New(t) | ||
result1 := Range(4) | ||
result2 := Range(-4) | ||
result3 := Range(0) | ||
is.Equal(result1, []int{0, 1, 2, 3}) | ||
is.Equal(result2, []int{0, -1, -2, -3}) | ||
is.Equal(result3, []int{}) | ||
} | ||
|
||
func TestRangeFrom(t *testing.T) { | ||
is := assert.New(t) | ||
result1 := RangeFrom(1, 5) | ||
result2 := RangeFrom(-1, -5) | ||
result3 := RangeFrom(10, 0) | ||
result4 := RangeFrom[float64](2.0, 3) | ||
result5 := RangeFrom[float64](-2.0, -3) | ||
is.Equal(result1, []int{1, 2, 3, 4, 5}) | ||
is.Equal(result2, []int{-1, -2, -3, -4, -5}) | ||
is.Equal(result3, []int{}) | ||
is.Equal(result4, []float64{2.0, 3.0, 4.0}) | ||
is.Equal(result5, []float64{-2.0, -3.0, -4.0}) | ||
} | ||
|
||
func TestRangeClose(t *testing.T) { | ||
is := assert.New(t) | ||
result1 := RangeWithSteps(0, 20, 6) | ||
result2 := RangeWithSteps(0, 3, -5) | ||
result3 := RangeWithSteps(1, 1, 0) | ||
result4 := RangeWithSteps[float64](1.0, 4.0, 2.0) | ||
result5 := RangeWithSteps[float32](-1.0, -4.0, -1.0) | ||
is.Equal(result1, []int{0, 6, 12, 18}) | ||
is.Equal(result2, []int{}) | ||
is.Equal(result3, []int{}) | ||
is.Equal(result4, []float64{1.0, 3.0}) | ||
is.Equal(result5, []float32{-1.0, -2.0, -3.0}) | ||
} |