mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 22:22:29 +08:00
Compare commits
50 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb23c9eef8 | ||
|
|
b4cd0750e4 | ||
|
|
c960841491 | ||
|
|
5d6f9443fd | ||
|
|
5483380066 | ||
|
|
20b9e5353e | ||
|
|
1ec3a5af87 | ||
|
|
f31dde97b1 | ||
|
|
1718fd1cf1 | ||
|
|
513c0f829c | ||
|
|
c51a806aff | ||
|
|
3c16d50c4b | ||
|
|
21dd6ab8aa | ||
|
|
f5bf5183cc | ||
|
|
f28b5b2f92 | ||
|
|
79867e8a63 | ||
|
|
19939c2b03 | ||
|
|
bf7ffbfa8d | ||
|
|
cbb46f9cb4 | ||
|
|
3ad142d5d7 | ||
|
|
1a436aeb41 | ||
|
|
1af8fe8daf | ||
|
|
ae54c8db6f | ||
|
|
be942ec33e | ||
|
|
fafb59dad6 | ||
|
|
e36c01cac9 | ||
|
|
aa0afa8d94 | ||
|
|
18e0031e0e | ||
|
|
a5018c110c | ||
|
|
142deb83b2 | ||
|
|
ccc0188352 | ||
|
|
98dba83107 | ||
|
|
9c6aff9030 | ||
|
|
7fd9a94922 | ||
|
|
dd3b1f3aed | ||
|
|
f9dce592e8 | ||
|
|
c4b0967623 | ||
|
|
41613061d5 | ||
|
|
50ef63e27d | ||
|
|
3ae4a35d04 | ||
|
|
89ea0ee15a | ||
|
|
85399e23ed | ||
|
|
19a6f218e6 | ||
|
|
ccaf290b63 | ||
|
|
54745d512c | ||
|
|
39234d4722 | ||
|
|
4b4310265b | ||
|
|
d8a982bf07 | ||
|
|
0334a6f02b | ||
|
|
ae0c613b8e |
38
README.md
38
README.md
@@ -4,13 +4,12 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -36,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.6. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.9. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.6 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.2.9 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -58,7 +57,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -175,6 +174,14 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
||||||
|
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix)
|
||||||
|
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow)
|
||||||
|
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat)
|
||||||
|
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601)
|
||||||
|
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)
|
||||||
|
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat)
|
||||||
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
||||||
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
||||||
|
|
||||||
### Fileutil package implements some basic functions for file operations.
|
### Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
@@ -223,6 +230,22 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
||||||
|
|
||||||
|
|
||||||
|
### Maputil package includes some functions to manipulate map.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/maputil"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)
|
||||||
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Filter)
|
||||||
|
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Intersect)
|
||||||
|
- [Keys](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Keys)
|
||||||
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Merge)
|
||||||
|
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Minus)
|
||||||
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
|
||||||
|
|
||||||
|
|
||||||
### Mathutil package implements some functions for math calculation.
|
### Mathutil package implements some functions for math calculation.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -230,9 +253,12 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Average)
|
||||||
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
||||||
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
||||||
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)
|
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)
|
||||||
|
- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Max)
|
||||||
|
- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Min)
|
||||||
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)
|
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)
|
||||||
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)
|
||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
||||||
@@ -309,6 +335,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
|
||||||
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
|
||||||
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
|
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice)
|
||||||
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
|
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
|
||||||
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
|
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
|
||||||
@@ -320,6 +347,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
|
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
|
||||||
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
|
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
|
||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
|
||||||
|
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SymmetricDifference)
|
||||||
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
|
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
|
||||||
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
|
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
|
||||||
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
|
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
|
||||||
|
|||||||
@@ -4,13 +4,12 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -36,9 +35,9 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.6。</b>
|
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.9。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.6 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.2.9 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -58,7 +57,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -174,6 +173,14 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
||||||
|
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)
|
||||||
|
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)
|
||||||
|
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat)
|
||||||
|
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)
|
||||||
|
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)
|
||||||
|
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)
|
||||||
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
||||||
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
||||||
|
|
||||||
### fileutil包支持文件基本操作。
|
### fileutil包支持文件基本操作。
|
||||||
|
|
||||||
@@ -222,6 +229,22 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
||||||
|
|
||||||
|
|
||||||
|
### maputil包包括一些操作map的函数.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/maputil"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 函数列表:
|
||||||
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ForEach)
|
||||||
|
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Filter)
|
||||||
|
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Intersect)
|
||||||
|
- [Keys](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Keys)
|
||||||
|
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Merge)
|
||||||
|
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Minus)
|
||||||
|
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
|
||||||
|
|
||||||
### mathutil包实现了一些数学计算的函数。
|
### mathutil包实现了一些数学计算的函数。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -229,9 +252,12 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
```
|
```
|
||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
- [Average](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Average)
|
||||||
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
||||||
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
||||||
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)
|
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)
|
||||||
|
- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Max)
|
||||||
|
- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Min)
|
||||||
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)
|
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)
|
||||||
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)
|
||||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
||||||
@@ -307,6 +333,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [FlattenDeep](#FlattenDeep)
|
- [FlattenDeep](#FlattenDeep)
|
||||||
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
|
||||||
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
|
||||||
|
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
|
||||||
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
|
- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice)
|
||||||
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
|
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
|
||||||
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
|
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
|
||||||
@@ -318,6 +345,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
|
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
|
||||||
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
|
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
|
||||||
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
|
||||||
|
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SymmetricDifference)
|
||||||
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
|
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
|
||||||
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
|
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
|
||||||
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
|
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ package algorithm
|
|||||||
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
import "github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
|
|
||||||
// BubbleSort use bubble to sort slice.
|
// BubbleSort use bubble to sort slice.
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
for j := 0; j < len(slice)-1-i; j++ {
|
for j := 0; j < len(slice)-1-i; j++ {
|
||||||
isCurrGreatThanNext := comparator.Compare(slice[j], slice[j+1]) == 1
|
isCurrGreatThanNext := comparator.Compare(slice[j], slice[j+1]) == 1
|
||||||
@@ -16,38 +16,26 @@ func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertionSort use insertion to sort slice.
|
// InsertionSort use insertion to sort slice.
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
for i := 0; i < len(slice); i++ {
|
||||||
if size <= 1 {
|
for j := i; j > 0; j-- {
|
||||||
return slice
|
isPreLessThanCurrent := comparator.Compare(slice[j], slice[j-1]) == -1
|
||||||
}
|
if isPreLessThanCurrent {
|
||||||
|
swap(slice, j, j-1)
|
||||||
for i := 1; i < size; i++ {
|
} else {
|
||||||
currentItem := slice[i]
|
break
|
||||||
preIndex := i - 1
|
}
|
||||||
preItem := slice[preIndex]
|
|
||||||
|
|
||||||
isPreLessThanCurrent := comparator.Compare(preItem, currentItem) == -1
|
|
||||||
for preIndex >= 0 && isPreLessThanCurrent {
|
|
||||||
slice[preIndex+1] = slice[preIndex]
|
|
||||||
preIndex--
|
|
||||||
}
|
}
|
||||||
|
|
||||||
slice[preIndex+1] = preItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectionSort use selection to sort slice.
|
// SelectionSort use selection to sort slice.
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
for i := 0; i < len(slice); i++ {
|
for i := 0; i < len(slice); i++ {
|
||||||
min := i
|
min := i
|
||||||
|
|
||||||
for j := i + 1; j < len(slice); j++ {
|
for j := i + 1; j < len(slice); j++ {
|
||||||
if comparator.Compare(slice[j], slice[min]) == -1 {
|
if comparator.Compare(slice[j], slice[min]) == -1 {
|
||||||
min = j
|
min = j
|
||||||
@@ -55,15 +43,11 @@ func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []
|
|||||||
}
|
}
|
||||||
swap(slice, i, min)
|
swap(slice, i, min)
|
||||||
}
|
}
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShellSort shell sort slice.
|
// ShellSort shell sort slice.
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
if size <= 1 {
|
|
||||||
return slice
|
|
||||||
}
|
|
||||||
|
|
||||||
gap := 1
|
gap := 1
|
||||||
for gap < size/3 {
|
for gap < size/3 {
|
||||||
@@ -76,21 +60,17 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
|||||||
swap(slice, j, j-gap)
|
swap(slice, j, j-gap)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gap /= 3
|
gap = gap / 3
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T {
|
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
if lowIndex < highIndex {
|
if lowIndex < highIndex {
|
||||||
p := partition(slice, lowIndex, highIndex, comparator)
|
p := partition(slice, lowIndex, highIndex, comparator)
|
||||||
QuickSort(slice, lowIndex, p-1, comparator)
|
QuickSort(slice, lowIndex, p-1, comparator)
|
||||||
QuickSort(slice, p+1, highIndex, comparator)
|
QuickSort(slice, p+1, highIndex, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// partition split slice into two parts
|
// partition split slice into two parts
|
||||||
@@ -110,7 +90,7 @@ func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HeapSort use heap to sort slice
|
// HeapSort use heap to sort slice
|
||||||
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
size := len(slice)
|
size := len(slice)
|
||||||
|
|
||||||
for i := size/2 - 1; i >= 0; i-- {
|
for i := size/2 - 1; i >= 0; i-- {
|
||||||
@@ -120,8 +100,6 @@ func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
|
|||||||
swap(slice, 0, j)
|
swap(slice, 0, j)
|
||||||
sift(slice, 0, j-1, comparator)
|
sift(slice, 0, j-1, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
@@ -145,15 +123,17 @@ func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraint
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MergeSort merge sorting for slice
|
// MergeSort merge sorting for slice
|
||||||
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T {
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
|
mergeSort(slice, 0, len(slice)-1, comparator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
if lowIndex < highIndex {
|
if lowIndex < highIndex {
|
||||||
mid := (lowIndex + highIndex) / 2
|
mid := (lowIndex + highIndex) / 2
|
||||||
MergeSort(slice, lowIndex, mid, comparator)
|
mergeSort(slice, lowIndex, mid, comparator)
|
||||||
MergeSort(slice, mid+1, highIndex, comparator)
|
mergeSort(slice, mid+1, highIndex, comparator)
|
||||||
merge(slice, lowIndex, mid, highIndex, comparator)
|
merge(slice, lowIndex, mid, highIndex, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
|
|||||||
@@ -28,22 +28,15 @@ func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
//decending order
|
|
||||||
// if p1.Age > p2.Age {
|
|
||||||
// return -1
|
|
||||||
// } else if p1.Age < p2.Age {
|
|
||||||
// return 1
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var peoples = []people{
|
// var peoples = []people{
|
||||||
{Name: "a", Age: 20},
|
// {Name: "a", Age: 20},
|
||||||
{Name: "b", Age: 10},
|
// {Name: "b", Age: 10},
|
||||||
{Name: "c", Age: 17},
|
// {Name: "c", Age: 17},
|
||||||
{Name: "d", Age: 8},
|
// {Name: "d", Age: 8},
|
||||||
{Name: "e", Age: 28},
|
// {Name: "e", Age: 28},
|
||||||
}
|
// }
|
||||||
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
@@ -60,40 +53,51 @@ func (c *intComparator) Compare(v1 any, v2 any) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
var intSlice = []int{2, 1, 5, 3, 6, 4}
|
// var intSlice = []int{2, 1, 5, 3, 6, 4}
|
||||||
|
|
||||||
func TestBubbleSortForStructSlice(t *testing.T) {
|
func TestBubbleSortForStructSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := BubbleSort(peoples, comparator)
|
BubbleSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBubbleSortForIntSlice(t *testing.T) {
|
func TestBubbleSortForIntSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedInt := BubbleSort(intSlice, comparator)
|
BubbleSort(numbers, comparator)
|
||||||
expected := []int{1, 2, 3, 4, 5, 6}
|
|
||||||
|
|
||||||
asssert.Equal(expected, sortedInt)
|
asssert.Equal([]int{1, 2, 3, 4, 5, 6}, numbers)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInsertionSort(t *testing.T) {
|
func TestInsertionSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestInsertionSort")
|
asssert := internal.NewAssert(t, "TestInsertionSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
InsertionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -101,12 +105,18 @@ func TestInsertionSort(t *testing.T) {
|
|||||||
func TestSelectionSort(t *testing.T) {
|
func TestSelectionSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestSelectionSort")
|
asssert := internal.NewAssert(t, "TestSelectionSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
SelectionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -114,12 +124,18 @@ func TestSelectionSort(t *testing.T) {
|
|||||||
func TestShellSort(t *testing.T) {
|
func TestShellSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestShellSort")
|
asssert := internal.NewAssert(t, "TestShellSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := ShellSort(peoples, comparator)
|
ShellSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -127,12 +143,18 @@ func TestShellSort(t *testing.T) {
|
|||||||
func TestQuickSort(t *testing.T) {
|
func TestQuickSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestQuickSort")
|
asssert := internal.NewAssert(t, "TestQuickSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := QuickSort(peoples, 0, len(peoples)-1, comparator)
|
QuickSort(peoples, 0, len(peoples)-1, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -140,12 +162,18 @@ func TestQuickSort(t *testing.T) {
|
|||||||
func TestHeapSort(t *testing.T) {
|
func TestHeapSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestHeapSort")
|
asssert := internal.NewAssert(t, "TestHeapSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := HeapSort(peoples, comparator)
|
HeapSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -153,12 +181,18 @@ func TestHeapSort(t *testing.T) {
|
|||||||
func TestMergeSort(t *testing.T) {
|
func TestMergeSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestMergeSort")
|
asssert := internal.NewAssert(t, "TestMergeSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := MergeSort(peoples, 0, len(peoples)-1, comparator)
|
MergeSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
@@ -166,6 +200,13 @@ func TestMergeSort(t *testing.T) {
|
|||||||
func TestCountSort(t *testing.T) {
|
func TestCountSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestCountSort")
|
asssert := internal.NewAssert(t, "TestCountSort")
|
||||||
|
|
||||||
|
peoples := []people{
|
||||||
|
{Name: "a", Age: 20},
|
||||||
|
{Name: "b", Age: 10},
|
||||||
|
{Name: "c", Age: 17},
|
||||||
|
{Name: "d", Age: 8},
|
||||||
|
{Name: "e", Age: 28},
|
||||||
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := CountSort(peoples, comparator)
|
sortedPeopleByAge := CountSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -21,14 +22,44 @@ func ToBool(s string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToBytes convert interface to bytes
|
// ToBytes convert interface to bytes
|
||||||
func ToBytes(data any) ([]byte, error) {
|
func ToBytes(value any) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
v := reflect.ValueOf(value)
|
||||||
enc := gob.NewEncoder(&buf)
|
|
||||||
err := enc.Encode(data)
|
switch value.(type) {
|
||||||
if err != nil {
|
case int, int8, int16, int32, int64:
|
||||||
return nil, err
|
number := v.Int()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
|
number := v.Uint()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case float32:
|
||||||
|
number := float32(v.Float())
|
||||||
|
bits := math.Float32bits(number)
|
||||||
|
bytes := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case float64:
|
||||||
|
number := v.Float()
|
||||||
|
bits := math.Float64bits(number)
|
||||||
|
bytes := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case bool:
|
||||||
|
return strconv.AppendBool([]byte{}, v.Bool()), nil
|
||||||
|
case string:
|
||||||
|
return []byte(v.String()), nil
|
||||||
|
case []byte:
|
||||||
|
return v.Bytes(), nil
|
||||||
|
default:
|
||||||
|
newValue, err := json.Marshal(value)
|
||||||
|
return newValue, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToChar convert string to char slice
|
// ToChar convert string to char slice
|
||||||
|
|||||||
@@ -42,14 +42,21 @@ func TestToBytes(t *testing.T) {
|
|||||||
"1",
|
"1",
|
||||||
}
|
}
|
||||||
expected := [][]byte{
|
expected := [][]byte{
|
||||||
{3, 4, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{3, 2, 0, 0},
|
{102, 97, 108, 115, 101},
|
||||||
{4, 12, 0, 1, 49},
|
{49},
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual, _ := ToBytes(cases[i])
|
actual, _ := ToBytes(cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bytesData, err := ToBytes("abc")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
assert.Equal("abc", ToString(bytesData))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToInt(t *testing.T) {
|
func TestToInt(t *testing.T) {
|
||||||
|
|||||||
@@ -44,14 +44,11 @@ func TestDoublyLink_InsertAt(t *testing.T) {
|
|||||||
err := link.InsertAt(1, 1)
|
err := link.InsertAt(1, 1)
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
err = link.InsertAt(0, 1)
|
link.InsertAt(0, 1)
|
||||||
err = link.InsertAt(1, 2)
|
link.InsertAt(1, 2)
|
||||||
err = link.InsertAt(2, 4)
|
link.InsertAt(2, 4)
|
||||||
err = link.InsertAt(2, 3)
|
link.InsertAt(2, 3)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
link.Print()
|
link.Print()
|
||||||
|
|
||||||
expected := []int{1, 2, 3, 4}
|
expected := []int{1, 2, 3, 4}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package datastructure
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/datastructure"
|
"github.com/duke-git/lancet/v2/datastructure"
|
||||||
)
|
)
|
||||||
@@ -144,6 +145,27 @@ func (link *SinglyLink[T]) DeleteAt(index int) error {
|
|||||||
return errors.New("delete error")
|
return errors.New("delete error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteValue delete value in singly linklist
|
||||||
|
func (link *SinglyLink[T]) DeleteValue(value T) {
|
||||||
|
if link.Head == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dummyHead := datastructure.NewLinkNode(value)
|
||||||
|
dummyHead.Next = link.Head
|
||||||
|
current := dummyHead
|
||||||
|
|
||||||
|
for current.Next != nil {
|
||||||
|
if reflect.DeepEqual(current.Next.Value, value) {
|
||||||
|
current.Next = current.Next.Next
|
||||||
|
link.length--
|
||||||
|
} else {
|
||||||
|
current = current.Next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
link.Head = dummyHead.Next
|
||||||
|
}
|
||||||
|
|
||||||
// Reverse the linked list
|
// Reverse the linked list
|
||||||
func (link *SinglyLink[T]) Reverse() {
|
func (link *SinglyLink[T]) Reverse() {
|
||||||
var pre, next *datastructure.LinkNode[T]
|
var pre, next *datastructure.LinkNode[T]
|
||||||
|
|||||||
@@ -45,20 +45,28 @@ func TestSinglyLink_InsertAt(t *testing.T) {
|
|||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
err = link.InsertAt(0, 1)
|
err = link.InsertAt(0, 1)
|
||||||
err = link.InsertAt(1, 2)
|
|
||||||
err = link.InsertAt(2, 4)
|
|
||||||
err = link.InsertAt(2, 3)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
|
err = link.InsertAt(1, 2)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
err = link.InsertAt(2, 4)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
err = link.InsertAt(2, 3)
|
||||||
|
if err != nil {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
link.Print()
|
link.Print()
|
||||||
|
|
||||||
expected := []int{1, 2, 3, 4}
|
expected := []int{1, 2, 3, 4}
|
||||||
values := link.Values()
|
values := link.Values()
|
||||||
|
|
||||||
assert.Equal(expected, values)
|
assert.Equal(expected, values)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSinglyLink_DeleteAtHead(t *testing.T) {
|
func TestSinglyLink_DeleteAtHead(t *testing.T) {
|
||||||
@@ -103,6 +111,24 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
|
|||||||
assert.Equal(expected, values)
|
assert.Equal(expected, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSinglyLink_DeleteValue(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteValue")
|
||||||
|
|
||||||
|
link := NewSinglyLink[int]()
|
||||||
|
|
||||||
|
link.InsertAtTail(1)
|
||||||
|
link.InsertAtTail(2)
|
||||||
|
link.InsertAtTail(2)
|
||||||
|
link.InsertAtTail(3)
|
||||||
|
link.InsertAtTail(4)
|
||||||
|
|
||||||
|
link.DeleteValue(2)
|
||||||
|
assert.Equal([]int{1, 3, 4}, link.Values())
|
||||||
|
|
||||||
|
link.DeleteValue(1)
|
||||||
|
assert.Equal([]int{3, 4}, link.Values())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSinglyLink_DeleteAt(t *testing.T) {
|
func TestSinglyLink_DeleteAt(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
||||||
|
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func TestPopFirst(t *testing.T) {
|
|||||||
assert.Equal([]int{2, 3}, list.Data())
|
assert.Equal([]int{2, 3}, list.Data())
|
||||||
|
|
||||||
list2 := NewList([]int{})
|
list2 := NewList([]int{})
|
||||||
v, ok = list2.PopFirst()
|
_, ok = list2.PopFirst()
|
||||||
assert.Equal(false, ok)
|
assert.Equal(false, ok)
|
||||||
assert.Equal([]int{}, list2.Data())
|
assert.Equal([]int{}, list2.Data())
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ func TestPopLast(t *testing.T) {
|
|||||||
assert.Equal([]int{1, 2}, list.Data())
|
assert.Equal([]int{1, 2}, list.Data())
|
||||||
|
|
||||||
list2 := NewList([]int{})
|
list2 := NewList([]int{})
|
||||||
v, ok = list2.PopLast()
|
_, ok = list2.PopLast()
|
||||||
assert.Equal(false, ok)
|
assert.Equal(false, ok)
|
||||||
assert.Equal([]int{}, list2.Data())
|
assert.Equal([]int{}, list2.Data())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package datastructure implements some data structure. eg. list, linklist, stack, queue, tree, graph.
|
||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
// LinkNode is a linkedlist node, which have a Value and Pre points to previous node, Next points to a next node of the link.
|
// LinkNode is a linkedlist node, which have a Value and Pre points to previous node, Next points to a next node of the link.
|
||||||
|
|||||||
@@ -1,79 +1,101 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ArrayQueue implements queue with slice
|
// ArrayQueue implements queue with slice
|
||||||
type ArrayQueue[T any] struct {
|
type ArrayQueue[T any] struct {
|
||||||
data []T
|
items []T
|
||||||
length int
|
head int
|
||||||
|
tail int
|
||||||
|
capacity int
|
||||||
|
size int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewArrayQueue return a empty ArrayQueue pointer
|
func NewArrayQueue[T any](capacity int) *ArrayQueue[T] {
|
||||||
func NewArrayQueue[T any](values ...T) *ArrayQueue[T] {
|
return &ArrayQueue[T]{
|
||||||
data := make([]T, len(values))
|
items: make([]T, 0, capacity),
|
||||||
for i, v := range values {
|
head: 0,
|
||||||
data[i] = v
|
tail: 0,
|
||||||
|
capacity: capacity,
|
||||||
|
size: 0,
|
||||||
}
|
}
|
||||||
return &ArrayQueue[T]{data: data, length: len(values)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data return queue data
|
// Data return queue data
|
||||||
func (q *ArrayQueue[T]) Data() []T {
|
func (q *ArrayQueue[T]) Data() []T {
|
||||||
return q.data
|
items := []T{}
|
||||||
|
for i := q.head; i < q.tail; i++ {
|
||||||
|
items = append(items, q.items[i])
|
||||||
|
}
|
||||||
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size return length of queue data
|
// Size return length of queue data
|
||||||
func (q *ArrayQueue[T]) Size() int {
|
func (q *ArrayQueue[T]) Size() int {
|
||||||
return q.length
|
return q.size
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsEmpty checks if queue is empty or not
|
// IsEmpty checks if queue is empty or not
|
||||||
func (q *ArrayQueue[T]) IsEmpty() bool {
|
func (q *ArrayQueue[T]) IsEmpty() bool {
|
||||||
return q.length == 0
|
return q.size == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Front return front value of queue
|
// Front return front value of queue
|
||||||
func (q *ArrayQueue[T]) Front() T {
|
func (q *ArrayQueue[T]) Front() T {
|
||||||
return q.data[0]
|
return q.items[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Back return back value of queue
|
// Back return back value of queue
|
||||||
func (q *ArrayQueue[T]) Back() T {
|
func (q *ArrayQueue[T]) Back() T {
|
||||||
return q.data[q.length-1]
|
return q.items[q.size-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
// EnQueue put element into queue
|
// EnQueue put element into queue
|
||||||
func (q *ArrayQueue[T]) EnQueue(value T) {
|
func (q *ArrayQueue[T]) Enqueue(item T) bool {
|
||||||
q.data = append(q.data, value)
|
if q.head == 0 && q.tail == q.capacity {
|
||||||
q.length++
|
return false
|
||||||
|
} else if q.head != 0 && q.tail == q.capacity {
|
||||||
|
for i := q.head; i < q.tail; i++ {
|
||||||
|
q.items[i-q.head] = q.items[i]
|
||||||
|
}
|
||||||
|
q.tail = q.tail - q.head
|
||||||
|
q.head = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
q.items = append(q.items, item)
|
||||||
|
q.tail++
|
||||||
|
q.size++
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error
|
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error
|
||||||
func (q *ArrayQueue[T]) DeQueue() (*T, error) {
|
func (q *ArrayQueue[T]) Dequeue() (T, bool) {
|
||||||
if q.IsEmpty() {
|
var item T
|
||||||
return nil, errors.New("queue is empty")
|
if q.head == q.tail {
|
||||||
|
return item, false
|
||||||
}
|
}
|
||||||
|
item = q.items[q.head]
|
||||||
headItem := q.data[0]
|
q.head++
|
||||||
q.data = q.data[1:]
|
q.size--
|
||||||
q.length--
|
return item, true
|
||||||
|
|
||||||
return &headItem, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the queue data
|
// Clear the queue data
|
||||||
func (q *ArrayQueue[T]) Clear() {
|
func (q *ArrayQueue[T]) Clear() {
|
||||||
q.data = []T{}
|
capacity := q.capacity
|
||||||
q.length = 0
|
q.items = make([]T, 0, capacity)
|
||||||
|
q.head = 0
|
||||||
|
q.tail = 0
|
||||||
|
q.size = 0
|
||||||
|
q.capacity = capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contain checks if the value is in queue or not
|
// Contain checks if the value is in queue or not
|
||||||
func (q *ArrayQueue[T]) Contain(value T) bool {
|
func (q *ArrayQueue[T]) Contain(value T) bool {
|
||||||
for _, v := range q.data {
|
for _, v := range q.items {
|
||||||
if reflect.DeepEqual(v, value) {
|
if reflect.DeepEqual(v, value) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -84,8 +106,8 @@ func (q *ArrayQueue[T]) Contain(value T) bool {
|
|||||||
// Print queue data
|
// Print queue data
|
||||||
func (q *ArrayQueue[T]) Print() {
|
func (q *ArrayQueue[T]) Print() {
|
||||||
info := "["
|
info := "["
|
||||||
for _, v := range q.data {
|
for i := q.head; i < q.tail; i++ {
|
||||||
info += fmt.Sprintf("%+v, ", v)
|
info += fmt.Sprintf("%+v, ", q.items[i])
|
||||||
}
|
}
|
||||||
info += "]"
|
info += "]"
|
||||||
fmt.Println(info)
|
fmt.Println(info)
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestArrayQueue_EnQueue(t *testing.T) {
|
func TestArrayQueue_Enqueue(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_EnQueue")
|
assert := internal.NewAssert(t, "TestArrayQueue_Enqueue")
|
||||||
|
|
||||||
queue := NewArrayQueue[int]()
|
queue := NewArrayQueue[int](5)
|
||||||
queue.EnQueue(1)
|
queue.Enqueue(1)
|
||||||
queue.EnQueue(2)
|
queue.Enqueue(2)
|
||||||
queue.EnQueue(3)
|
queue.Enqueue(3)
|
||||||
|
|
||||||
expected := []int{1, 2, 3}
|
expected := []int{1, 2, 3}
|
||||||
data := queue.Data()
|
data := queue.Data()
|
||||||
@@ -24,26 +24,30 @@ func TestArrayQueue_EnQueue(t *testing.T) {
|
|||||||
assert.Equal(3, size)
|
assert.Equal(3, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayQueue_DeQueue(t *testing.T) {
|
func TestArrayQueue_Dequeue(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_DeQueue")
|
assert := internal.NewAssert(t, "TestArrayQueue_Dequeue")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
val, err := queue.DeQueue()
|
val, ok := queue.Dequeue()
|
||||||
if err != nil {
|
assert.Equal(true, ok)
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
|
|
||||||
queue.Print()
|
queue.Print()
|
||||||
assert.Equal(1, *val)
|
assert.Equal(1, val)
|
||||||
|
|
||||||
assert.Equal([]int{2, 3}, queue.Data())
|
assert.Equal([]int{2, 3}, queue.Data())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestArrayQueue_Front(t *testing.T) {
|
func TestArrayQueue_Front(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Front")
|
assert := internal.NewAssert(t, "TestArrayQueue_Front")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
val := queue.Front()
|
val := queue.Front()
|
||||||
|
|
||||||
queue.Print()
|
queue.Print()
|
||||||
@@ -55,7 +59,11 @@ func TestArrayQueue_Front(t *testing.T) {
|
|||||||
func TestArrayQueue_Back(t *testing.T) {
|
func TestArrayQueue_Back(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Back")
|
assert := internal.NewAssert(t, "TestArrayQueue_Back")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
val := queue.Back()
|
val := queue.Back()
|
||||||
|
|
||||||
queue.Print()
|
queue.Print()
|
||||||
@@ -67,7 +75,10 @@ func TestArrayQueue_Back(t *testing.T) {
|
|||||||
func TestArrayQueue_Contain(t *testing.T) {
|
func TestArrayQueue_Contain(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Contain")
|
assert := internal.NewAssert(t, "TestArrayQueue_Contain")
|
||||||
|
|
||||||
queue := NewArrayQueue(1, 2, 3)
|
queue := NewArrayQueue[int](4)
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
assert.Equal(true, queue.Contain(1))
|
assert.Equal(true, queue.Contain(1))
|
||||||
assert.Equal(false, queue.Contain(4))
|
assert.Equal(false, queue.Contain(4))
|
||||||
@@ -76,11 +87,12 @@ func TestArrayQueue_Contain(t *testing.T) {
|
|||||||
func TestArrayQueue_Clear(t *testing.T) {
|
func TestArrayQueue_Clear(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestArrayQueue_Clear")
|
assert := internal.NewAssert(t, "TestArrayQueue_Clear")
|
||||||
|
|
||||||
queue := NewArrayQueue[int]()
|
queue := NewArrayQueue[int](4)
|
||||||
|
|
||||||
assert.Equal(true, queue.IsEmpty())
|
assert.Equal(true, queue.IsEmpty())
|
||||||
assert.Equal(0, queue.Size())
|
assert.Equal(0, queue.Size())
|
||||||
|
|
||||||
queue.EnQueue(1)
|
queue.Enqueue(1)
|
||||||
assert.Equal(false, queue.IsEmpty())
|
assert.Equal(false, queue.IsEmpty())
|
||||||
assert.Equal(1, queue.Size())
|
assert.Equal(1, queue.Size())
|
||||||
|
|
||||||
|
|||||||
@@ -40,14 +40,14 @@ func TestLinkedQueue_Front(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedQueue_Front")
|
assert := internal.NewAssert(t, "TestLinkedQueue_Front")
|
||||||
|
|
||||||
queue := NewLinkedQueue[int]()
|
queue := NewLinkedQueue[int]()
|
||||||
val, err := queue.Front()
|
_, err := queue.Front()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
queue.EnQueue(1)
|
queue.EnQueue(1)
|
||||||
queue.EnQueue(2)
|
queue.EnQueue(2)
|
||||||
queue.EnQueue(3)
|
queue.EnQueue(3)
|
||||||
|
|
||||||
val, err = queue.Front()
|
val, err := queue.Front()
|
||||||
assert.Equal(1, *val)
|
assert.Equal(1, *val)
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
}
|
}
|
||||||
@@ -56,14 +56,14 @@ func TestLinkedQueue_Back(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedQueue_Back")
|
assert := internal.NewAssert(t, "TestLinkedQueue_Back")
|
||||||
|
|
||||||
queue := NewLinkedQueue[int]()
|
queue := NewLinkedQueue[int]()
|
||||||
val, err := queue.Back()
|
_, err := queue.Back()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
queue.EnQueue(1)
|
queue.EnQueue(1)
|
||||||
queue.EnQueue(2)
|
queue.EnQueue(2)
|
||||||
queue.EnQueue(3)
|
queue.EnQueue(3)
|
||||||
|
|
||||||
val, err = queue.Back()
|
val, err := queue.Back()
|
||||||
assert.Equal(3, *val)
|
assert.Equal(3, *val)
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,3 +103,34 @@ func (s Set[T]) Intersection(other Set[T]) Set[T] {
|
|||||||
|
|
||||||
return set
|
return set
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SymmetricDifference creates a new set whose element is in set1 or set2, but not in both sets
|
||||||
|
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
|
||||||
|
set := NewSet[T]()
|
||||||
|
s.Iterate(func(value T) {
|
||||||
|
if !other.Contain(value) {
|
||||||
|
set.Add(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
other.Iterate(func(value T) {
|
||||||
|
if !s.Contain(value) {
|
||||||
|
set.Add(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minus creates an set of whose element in origin set but not in compared set
|
||||||
|
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
|
||||||
|
set := NewSet[T]()
|
||||||
|
|
||||||
|
s.Iterate(func(value T) {
|
||||||
|
if !comparedSet.Contain(value) {
|
||||||
|
set.Add(value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return set
|
||||||
|
}
|
||||||
|
|||||||
@@ -127,3 +127,23 @@ func TestSet_Intersection(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(expected, intersectionSet)
|
assert.Equal(expected, intersectionSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSet_SymmetricDifference(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_SymmetricDifference")
|
||||||
|
|
||||||
|
set1 := NewSet(1, 2, 3)
|
||||||
|
set2 := NewSet(2, 3, 4, 5)
|
||||||
|
|
||||||
|
assert.Equal(NewSet(1, 4, 5), set1.SymmetricDifference(set2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSet_Minus(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSet_Minus")
|
||||||
|
|
||||||
|
set1 := NewSet(1, 2, 3)
|
||||||
|
set2 := NewSet(2, 3, 4, 5)
|
||||||
|
set3 := NewSet(2, 3)
|
||||||
|
|
||||||
|
assert.Equal(NewSet(1), set1.Minus(set2))
|
||||||
|
assert.Equal(NewSet(4, 5), set2.Minus(set3))
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,14 +26,14 @@ func TestArrayStack_Pop(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestArrayStack_Pop")
|
assert := internal.NewAssert(t, "TestArrayStack_Pop")
|
||||||
|
|
||||||
stack := NewArrayStack[int]()
|
stack := NewArrayStack[int]()
|
||||||
topItem, err := stack.Pop()
|
_, err := stack.Pop()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Pop()
|
topItem, err := stack.Pop()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
@@ -45,14 +45,14 @@ func TestArrayStack_Peak(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestArrayStack_Peak")
|
assert := internal.NewAssert(t, "TestArrayStack_Peak")
|
||||||
|
|
||||||
stack := NewArrayStack[int]()
|
stack := NewArrayStack[int]()
|
||||||
topItem, err := stack.Peak()
|
_, err := stack.Peak()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Peak()
|
topItem, err := stack.Peak()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
|
|||||||
@@ -28,14 +28,14 @@ func TestLinkedStack_Pop(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedStack_Pop")
|
assert := internal.NewAssert(t, "TestLinkedStack_Pop")
|
||||||
|
|
||||||
stack := NewLinkedStack[int]()
|
stack := NewLinkedStack[int]()
|
||||||
topItem, err := stack.Pop()
|
_, err := stack.Pop()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Pop()
|
topItem, err := stack.Pop()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
@@ -48,14 +48,14 @@ func TestLinkedStack_Peak(t *testing.T) {
|
|||||||
assert := internal.NewAssert(t, "TestLinkedStack_Peak")
|
assert := internal.NewAssert(t, "TestLinkedStack_Peak")
|
||||||
|
|
||||||
stack := NewLinkedStack[int]()
|
stack := NewLinkedStack[int]()
|
||||||
topItem, err := stack.Peak()
|
_, err := stack.Peak()
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
stack.Push(1)
|
stack.Push(1)
|
||||||
stack.Push(2)
|
stack.Push(2)
|
||||||
stack.Push(3)
|
stack.Push(3)
|
||||||
|
|
||||||
topItem, err = stack.Peak()
|
topItem, err := stack.Peak()
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
assert.Equal(3, *topItem)
|
assert.Equal(3, *topItem)
|
||||||
|
|
||||||
|
|||||||
60
datetime/conversion.go
Normal file
60
datetime/conversion.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license.
|
||||||
|
|
||||||
|
package datetime
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnixNow return unix timestamp of current time
|
||||||
|
func NewUnixNow() *theTime {
|
||||||
|
return &theTime{unix: time.Now().Unix()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnix return unix timestamp of specified time
|
||||||
|
func NewUnix(unix int64) *theTime {
|
||||||
|
return &theTime{unix: unix}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss"
|
||||||
|
func NewFormat(t string) (*theTime, error) {
|
||||||
|
timeLayout := "2006-01-02 15:04:05"
|
||||||
|
loc := time.FixedZone("CST", 8*3600)
|
||||||
|
tt, err := time.ParseInLocation(timeLayout, t, loc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &theTime{unix: tt.Unix()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewISO8601 return unix timestamp of specified iso8601 time string
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error) {
|
||||||
|
t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &theTime{unix: t.Unix()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToUnix return unix timestamp
|
||||||
|
func (t *theTime) ToUnix() int64 {
|
||||||
|
return t.unix
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time
|
||||||
|
func (t *theTime) ToFormat() string {
|
||||||
|
return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormatForTpl return the time string which format is specified tpl
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string {
|
||||||
|
return time.Unix(t.unix, 0).Format(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormatForTpl return iso8601 time string
|
||||||
|
func (t *theTime) ToIso8601() string {
|
||||||
|
return time.Unix(t.unix, 0).Format(time.RFC3339)
|
||||||
|
}
|
||||||
53
datetime/conversion_test.go
Normal file
53
datetime/conversion_test.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package datetime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToUnix(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToUnix")
|
||||||
|
|
||||||
|
tm1 := NewUnixNow()
|
||||||
|
unixTimestamp := tm1.ToUnix()
|
||||||
|
tm2 := NewUnix(unixTimestamp)
|
||||||
|
|
||||||
|
assert.Equal(tm1, tm2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFormat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToFormat")
|
||||||
|
|
||||||
|
_, err := NewFormat("2022/03/18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToFormat -> ", tm.ToFormat())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFormatForTpl(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToFormatForTpl")
|
||||||
|
|
||||||
|
_, err := NewFormat("2022/03/18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToFormatForTpl -> ", tm.ToFormatForTpl("2006/01/02 15:04:05"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToIso8601(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToIso8601")
|
||||||
|
|
||||||
|
_, err := NewISO8601("2022-03-18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err := NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToIso8601 -> ", tm.ToIso8601())
|
||||||
|
}
|
||||||
@@ -21,18 +21,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
- [BubbleSort](#BubbleSort)
|
- [Algorithm](#algorithm)
|
||||||
- [CountSort](#CountSort)
|
- [Source](#source)
|
||||||
- [HeapSort](#HeapSort)
|
- [Usage](#usage)
|
||||||
- [InsertionSort](#InsertionSort)
|
- [Index](#index)
|
||||||
- [MergeSort](#MergeSort)
|
- [Documentation](#documentation)
|
||||||
- [QuickSort](#QuickSort)
|
- [<span id="BubbleSort">BubbleSort</span>](#bubblesort)
|
||||||
- [SelectionSort](#SelectionSort)
|
- [<span id="InsertionSort">InsertionSort</span>](#insertionsort)
|
||||||
- [ShellSort](#ShellSort)
|
- [<span id="SelectionSort">SelectionSort</span>](#selectionsort)
|
||||||
- [BinarySearch](#BinarySearch)
|
- [<span id="ShellSort">ShellSort</span>](#shellsort)
|
||||||
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
- [<span id="QuickSort">QuickSort</span>](#quicksort)
|
||||||
- [LinearSearch](#LinearSearch)
|
- [<span id="HeapSort">HeapSort</span>](#heapsort)
|
||||||
- [LRUCache](#LRUCache)
|
- [<span id="MergeSort">MergeSort</span>](#mergesort)
|
||||||
|
- [<span id="CountSort">CountSort</span>](#countsort)
|
||||||
|
- [<span id="BinarySearch">BinarySearch</span>](#binarysearch)
|
||||||
|
- [<span id="BinaryIterativeSearch">BinaryIterativeSearch</span>](#binaryiterativesearch)
|
||||||
|
- [<span id="LinearSearch">LinearSearch</span>](#linearsearch)
|
||||||
|
- [<span id="LRUCache">LRUCache</span>](#lrucache)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -46,7 +51,7 @@ import (
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -76,9 +81,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.BubbleSort(intSlice, comparator)
|
algorithm.BubbleSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -91,7 +96,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -141,9 +146,9 @@ func main() {
|
|||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeople := algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -156,7 +161,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -186,9 +191,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.SelectionSort(intSlice, comparator)
|
algorithm.SelectionSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -201,7 +206,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -231,9 +236,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.ShellSort(intSlice, comparator)
|
algorithm.ShellSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -246,7 +251,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -276,9 +281,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -291,7 +296,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -321,9 +326,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.HeapSort(intSlice, comparator)
|
algorithm.HeapSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -336,7 +341,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -366,9 +371,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.MergeSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -21,18 +21,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [BubbleSort](#BubbleSort)
|
- [Algorithm](#algorithm)
|
||||||
- [CountSort](#CountSort)
|
- [源码](#源码)
|
||||||
- [HeapSort](#HeapSort)
|
- [用法](#用法)
|
||||||
- [InsertionSort](#InsertionSort)
|
- [目录](#目录)
|
||||||
- [MergeSort](#MergeSort)
|
- [文档](#文档)
|
||||||
- [QuickSort](#QuickSort)
|
- [<span id="BubbleSort">BubbleSort</span>](#bubblesort)
|
||||||
- [SelectionSort](#SelectionSort)
|
- [<span id="InsertionSort">InsertionSort</span>](#insertionsort)
|
||||||
- [ShellSort](#ShellSort)
|
- [<span id="SelectionSort">SelectionSort</span>](#selectionsort)
|
||||||
- [BinarySearch](#BinarySearch)
|
- [<span id="ShellSort">ShellSort</span>](#shellsort)
|
||||||
- [BinaryIterativeSearch](#BinaryIterativeSearch)
|
- [<span id="QuickSort">QuickSort</span>](#quicksort)
|
||||||
- [LinearSearch](#LinearSearch)
|
- [<span id="HeapSort">HeapSort</span>](#heapsort)
|
||||||
- [LRUCache](#LRUCache)
|
- [<span id="MergeSort">MergeSort</span>](#mergesort)
|
||||||
|
- [<span id="CountSort">CountSort</span>](#countsort)
|
||||||
|
- [<span id="BinarySearch">BinarySearch</span>](#binarysearch)
|
||||||
|
- [<span id="BinaryIterativeSearch">BinaryIterativeSearch</span>](#binaryiterativesearch)
|
||||||
|
- [<span id="LinearSearch">LinearSearch</span>](#linearsearch)
|
||||||
|
- [<span id="LRUCache">LRUCache</span>](#lrucache)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -46,7 +51,7 @@ import (
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -76,9 +81,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.BubbleSort(intSlice, comparator)
|
algorithm.BubbleSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -91,7 +96,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -141,9 +146,9 @@ func main() {
|
|||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeople := algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -156,7 +161,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -186,9 +191,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.SelectionSort(intSlice, comparator)
|
algorithm.SelectionSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -201,7 +206,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -231,9 +236,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.ShellSort(intSlice, comparator)
|
algorithm.ShellSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -276,9 +281,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -321,9 +326,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.HeapSort(intSlice, comparator)
|
algorithm.HeapSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -336,7 +341,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -366,9 +371,9 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.MergeSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
232
docs/datetime.md
232
docs/datetime.md
@@ -6,6 +6,7 @@ Package datetime supports date and time format and compare.
|
|||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
|
- [https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/datetime/conversion.go](https://github.com/duke-git/lancet/blob/main/datetime/conversion.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -28,7 +29,6 @@ import (
|
|||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
@@ -41,9 +41,19 @@ import (
|
|||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
|
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
|
- [NewUnixNow](#NewUnixNow)
|
||||||
|
- [NewUnix](#NewUnix)
|
||||||
|
- [NewFormat](#NewFormat)
|
||||||
|
- [NewISO8601](#NewISO8601)
|
||||||
|
- [ToUnix](#ToUnix)
|
||||||
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
@@ -667,3 +677,221 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
<p>Return unix timestamp of current time</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnixNow() *theTime
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
<p>Return unix timestamp of specified int64 value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnix(unix int64) *theTime
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnix(1647597438)
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewFormat(t string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm) //&{1647594245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
<p>Return unix timestamp of specified iso8601 time string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
fmt.Println(tm) //&{1136214245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
<p>Return unix timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToUnix() int64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm.ToUnix()) //1647597438
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormat() string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
<p>Return the time string which format is specified tpl.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
|
||||||
|
fmt.Println(ts) //"2022/03/18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
<p>Return iso8601 time string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToIso8601() string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
ts := tm.ToIso8601()
|
||||||
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -28,7 +28,6 @@ import (
|
|||||||
- [BeginOfWeek](#BeginOfWeek)
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
- [BeginOfMonth](#BeginOfMonth)
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
- [BeginOfYear](#BeginOfYear)
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
- [EndOfMinute](#EndOfMinute)
|
- [EndOfMinute](#EndOfMinute)
|
||||||
- [EndOfHour](#EndOfHour)
|
- [EndOfHour](#EndOfHour)
|
||||||
- [EndOfDay](#EndOfDay)
|
- [EndOfDay](#EndOfDay)
|
||||||
@@ -43,6 +42,15 @@ import (
|
|||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
|
||||||
|
- [NewUnixNow](#NewUnixNow)
|
||||||
|
- [NewUnix](#NewUnix)
|
||||||
|
- [NewFormat](#NewFormat)
|
||||||
|
- [NewISO8601](#NewISO8601)
|
||||||
|
- [ToUnix](#ToUnix)
|
||||||
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
@@ -665,4 +673,221 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
<p>创建一个当前时间的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnixNow() *theTime
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
<p>创建一个unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnix(unix int64) *theTime
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnix(1647597438)
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewFormat(t string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm) //&{1647594245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
fmt.Println(tm) //&{1136214245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
<p>返回unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToUnix() int64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm.ToUnix()) //1647597438
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormat() string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
<p>返回tpl格式指定的日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
|
||||||
|
fmt.Println(ts) //"2022/03/18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
<p>返回iso8601日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToIso8601() string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
ts := tm.ToIso8601()
|
||||||
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
304
docs/maputil.md
Normal file
304
docs/maputil.md
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
# Maputil
|
||||||
|
Package maputil includes some functions to manipulate map.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Source:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/maputil/maputil.go](https://github.com/duke-git/lancet/blob/main/maputil/maputil.go)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Example:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
- [ForEach](#ForEach)
|
||||||
|
- [Filter](#Filter)
|
||||||
|
- [Intersect](#Intersect)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Merge](#Merge)
|
||||||
|
- [Minus](#Minus)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ForEach">ForEach</span>
|
||||||
|
<p>Executes iteratee funcation for every key and value pair in map.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
maputil.ForEach(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
fmt.Println(sum) // 10
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Filter">Filter</span>
|
||||||
|
<p>Iterates over map, return a new map contains all key and value pairs pass the predicate function.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
maputil.Filter(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
res := maputil.Filter(m, isEven)
|
||||||
|
fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Intersect">Intersect</span>
|
||||||
|
<p>Iterates over maps, return a new map of key and value pairs in all given maps.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 6,
|
||||||
|
"d": 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 9,
|
||||||
|
"e": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
<p>Returns a slice of the map's keys.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Keys[K comparable, V any](m map[K]V) []K
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := maputil.Keys(m)
|
||||||
|
sort.Ints(keys)
|
||||||
|
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Merge">Merge</span>
|
||||||
|
<p>Merge maps, next key will overwrite previous key.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Minus">Minus</span>
|
||||||
|
<p>Creates an map of whose key in mapA but not in mapB.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 11,
|
||||||
|
"b": 22,
|
||||||
|
"d": 33,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
<p>Returns a slice of the map's values.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Values[K comparable, V any](m map[K]V) []V
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := maputil.Values(m)
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
||||||
|
}
|
||||||
|
```
|
||||||
304
docs/maputil_zh-CN.md
Normal file
304
docs/maputil_zh-CN.md
Normal file
@@ -0,0 +1,304 @@
|
|||||||
|
# Maputil
|
||||||
|
maputil包包括一些操作map的函数。
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码:
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/maputil/maputil.go](https://github.com/duke-git/lancet/blob/main/maputil/maputil.go)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录:
|
||||||
|
- [ForEach](#ForEach)
|
||||||
|
- [Filter](#Filter)
|
||||||
|
- [Intersect](#Intersect)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Merge](#Merge)
|
||||||
|
- [Minus](#Minus)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## API文档:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ForEach">ForEach</span>
|
||||||
|
<p>对map中的每对key和value执行iteratee函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
maputil.ForEach(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
fmt.Println(sum) // 10
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Filter">Filter</span>
|
||||||
|
<p>迭代map中的每对key和value, 返回符合predicate函数的key, value</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
maputil.Filter(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
res := maputil.Filter(m, isEven)
|
||||||
|
fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Intersect">Intersect</span>
|
||||||
|
<p>多个map的交集操作</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 6,
|
||||||
|
"d": 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 9,
|
||||||
|
"e": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
<p>返回map中所有key的切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Keys[K comparable, V any](m map[K]V) []K
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := maputil.Keys(m)
|
||||||
|
sort.Ints(keys)
|
||||||
|
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Merge">Merge</span>
|
||||||
|
<p>合并多个maps, 相同的key会被后来的key覆盖</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Minus">Minus</span>
|
||||||
|
<p>返回一个map,其中的key存在于mapA,不存在于mapB.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 11,
|
||||||
|
"b": 22,
|
||||||
|
"d": 33,
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
<p>返回map中所有value的切片</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Values[K comparable, V any](m map[K]V) []V
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/maputil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := maputil.Values(m)
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -20,9 +20,13 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
- [Average](#Average)
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
|
- [Max](#Max)
|
||||||
|
- [Min](#Min)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
- [RoundToString](#RoundToString)
|
- [RoundToString](#RoundToString)
|
||||||
@@ -33,6 +37,35 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Average">Average</span>
|
||||||
|
<p>Return average value of numbers. Maybe call RoundToFloat to round result.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Average[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Average(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Average(1, 1)) //1
|
||||||
|
avg := mathutil.Average(1.2, 1.4) //1.2999999998
|
||||||
|
roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
<p>Calculate x to the nth power.</p>
|
<p>Calculate x to the nth power.</p>
|
||||||
|
|
||||||
@@ -117,6 +150,60 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Max">Max</span>
|
||||||
|
<p>Return max value of numbers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Max[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Max(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Max(1, 2, 3)) //3
|
||||||
|
fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Min">Min</span>
|
||||||
|
<p>Return min value of numbers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Min[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Min(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Min(1, 2, 3)) //1
|
||||||
|
fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,12 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
- [Average](#Average)
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
|
- [Max](#Max)
|
||||||
|
- [Min](#Min)
|
||||||
|
|
||||||
- [Percent](#Percent)
|
- [Percent](#Percent)
|
||||||
- [RoundToFloat](#RoundToFloat)
|
- [RoundToFloat](#RoundToFloat)
|
||||||
@@ -34,6 +37,33 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Average">Average</span>
|
||||||
|
<p>计算平均数. 可能需要对结果调用RoundToFloat方法四舍五入</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Average[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Average(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Average(1, 1)) //1
|
||||||
|
avg := mathutil.Average(1.2, 1.4) //1.2999999998
|
||||||
|
roundAvg := mmathutil.RoundToFloat(avg, 1) // 1.3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
<p>指数计算(x的n次方)</p>
|
<p>指数计算(x的n次方)</p>
|
||||||
|
|
||||||
@@ -117,6 +147,59 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Max">Max</span>
|
||||||
|
<p>返回参数中的最大数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Max[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Max(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Max(1, 2, 3)) //3
|
||||||
|
fmt.Println(mathutil.Max(1.2, 1.4, 1.1, 1.4)) //1.4
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Min">Min</span>
|
||||||
|
<p>返回参数中的最小数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Min[T lancetconstraints.Number](numbers ...T) T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Min(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.Min(1, 2, 3)) //1
|
||||||
|
fmt.Println(mathutil.Min(1.2, 1.4, 1.1, 1.4)) //1.1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
<p>计算百分比,保留n位小数</p>
|
<p>计算百分比,保留n位小数</p>
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import (
|
|||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
|
- [GroupWith](#GroupWith)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -50,6 +51,7 @@ import (
|
|||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
@@ -564,6 +566,34 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GroupWith">GroupWith</span>
|
||||||
|
<p>Return a map composed of keys generated from the results of running each element of slice thru iteratee.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
res := slice.GroupWith(nums, floor)
|
||||||
|
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
<p>Convert interface slice to int slice.</p>
|
<p>Convert interface slice to int slice.</p>
|
||||||
|
|
||||||
@@ -880,6 +910,35 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SymmetricDifference">SymmetricDifference</span>
|
||||||
|
<p>Create a slice whose element is in given slices, but not in both slices.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SymmetricDifference[T any](slices ...[]T) []T
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := []int{1, 2, 3}
|
||||||
|
s2 := []int{1, 2, 4}
|
||||||
|
s3 := []int{1, 2, 3, 5}
|
||||||
|
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1)) //[]int{1, 2, 3}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2)) //[]int{3, 4}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2, s3)) //[]int{3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
<p>Remove duplicate elements in slice.</p>
|
<p>Remove duplicate elements in slice.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import (
|
|||||||
- [ForEach](#ForEach)
|
- [ForEach](#ForEach)
|
||||||
|
|
||||||
- [GroupBy](#GroupBy)
|
- [GroupBy](#GroupBy)
|
||||||
|
- [GroupWith](#GroupWith)
|
||||||
- [IntSlice](#IntSlice)
|
- [IntSlice](#IntSlice)
|
||||||
- [InterfaceSlice](#InterfaceSlice)
|
- [InterfaceSlice](#InterfaceSlice)
|
||||||
- [Intersection](#Intersection)
|
- [Intersection](#Intersection)
|
||||||
@@ -50,6 +51,7 @@ import (
|
|||||||
- [SortByField](#SortByField)
|
- [SortByField](#SortByField)
|
||||||
- [Some](#Some)
|
- [Some](#Some)
|
||||||
- [StringSlice](#StringSlice)
|
- [StringSlice](#StringSlice)
|
||||||
|
- [SymmetricDifference](#SymmetricDifference)
|
||||||
- [Unique](#Unique)
|
- [Unique](#Unique)
|
||||||
- [Union](#Union)
|
- [Union](#Union)
|
||||||
- [UpdateAt](#UpdateAt)
|
- [UpdateAt](#UpdateAt)
|
||||||
@@ -565,6 +567,32 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GroupWith">GroupWith</span>
|
||||||
|
<p>创建一个map,key是iteratee遍历slice中的每个元素返回的结果。 分组值的顺序是由他们出现在slice中的顺序确定的。每个键对应的值负责生成key的元素组成的数组。iteratee调用1个参数: (value)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
res := slice.GroupWith(nums, floor)
|
||||||
|
fmt.Println(res) //map[float64][]float64{ 4: {4.2}, 6: {6.1, 6.3},}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
<p>将接口切片转换为int切片</p>
|
<p>将接口切片转换为int切片</p>
|
||||||
@@ -882,6 +910,34 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SymmetricDifference">SymmetricDifference</span>
|
||||||
|
<p>返回一个切片,其中的元素存在于参数切片中,但不同时存储在于参数切片中(交集取反)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SymmetricDifference[T any](slices ...[]T) []T
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
s1 := []int{1, 2, 3}
|
||||||
|
s2 := []int{1, 2, 4}
|
||||||
|
s3 := []int{1, 2, 3, 5}
|
||||||
|
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1)) //[]int{1, 2, 3}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2)) //[]int{3, 4}
|
||||||
|
fmt.Println(slice.SymmetricDifference(s1, s2, s3)) //[]int{3, 4, 5}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
<p>删除切片中的重复元素</p>
|
<p>删除切片中的重复元素</p>
|
||||||
|
|
||||||
|
|||||||
@@ -76,10 +76,10 @@ func TestCopyFile(t *testing.T) {
|
|||||||
func TestListFileNames(t *testing.T) {
|
func TestListFileNames(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestListFileNames")
|
assert := internal.NewAssert(t, "TestListFileNames")
|
||||||
|
|
||||||
filesInPath, err := ListFileNames("../datetime/")
|
filesInPath, err := ListFileNames("./")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
expected := []string{"datetime.go", "datetime_test.go"}
|
expected := []string{"file.go", "file_test.go"}
|
||||||
assert.Equal(expected, filesInPath)
|
assert.Equal(expected, filesInPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,3 +11,8 @@ type Comparator interface {
|
|||||||
// Descending order: should return 1 -> v1 < v2, 0 -> v1 = v2, -1 -> v1 > v2
|
// Descending order: should return 1 -> v1 < v2, 0 -> v1 = v2, -1 -> v1 > v2
|
||||||
Compare(v1, v2 any) int
|
Compare(v1, v2 any) int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Number contains all types of number and uintptr, used for generics constraint
|
||||||
|
type Number interface {
|
||||||
|
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
|
||||||
|
}
|
||||||
|
|||||||
106
maputil/map.go
Normal file
106
maputil/map.go
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package maputil includes some functions to manipulate map.
|
||||||
|
package maputil
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
// Keys returns a slice of the map's keys
|
||||||
|
func Keys[K comparable, V any](m map[K]V) []K {
|
||||||
|
keys := make([]K, 0, len(m))
|
||||||
|
|
||||||
|
for k := range m {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values returns a slice of the map's values
|
||||||
|
func Values[K comparable, V any](m map[K]V) []V {
|
||||||
|
values := make([]V, 0, len(m))
|
||||||
|
|
||||||
|
for _, v := range m {
|
||||||
|
values = append(values, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
|
// Merge maps, next key will overwrite previous key
|
||||||
|
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
res := make(map[K]V, 0)
|
||||||
|
|
||||||
|
for _, m := range maps {
|
||||||
|
for k, v := range m {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// ForEach executes iteratee funcation for every key and value pair in map
|
||||||
|
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) {
|
||||||
|
for k, v := range m {
|
||||||
|
iteratee(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter iterates over map, return a new map contains all key and value pairs pass the predicate function
|
||||||
|
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
|
||||||
|
res := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range m {
|
||||||
|
if predicate(k, v) {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Intersect iterates over maps, return a new map of key and value pairs in all given maps
|
||||||
|
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
|
||||||
|
if len(maps) == 0 {
|
||||||
|
return map[K]V{}
|
||||||
|
}
|
||||||
|
if len(maps) == 1 {
|
||||||
|
return maps[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
var res map[K]V
|
||||||
|
|
||||||
|
reducer := func(m1, m2 map[K]V) map[K]V {
|
||||||
|
m := make(map[K]V)
|
||||||
|
for k, v1 := range m1 {
|
||||||
|
if v2, ok := m2[k]; ok && reflect.DeepEqual(v1, v2) {
|
||||||
|
m[k] = v1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
reduceMaps := make([]map[K]V, 2, 2)
|
||||||
|
res = reducer(maps[0], maps[1])
|
||||||
|
|
||||||
|
for i := 2; i < len(maps); i++ {
|
||||||
|
reduceMaps[0] = res
|
||||||
|
reduceMaps[1] = maps[i]
|
||||||
|
res = reducer(reduceMaps[0], reduceMaps[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minus creates an map of whose key in mapA but not in mapB
|
||||||
|
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
|
||||||
|
res := make(map[K]V)
|
||||||
|
|
||||||
|
for k, v := range mapA {
|
||||||
|
if _, ok := mapB[k]; !ok {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
150
maputil/map_test.go
Normal file
150
maputil/map_test.go
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package maputil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKeys(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestKeys")
|
||||||
|
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
keys := Keys(m)
|
||||||
|
sort.Ints(keys)
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3, 4, 5}, keys)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestValues")
|
||||||
|
|
||||||
|
m := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "a",
|
||||||
|
3: "b",
|
||||||
|
4: "c",
|
||||||
|
5: "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
values := Values(m)
|
||||||
|
sort.Strings(values)
|
||||||
|
|
||||||
|
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMerge(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMerge")
|
||||||
|
|
||||||
|
m1 := map[int]string{
|
||||||
|
1: "a",
|
||||||
|
2: "b",
|
||||||
|
}
|
||||||
|
m2 := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
|
||||||
|
expected := map[int]string{
|
||||||
|
1: "1",
|
||||||
|
2: "b",
|
||||||
|
3: "2",
|
||||||
|
}
|
||||||
|
acturl := Merge(m1, m2)
|
||||||
|
|
||||||
|
assert.Equal(expected, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestForEach(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestForEach")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
var sum int
|
||||||
|
|
||||||
|
ForEach(m, func(_ string, value int) {
|
||||||
|
sum += value
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(10, sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilter(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFilter")
|
||||||
|
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
"d": 4,
|
||||||
|
"e": 5,
|
||||||
|
}
|
||||||
|
isEven := func(_ string, value int) bool {
|
||||||
|
return value%2 == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
acturl := Filter(m, isEven)
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{
|
||||||
|
"b": 2,
|
||||||
|
"d": 4,
|
||||||
|
}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntersect(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIntersect")
|
||||||
|
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 6,
|
||||||
|
"d": 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
m3 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 9,
|
||||||
|
"e": 9,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{"a": 1, "b": 2, "c": 3}, Intersect(m1))
|
||||||
|
assert.Equal(map[string]int{"a": 1, "b": 2}, Intersect(m1, m2))
|
||||||
|
assert.Equal(map[string]int{"a": 1}, Intersect(m1, m2, m3))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMinus(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMinus")
|
||||||
|
|
||||||
|
m1 := map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
m2 := map[string]int{
|
||||||
|
"a": 11,
|
||||||
|
"b": 22,
|
||||||
|
"d": 33,
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/lancetconstraints"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Exponent calculate x^n
|
// Exponent calculate x^n
|
||||||
@@ -90,3 +92,40 @@ func TruncRound(x float64, n int) float64 {
|
|||||||
res, _ := strconv.ParseFloat(newFloat, 64)
|
res, _ := strconv.ParseFloat(newFloat, 64)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Max return max value of params
|
||||||
|
func Max[T lancetconstraints.Number](numbers ...T) T {
|
||||||
|
max := numbers[0]
|
||||||
|
|
||||||
|
for _, v := range numbers {
|
||||||
|
if max < v {
|
||||||
|
max = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min return min value of params
|
||||||
|
func Min[T lancetconstraints.Number](numbers ...T) T {
|
||||||
|
min := numbers[0]
|
||||||
|
|
||||||
|
for _, v := range numbers {
|
||||||
|
if min > v {
|
||||||
|
min = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
|
||||||
|
// Average return average value of params
|
||||||
|
func Average[T lancetconstraints.Number](numbers ...T) T {
|
||||||
|
var sum T
|
||||||
|
n := T(len(numbers))
|
||||||
|
|
||||||
|
for _, v := range numbers {
|
||||||
|
sum += v
|
||||||
|
}
|
||||||
|
return sum / n
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,3 +70,29 @@ func TestTruncRound(t *testing.T) {
|
|||||||
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
||||||
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAverage(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestAverage")
|
||||||
|
|
||||||
|
assert.Equal(Average(0, 0), 0)
|
||||||
|
assert.Equal(Average(1, 1), 1)
|
||||||
|
avg := Average(1.2, 1.4)
|
||||||
|
t.Log(avg)
|
||||||
|
assert.Equal(1.3, RoundToFloat(avg, 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMax(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMax")
|
||||||
|
|
||||||
|
assert.Equal(Max(0, 0), 0)
|
||||||
|
assert.Equal(Max(1, 2, 3), 3)
|
||||||
|
assert.Equal(Max(1.2, 1.4, 1.1, 1.4), 1.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMin(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMin")
|
||||||
|
|
||||||
|
assert.Equal(Min(0, 0), 0)
|
||||||
|
assert.Equal(Min(1, 2, 3), 1)
|
||||||
|
assert.Equal(Min(1.2, 1.4, 1.1, 1.4), 1.1)
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
||||||
// params[0] is header which type should be http.Header or map[string]string,
|
// params[0] is header which type should be http.Header or map[string]string,
|
||||||
// params[1] is query param which type should be url.Values or map[string]any,
|
// params[1] is query param which type should be url.Values or map[string]any, when content-type header is
|
||||||
|
// multipart/form-data or application/x-www-form-urlencoded, params[1] should be url.Values
|
||||||
// params[2] is post body which type should be []byte.
|
// params[2] is post body which type should be []byte.
|
||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
package netutil
|
package netutil
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -46,6 +47,29 @@ func TestHttpPost(t *testing.T) {
|
|||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHttpPostFormData(t *testing.T) {
|
||||||
|
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||||
|
header := map[string]string{
|
||||||
|
// "Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
"Content-Type": "multipart/form-data",
|
||||||
|
}
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
}
|
||||||
|
postData := url.Values{}
|
||||||
|
postData.Add("userId", "1")
|
||||||
|
postData.Add("title", "TestAddToDo")
|
||||||
|
|
||||||
|
resp, err := HttpPost(apiUrl, header, postData, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHttpPut(t *testing.T) {
|
func TestHttpPut(t *testing.T) {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos/1"
|
url := "https://jsonplaceholder.typicode.com/todos/1"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
|
|||||||
@@ -81,10 +81,16 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = setBodyByte(req, body)
|
if req.Header.Get("Content-Type") == "multipart/form-data" || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
||||||
|
formData := queryParam.(url.Values)
|
||||||
|
err = setBodyByte(req, []byte(formData.Encode()))
|
||||||
|
} else {
|
||||||
|
err = setBodyByte(req, body)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -253,6 +253,25 @@ func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T) {
|
|||||||
return groupA, groupB
|
return groupA, groupB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GroupWith return a map composed of keys generated from the results of running each element of slice thru iteratee.
|
||||||
|
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T {
|
||||||
|
if iteratee == nil {
|
||||||
|
panic("iteratee func is missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
res := make(map[U][]T)
|
||||||
|
|
||||||
|
for _, v := range slice {
|
||||||
|
key := iteratee(v)
|
||||||
|
if _, ok := res[key]; !ok {
|
||||||
|
res[key] = []T{}
|
||||||
|
}
|
||||||
|
res[key] = append(res[key], v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
// Find iterates over elements of slice, returning the first one that passes a truth test on predicate function.
|
||||||
// If return T is nil then no items matched the predicate func
|
// If return T is nil then no items matched the predicate func
|
||||||
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) {
|
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) {
|
||||||
@@ -552,7 +571,6 @@ func Union[T any](slices ...[]T) []T {
|
|||||||
|
|
||||||
// Intersection creates a slice of unique values that included by all slices.
|
// Intersection creates a slice of unique values that included by all slices.
|
||||||
func Intersection[T any](slices ...[]T) []T {
|
func Intersection[T any](slices ...[]T) []T {
|
||||||
var res []T
|
|
||||||
if len(slices) == 0 {
|
if len(slices) == 0 {
|
||||||
return []T{}
|
return []T{}
|
||||||
}
|
}
|
||||||
@@ -560,28 +578,51 @@ func Intersection[T any](slices ...[]T) []T {
|
|||||||
return Unique(slices[0])
|
return Unique(slices[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
//return elements both in slice1 and slice2
|
var res []T
|
||||||
reduceFunc := func(slice1, slice2 []T) []T {
|
|
||||||
|
reducer := func(s1, s2 []T) []T {
|
||||||
s := make([]T, 0, 0)
|
s := make([]T, 0, 0)
|
||||||
for _, v := range slice1 {
|
for _, v := range s1 {
|
||||||
if Contain(slice2, v) {
|
if Contain(s2, v) {
|
||||||
s = append(s, v)
|
s = append(s, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
res = reduceFunc(slices[0], slices[1])
|
res = reducer(slices[0], slices[1])
|
||||||
|
|
||||||
if len(slices) == 2 {
|
reduceSlice := make([][]T, 2, 2)
|
||||||
return Unique(res)
|
for i := 2; i < len(slices); i++ {
|
||||||
|
reduceSlice[0] = res
|
||||||
|
reduceSlice[1] = slices[i]
|
||||||
|
res = reducer(reduceSlice[0], reduceSlice[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp := make([][]T, 2, 2)
|
return Unique(res)
|
||||||
for i := 2; i < len(slices); i++ {
|
}
|
||||||
tmp[0] = res
|
|
||||||
tmp[1] = slices[i]
|
// SymmetricDifference oppoiste operation of intersection function
|
||||||
res = reduceFunc(tmp[0], tmp[1])
|
func SymmetricDifference[T any](slices ...[]T) []T {
|
||||||
|
if len(slices) == 0 {
|
||||||
|
return []T{}
|
||||||
|
}
|
||||||
|
if len(slices) == 1 {
|
||||||
|
return Unique(slices[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
res := make([]T, 0)
|
||||||
|
|
||||||
|
intersectSlice := Intersection(slices...)
|
||||||
|
|
||||||
|
for i := 0; i < len(slices); i++ {
|
||||||
|
slice := slices[i]
|
||||||
|
for _, v := range slice {
|
||||||
|
if !Contain(intersectSlice, v) {
|
||||||
|
res = append(res, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Unique(res)
|
return Unique(res)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package slice
|
package slice
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/internal"
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
@@ -142,6 +143,20 @@ func TestGroupBy(t *testing.T) {
|
|||||||
assert.Equal(expectedOdd, odd)
|
assert.Equal(expectedOdd, odd)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGroupWith(t *testing.T) {
|
||||||
|
nums := []float64{6.1, 4.2, 6.3}
|
||||||
|
floor := func(num float64) float64 {
|
||||||
|
return math.Floor(num)
|
||||||
|
}
|
||||||
|
expected := map[float64][]float64{
|
||||||
|
4: {4.2},
|
||||||
|
6: {6.1, 6.3},
|
||||||
|
}
|
||||||
|
actual := GroupWith(nums, floor)
|
||||||
|
assert := internal.NewAssert(t, "TestGroupWith")
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCount(t *testing.T) {
|
func TestCount(t *testing.T) {
|
||||||
nums := []int{1, 2, 3, 4, 5, 6}
|
nums := []int{1, 2, 3, 4, 5, 6}
|
||||||
evenFunc := func(i, num int) bool {
|
evenFunc := func(i, num int) bool {
|
||||||
@@ -392,7 +407,18 @@ func TestIntersection(t *testing.T) {
|
|||||||
for i := 0; i < len(res); i++ {
|
for i := 0; i < len(res); i++ {
|
||||||
assert.Equal(expected[i], res[i])
|
assert.Equal(expected[i], res[i])
|
||||||
}
|
}
|
||||||
// assert.IsNil(Intersection())
|
}
|
||||||
|
|
||||||
|
func TestSymmetricDifference(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSymmetricDifference")
|
||||||
|
|
||||||
|
s1 := []int{1, 2, 3}
|
||||||
|
s2 := []int{1, 2, 4}
|
||||||
|
s3 := []int{1, 2, 3, 5}
|
||||||
|
|
||||||
|
assert.Equal([]int{1, 2, 3}, SymmetricDifference(s1))
|
||||||
|
assert.Equal([]int{3, 4}, SymmetricDifference(s1, s2))
|
||||||
|
assert.Equal([]int{3, 4, 5}, SymmetricDifference(s1, s2, s3))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReverse(t *testing.T) {
|
func TestReverse(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user