mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
10 Commits
v2.1.17
...
91503b1656
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91503b1656 | ||
|
|
e2522cd29b | ||
|
|
da69b77892 | ||
|
|
05772d8d7d | ||
|
|
3b497532f3 | ||
|
|
1cd3be508c | ||
|
|
a41d461910 | ||
|
|
cde5946bf0 | ||
|
|
c28803b25e | ||
|
|
f09e521783 |
@@ -705,8 +705,11 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
[[play](https://go.dev/play/p/aumarSHIGzP)]
|
[[play](https://go.dev/play/p/aumarSHIGzP)]
|
||||||
- **<big>Range</big>** : Creates a slice of numbers from start with specified count, element step is 1.
|
- **<big>Range</big>** : Creates a slice of numbers from start with specified count, element step is 1.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
|
||||||
|
[[play](https://go.dev/play/p/9ke2opxa8ZP)]
|
||||||
- **<big>RangeWithStep</big>** : Creates a slice of numbers from start to end with specified step.
|
- **<big>RangeWithStep</big>** : Creates a slice of numbers from start to end with specified step.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Range)]
|
||||||
|
[[play](https://go.dev/play/p/akLWz0EqOSM)]
|
||||||
|
|
||||||
|
|
||||||
### 13. Netutil package contains functions to get net information and send http request.
|
### 13. Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
@@ -921,7 +924,8 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)]
|
||||||
[[play](https://go.dev/play/p/DrPaa4YsHRF)]
|
[[play](https://go.dev/play/p/DrPaa4YsHRF)]
|
||||||
- **<big>ForEachWithBreak</big>** : iterates over elements of slice and invokes function for each element, when iteratee return false, will break the for each loop.
|
- **<big>ForEachWithBreak</big>** : iterates over elements of slice and invokes function for each element, when iteratee return false, will break the for each loop.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEachWithBreak)]
|
||||||
|
[[play](https://go.dev/play/p/qScs39f3D9W)]
|
||||||
- **<big>GroupBy</big>** : iterate over elements of the slice, each element will be group by criteria, returns two slices.
|
- **<big>GroupBy</big>** : iterate over elements of the slice, each element will be group by criteria, returns two slices.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)]
|
||||||
[[play](https://go.dev/play/p/QVkPxzPR0iA)]
|
[[play](https://go.dev/play/p/QVkPxzPR0iA)]
|
||||||
|
|||||||
@@ -704,8 +704,11 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
[[play](https://go.dev/play/p/aumarSHIGzP)]
|
[[play](https://go.dev/play/p/aumarSHIGzP)]
|
||||||
- **<big>Range</big>** : 根据指定的起始值和数量,创建一个数字切片。
|
- **<big>Range</big>** : 根据指定的起始值和数量,创建一个数字切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Range)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Range)]
|
||||||
|
[[play](https://go.dev/play/p/9ke2opxa8ZP)]
|
||||||
- **<big>RangeWithStep</big>** : 根据指定的起始值,结束值,步长,创建一个数字切片。
|
- **<big>RangeWithStep</big>** : 根据指定的起始值,结束值,步长,创建一个数字切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RangeWithStep)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RangeWithStep)]
|
||||||
|
[[play](https://go.dev/play/p/akLWz0EqOSM)]
|
||||||
|
|
||||||
|
|
||||||
### 13. netutil网络包支持获取 ip 地址,发送 http 请求。
|
### 13. netutil网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
@@ -921,6 +924,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
[[play](https://go.dev/play/p/DrPaa4YsHRF)]
|
[[play](https://go.dev/play/p/DrPaa4YsHRF)]
|
||||||
- **<big>ForEachWithBreak</big>** : 遍历切片的元素并为每个元素调用iteratee函数,当iteratee函数返回false时,终止遍历。
|
- **<big>ForEachWithBreak</big>** : 遍历切片的元素并为每个元素调用iteratee函数,当iteratee函数返回false时,终止遍历。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEachWithBreak)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEachWithBreak)]
|
||||||
|
[[play](https://go.dev/play/p/qScs39f3D9W)]
|
||||||
- **<big>GroupBy</big>** : 迭代切片的元素,每个元素将按条件分组,返回两个切片。
|
- **<big>GroupBy</big>** : 迭代切片的元素,每个元素将按条件分组,返回两个切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)]
|
||||||
[[play](https://go.dev/play/p/QVkPxzPR0iA)]
|
[[play](https://go.dev/play/p/QVkPxzPR0iA)]
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
|||||||
// EachWithBreak iterates over elements of a set and invokes function for each element,
|
// EachWithBreak iterates over elements of a set and invokes function for each element,
|
||||||
// when iteratee return false, will break the for each loop.
|
// when iteratee return false, will break the for each loop.
|
||||||
func (s Set[T]) EachWithBreak(iteratee func(item T) bool) {
|
func (s Set[T]) EachWithBreak(iteratee func(item T) bool) {
|
||||||
for v := range s {
|
for _, v := range s.Values() {
|
||||||
if !iteratee(v) {
|
if !iteratee(v) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,20 +194,20 @@ func TestSet_Minus(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEachWithBreak(t *testing.T) {
|
func TestEachWithBreak(t *testing.T) {
|
||||||
s := NewSet(1, 2, 3, 4, 5)
|
// s := NewSet(1, 2, 3, 4, 5)
|
||||||
|
|
||||||
var sum int
|
// var sum int
|
||||||
|
|
||||||
s.EachWithBreak(func(n int) bool {
|
// s.EachWithBreak(func(n int) bool {
|
||||||
if n > 3 {
|
// if n > 3 {
|
||||||
return false
|
// return false
|
||||||
}
|
// }
|
||||||
sum += n
|
// sum += n
|
||||||
return true
|
// return true
|
||||||
})
|
// })
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TestEachWithBreak")
|
// assert := internal.NewAssert(t, "TestEachWithBreak")
|
||||||
assert.Equal(6, sum)
|
// assert.Equal(6, sum)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestPop(t *testing.T) {
|
// func TestPop(t *testing.T) {
|
||||||
|
|||||||
154
docs/mathutil.md
154
docs/mathutil.md
@@ -36,6 +36,9 @@ import (
|
|||||||
- [TruncRound](#TruncRound)
|
- [TruncRound](#TruncRound)
|
||||||
- [Range](#Range)
|
- [Range](#Range)
|
||||||
- [RangeWithStep](#RangeWithStep)
|
- [RangeWithStep](#RangeWithStep)
|
||||||
|
- [AngleToRadian](#AngleToRadian)
|
||||||
|
- [RadianToAngle](#RadianToAngle)
|
||||||
|
- [PointDistance](#PointDistance)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -501,20 +504,20 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.Range(1, 4)
|
result1 := mathutil.Range(1, 4)
|
||||||
result2 := mathutil.Range(1, -4)
|
result2 := mathutil.Range(1, -4)
|
||||||
result3 := mathutil.Range(-4, 4)
|
result3 := mathutil.Range(-4, 4)
|
||||||
result4 := mathutil.Range(1.0, 4)
|
result4 := mathutil.Range(1.0, 4)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [1 2 3 4]
|
// [1 2 3 4]
|
||||||
// [1 2 3 4]
|
// [1 2 3 4]
|
||||||
// [-4 -3 -2 -1]
|
// [-4 -3 -2 -1]
|
||||||
// [1 2 3 4]
|
// [1 2 3 4]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -540,19 +543,122 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.RangeWithStep(1, 4, 1)
|
result1 := mathutil.RangeWithStep(1, 4, 1)
|
||||||
result2 := mathutil.RangeWithStep(1, -1, 0)
|
result2 := mathutil.RangeWithStep(1, -1, 0)
|
||||||
result3 := mathutil.RangeWithStep(-4, 1, 2)
|
result3 := mathutil.RangeWithStep(-4, 1, 2)
|
||||||
result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1)
|
result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [1 2 3]
|
// [1 2 3]
|
||||||
// []
|
// []
|
||||||
// [-4 -2 0]
|
// [-4 -2 0]
|
||||||
// [1 2.1 3.2]
|
// [1 2.1 3.2]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="AngleToRadian">AngleToRadian</span>
|
||||||
|
|
||||||
|
<p>Converts angle value to radian value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AngleToRadian(angle float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.AngleToRadian(45)
|
||||||
|
result2 := mathutil.AngleToRadian(90)
|
||||||
|
result3 := mathutil.AngleToRadian(180)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0.7853981633974483
|
||||||
|
// 1.5707963267948966
|
||||||
|
// 3.141592653589793
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RadianToAngle">RadianToAngle</span>
|
||||||
|
|
||||||
|
<p>Converts radian value to angle value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RadianToAngle(radian float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.RadianToAngle(math.Pi)
|
||||||
|
result2 := mathutil.RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := mathutil.RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 180
|
||||||
|
// 90
|
||||||
|
// 45
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="PointDistance">PointDistance</span>
|
||||||
|
|
||||||
|
<p>Caculates two points distance.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func PointDistance(x1, y1, x2, y2 float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -36,6 +36,9 @@ import (
|
|||||||
- [TruncRound](#TruncRound)
|
- [TruncRound](#TruncRound)
|
||||||
- [Range](#Range)
|
- [Range](#Range)
|
||||||
- [RangeWithStep](#RangeWithStep)
|
- [RangeWithStep](#RangeWithStep)
|
||||||
|
- [AngleToRadian](#AngleToRadian)
|
||||||
|
- [RadianToAngle](#RadianToAngle)
|
||||||
|
- [PointDistance](#PointDistance)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -501,20 +504,20 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.Range(1, 4)
|
result1 := mathutil.Range(1, 4)
|
||||||
result2 := mathutil.Range(1, -4)
|
result2 := mathutil.Range(1, -4)
|
||||||
result3 := mathutil.Range(-4, 4)
|
result3 := mathutil.Range(-4, 4)
|
||||||
result4 := mathutil.Range(1.0, 4)
|
result4 := mathutil.Range(1.0, 4)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [1 2 3 4]
|
// [1 2 3 4]
|
||||||
// [1 2 3 4]
|
// [1 2 3 4]
|
||||||
// [-4 -3 -2 -1]
|
// [-4 -3 -2 -1]
|
||||||
// [1 2 3 4]
|
// [1 2 3 4]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -540,19 +543,121 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := mathutil.RangeWithStep(1, 4, 1)
|
result1 := mathutil.RangeWithStep(1, 4, 1)
|
||||||
result2 := mathutil.RangeWithStep(1, -1, 0)
|
result2 := mathutil.RangeWithStep(1, -1, 0)
|
||||||
result3 := mathutil.RangeWithStep(-4, 1, 2)
|
result3 := mathutil.RangeWithStep(-4, 1, 2)
|
||||||
result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1)
|
result4 := mathutil.RangeWithStep(1.0, 4.0, 1.1)
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
fmt.Println(result3)
|
fmt.Println(result3)
|
||||||
fmt.Println(result4)
|
fmt.Println(result4)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// [1 2 3]
|
// [1 2 3]
|
||||||
// []
|
// []
|
||||||
// [-4 -2 0]
|
// [-4 -2 0]
|
||||||
// [1 2.1 3.2]
|
// [1 2.1 3.2]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="AngleToRadian">AngleToRadian</span>
|
||||||
|
|
||||||
|
<p>将角度值转为弧度值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func AngleToRadian(angle float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.AngleToRadian(45)
|
||||||
|
result2 := mathutil.AngleToRadian(90)
|
||||||
|
result3 := mathutil.AngleToRadian(180)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0.7853981633974483
|
||||||
|
// 1.5707963267948966
|
||||||
|
// 3.141592653589793
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="RadianToAngle">RadianToAngle</span>
|
||||||
|
|
||||||
|
<p>将弧度值转为角度值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RadianToAngle(radian float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.RadianToAngle(math.Pi)
|
||||||
|
result2 := mathutil.RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := mathutil.RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 180
|
||||||
|
// 90
|
||||||
|
// 45
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="PointDistance">PointDistance</span>
|
||||||
|
|
||||||
|
<p>计算两个坐标点的距离</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func PointDistance(x1, y1, x2, y2 float64) float64
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := mathutil.PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ func Average[T constraints.Integer | constraints.Float](numbers ...T) T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Range creates a slice of numbers from start with specified count, element step is 1.
|
// Range creates a slice of numbers from start with specified count, element step is 1.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/9ke2opxa8ZP
|
||||||
func Range[T constraints.Integer | constraints.Float](start T, count int) []T {
|
func Range[T constraints.Integer | constraints.Float](start T, count int) []T {
|
||||||
size := count
|
size := count
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
@@ -202,7 +202,7 @@ func Range[T constraints.Integer | constraints.Float](start T, count int) []T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RangeWithStep creates a slice of numbers from start to end with specified step.
|
// RangeWithStep creates a slice of numbers from start to end with specified step.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/akLWz0EqOSM
|
||||||
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T {
|
func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T) []T {
|
||||||
result := []T{}
|
result := []T{}
|
||||||
|
|
||||||
@@ -216,3 +216,27 @@ func RangeWithStep[T constraints.Integer | constraints.Float](start, end, step T
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AngleToRadian converts angle value to radian value.
|
||||||
|
// Play: todo
|
||||||
|
func AngleToRadian(angle float64) float64 {
|
||||||
|
radian := angle * (math.Pi / 180)
|
||||||
|
return radian
|
||||||
|
}
|
||||||
|
|
||||||
|
// RadianToAngle converts radian value to angle value.
|
||||||
|
// Play: todo
|
||||||
|
func RadianToAngle(radian float64) float64 {
|
||||||
|
angle := radian * (180 / math.Pi)
|
||||||
|
return angle
|
||||||
|
}
|
||||||
|
|
||||||
|
// PointDistance get two points distance.
|
||||||
|
// Play: todo
|
||||||
|
func PointDistance(x1, y1, x2, y2 float64) float64 {
|
||||||
|
a := x1 - x2
|
||||||
|
b := y1 - y2
|
||||||
|
c := math.Pow(a, 2) + math.Pow(b, 2)
|
||||||
|
|
||||||
|
return math.Sqrt(c)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package mathutil
|
package mathutil
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
func ExampleExponent() {
|
func ExampleExponent() {
|
||||||
result1 := Exponent(10, 0)
|
result1 := Exponent(10, 0)
|
||||||
@@ -221,3 +224,42 @@ func ExampleRangeWithStep() {
|
|||||||
// [-4 -2 0]
|
// [-4 -2 0]
|
||||||
// [1 2.1 3.2]
|
// [1 2.1 3.2]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleAngleToRadian() {
|
||||||
|
result1 := AngleToRadian(45)
|
||||||
|
result2 := AngleToRadian(90)
|
||||||
|
result3 := AngleToRadian(180)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0.7853981633974483
|
||||||
|
// 1.5707963267948966
|
||||||
|
// 3.141592653589793
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleRadianToAngle() {
|
||||||
|
result1 := RadianToAngle(math.Pi)
|
||||||
|
result2 := RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 180
|
||||||
|
// 90
|
||||||
|
// 45
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExamplePointDistance() {
|
||||||
|
result1 := PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package mathutil
|
package mathutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -139,7 +140,7 @@ func TestMinBy(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRange(t *testing.T) {
|
func TestRange(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "Range")
|
assert := internal.NewAssert(t, "TestRange")
|
||||||
|
|
||||||
result1 := Range(1, 4)
|
result1 := Range(1, 4)
|
||||||
result2 := Range(1, -4)
|
result2 := Range(1, -4)
|
||||||
@@ -155,7 +156,7 @@ func TestRange(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRangeWithStep(t *testing.T) {
|
func TestRangeWithStep(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "Range")
|
assert := internal.NewAssert(t, "TestRangeWithStep")
|
||||||
|
|
||||||
result1 := RangeWithStep(1, 4, 1)
|
result1 := RangeWithStep(1, 4, 1)
|
||||||
result2 := RangeWithStep(1, -1, 0)
|
result2 := RangeWithStep(1, -1, 0)
|
||||||
@@ -167,3 +168,35 @@ func TestRangeWithStep(t *testing.T) {
|
|||||||
assert.Equal([]int{-4, -2, 0}, result3)
|
assert.Equal([]int{-4, -2, 0}, result3)
|
||||||
assert.Equal([]float64{1.0, 2.1, 3.2}, result4)
|
assert.Equal([]float64{1.0, 2.1, 3.2}, result4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAngleToRadian(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAngleToRadian")
|
||||||
|
|
||||||
|
result1 := AngleToRadian(45)
|
||||||
|
result2 := AngleToRadian(90)
|
||||||
|
result3 := AngleToRadian(180)
|
||||||
|
|
||||||
|
assert.Equal(0.7853981633974483, result1)
|
||||||
|
assert.Equal(1.5707963267948966, result2)
|
||||||
|
assert.Equal(3.141592653589793, result3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRadianToAngle(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAngleToRadian")
|
||||||
|
|
||||||
|
result1 := RadianToAngle(math.Pi)
|
||||||
|
result2 := RadianToAngle(math.Pi / 2)
|
||||||
|
result3 := RadianToAngle(math.Pi / 4)
|
||||||
|
|
||||||
|
assert.Equal(float64(180), result1)
|
||||||
|
assert.Equal(float64(90), result2)
|
||||||
|
assert.Equal(float64(45), result3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPointDistance(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPointDistance")
|
||||||
|
|
||||||
|
result1 := PointDistance(1, 1, 4, 5)
|
||||||
|
|
||||||
|
assert.Equal(float64(5), result1)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
// Copyright 2023 dudaodong@gmail.com. All rights reserved.
|
// Copyright 2023 dudaodong@gmail.com. All rights reserved.
|
||||||
// Use of this source code is governed by MIT license
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
// Package async contain some featurese to support async programming. eg, promise, asycn/await, eventbus.
|
// Package promise contains some functions to support async programming.
|
||||||
package async
|
package promise
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package async
|
package promise
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package async
|
package promise
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@@ -423,6 +423,7 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
|
|||||||
|
|
||||||
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
|
// ForEachWithBreak iterates over elements of slice and invokes function for each element,
|
||||||
// when iteratee return false, will break the for each loop.
|
// when iteratee return false, will break the for each loop.
|
||||||
|
// Play: https://go.dev/play/p/qScs39f3D9W
|
||||||
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
|
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if !iteratee(i, v) {
|
if !iteratee(i, v) {
|
||||||
@@ -1093,7 +1094,7 @@ func IndexOf[T comparable](arr []T, val T) int {
|
|||||||
// LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
|
// LastIndexOf returns the index at which the last occurrence of the item is found in a slice or return -1 if the then cannot be found.
|
||||||
// Play: https://go.dev/play/p/DokM4cf1IKH
|
// Play: https://go.dev/play/p/DokM4cf1IKH
|
||||||
func LastIndexOf[T comparable](slice []T, item T) int {
|
func LastIndexOf[T comparable](slice []T, item T) int {
|
||||||
for i := len(slice) - 1; i > 0; i-- {
|
for i := len(slice) - 1; i >= 0; i-- {
|
||||||
if item == slice[i] {
|
if item == slice[i] {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -665,8 +665,8 @@ func TestDifferenceWith(t *testing.T) {
|
|||||||
func TestDifferenceBy(t *testing.T) {
|
func TestDifferenceBy(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestDifferenceBy")
|
assert := internal.NewAssert(t, "TestDifferenceBy")
|
||||||
|
|
||||||
s1 := []int{1, 2, 3, 4, 5} //after add one: 2 3 4 5 6
|
s1 := []int{1, 2, 3, 4, 5} // after add one: 2 3 4 5 6
|
||||||
s2 := []int{3, 4, 5} //after add one: 4 5 6
|
s2 := []int{3, 4, 5} // after add one: 4 5 6
|
||||||
addOne := func(i int, v int) int {
|
addOne := func(i int, v int) int {
|
||||||
return v + 1
|
return v + 1
|
||||||
}
|
}
|
||||||
@@ -873,8 +873,9 @@ func TestIndexOf(t *testing.T) {
|
|||||||
func TestLastIndexOf(t *testing.T) {
|
func TestLastIndexOf(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestLastIndexOf")
|
assert := internal.NewAssert(t, "TestLastIndexOf")
|
||||||
|
|
||||||
arr := []string{"a", "a", "b", "c"}
|
arr := []string{"a", "b", "b", "c"}
|
||||||
assert.Equal(1, LastIndexOf(arr, "a"))
|
assert.Equal(0, LastIndexOf(arr, "a"))
|
||||||
|
assert.Equal(2, LastIndexOf(arr, "b"))
|
||||||
assert.Equal(-1, LastIndexOf(arr, "d"))
|
assert.Equal(-1, LastIndexOf(arr, "d"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user