diff --git a/docs/mathutil.md b/docs/mathutil.md index 24190f8..2262269 100644 --- a/docs/mathutil.md +++ b/docs/mathutil.md @@ -34,6 +34,8 @@ import ( - [RoundToFloat](#RoundToFloat) - [RoundToString](#RoundToString) - [TruncRound](#TruncRound) +- [Range](#Range) +- [RangeWithStep](#RangeWithStep)
@@ -476,3 +478,81 @@ func main() { // 0.125 } ``` + +### Range + +

Creates a slice of numbers from start with specified count, element step is 1.

+ +Signature: + +```go +func Range[T constraints.Integer | constraints.Float](start T, count int) []T +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.Range(1, 4) + result2 := mathutil.Range(1, -4) + result3 := mathutil.Range(-4, 4) + result4 := mathutil.Range(1.0, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // [1 2 3 4] + // [1 2 3 4] + // [-4 -3 -2 -1] + // [1 2 3 4] +} +``` + +### RangeWithStep + +

Creates a slice of numbers from start to end with specified step.

+ +Signature: + +```go +func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.RangeWithStep(1, 4, 1) + result2 := mathutil.RangeWithStep(1, -1, 0) + result3 := mathutil.RangeWithStep(-4, 1, 2) + result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // [1 2 3] + // [] + // [-4 -2 0] + // [1 2.1 3.2] +} +``` diff --git a/docs/mathutil_zh-CN.md b/docs/mathutil_zh-CN.md index 391f74a..3247b26 100644 --- a/docs/mathutil_zh-CN.md +++ b/docs/mathutil_zh-CN.md @@ -34,6 +34,8 @@ import ( - [RoundToFloat](#RoundToFloat) - [RoundToString](#RoundToString) - [TruncRound](#TruncRound) +- [Range](#Range) +- [RangeWithStep](#RangeWithStep)
@@ -476,3 +478,81 @@ func main() { // 0.125 } ``` + +### Range + +

根据指定的起始值和数量,创建一个数字切片。

+ +函数签名: + +```go +func Range[T constraints.Integer | constraints.Float](start T, count int) []T +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.Range(1, 4) + result2 := mathutil.Range(1, -4) + result3 := mathutil.Range(-4, 4) + result4 := mathutil.Range(1.0, 4) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // [1 2 3 4] + // [1 2 3 4] + // [-4 -3 -2 -1] + // [1 2 3 4] +} +``` + +### RangeWithStep + +

根据指定的起始值,结束值,步长,创建一个数字切片。

+ +函数签名: + +```go +func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/mathutil" +) + +func main() { + result1 := mathutil.RangeWithStep(1, 4, 1) + result2 := mathutil.RangeWithStep(1, -1, 0) + result3 := mathutil.RangeWithStep(-4, 1, 2) + result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // [1 2 3] + // [] + // [-4 -2 0] + // [1 2.1 3.2] +} +```