1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-23 13:52:26 +08:00

Compare commits

...

4 Commits

Author SHA1 Message Date
dudaodong
0d0848ac67 refactor: use for loop than for range 2023-07-10 10:56:53 +08:00
dudaodong
d5334f892f feat: add Join 2023-07-10 10:51:43 +08:00
dudaodong
36ef5b3bd3 doc: add doc for HasKey 2023-07-10 10:39:55 +08:00
dudaodong
6f48b00c88 feat: add HasKey 2023-07-10 10:33:04 +08:00
10 changed files with 258 additions and 8 deletions

View File

@@ -43,6 +43,7 @@ import (
- [Merge](#Merge)
- [Minus](#Minus)
- [IsDisjoint](#IsDisjoint)
- [HasKey](#HasKey)
<div STYLE="page-break-after: always;"></div>
@@ -893,7 +894,7 @@ func main() {
### <span id="IsDisjoint">IsDisjoint</span>
<p>Checks two maps are disjoint if they have no keys in common</p>
<p>Checks two maps are disjoint if they have no keys in common.</p>
<b>Signature:</b>
@@ -937,3 +938,49 @@ func main() {
// false
}
```
### <span id="HasKey">HasKey</span>
<p>Checks if map has key or not. This function is used to replace the following boilerplate code:</p>
```go
_, haskey := amap["baz"];
if haskey {
fmt.Println("map has key baz")
}
```
<b>Signature:</b>
```go
func HasKey[K comparable, V any](m map[K]V, key K) bool
```
<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,
}
result1 := HasKey(m, "a")
result2 := HasKey(m, "c")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```

View File

@@ -43,6 +43,7 @@ import (
- [Merge](#Merge)
- [Minus](#Minus)
- [IsDisjoint](#IsDisjoint)
- [HasKey](#HasKey)
<div STYLE="page-break-after: always;"></div>
@@ -932,3 +933,49 @@ func main() {
// false
}
```
### <span id="HasKey">HasKey</span>
<p>检查map是否包含某个key。用于代替以下样板代码:</p>
```go
_, haskey := amap["baz"];
if haskey {
fmt.Println("map has key baz")
}
```
<b>函数签名:</b>
```go
func HasKey[K comparable, V any](m map[K]V, key K) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
}
result1 := HasKey(m, "a")
result2 := HasKey(m, "c")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}
```

View File

@@ -90,6 +90,7 @@ import (
- [UpdateAt](#UpdateAt)
- [Without](#Without)
- [KeyBy](#KeyBy)
- [Join](#Join)
<div STYLE="page-break-after: always;"></div>
@@ -2417,3 +2418,36 @@ func main() {
// map[1:a 2:ab 3:abc]
}
```
### <span id="Join">Join</span>
<p>Join the slice item with specify separator.</p>
<b>Signature:</b>
```go
func Join[T any](s []T, separator string) string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
result1 := slice.Join(nums, ",")
result2 := slice.Join(nums, "-")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1,2,3,4,5
// 1-2-3-4-5
}
```

View File

@@ -90,6 +90,7 @@ import (
- [UpdateAt](#UpdateAt)
- [Without](#Without)
- [KeyBy](#KeyBy)
- [Join](#Join)
<div STYLE="page-break-after: always;"></div>
@@ -2390,7 +2391,7 @@ func main() {
### <span id="KeyBy">KeyBy</span>
<p>将切片每个元素调用函数后转为map</p>
<p>将切片每个元素调用函数后转为map</p>
<b>函数签名:</b>
@@ -2417,3 +2418,36 @@ func main() {
// map[1:a 2:ab 3:abc]
}
```
### <span id="Join">Join</span>
<p>用指定的分隔符链接切片元素。</p>
<b>函数签名:</b>
```go
func Join[T any](s []T, separator string) string
```
<b>示例:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
nums := []int{1, 2, 3, 4, 5}
result1 := slice.Join(nums, ",")
result2 := slice.Join(nums, "-")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1,2,3,4,5
// 1-2-3-4-5
}
```

View File

@@ -289,3 +289,21 @@ func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value
return result
}
// HasKey checks if map has key or not.
// This function is used to replace the following boilerplate code:
// _, haskey := amap["baz"];
//
// if haskey {
// fmt.Println("map has key baz")
// }
//
// Play: todo
func HasKey[K comparable, V any](m map[K]V, key K) bool {
_, haskey := m[key]
if haskey {
return true
}
return false
}

View File

@@ -433,3 +433,20 @@ func ExampleMapTo() {
// <nil>
// {Nothin 28 123456789 {test 1}}
}
func ExampleHasKey() {
m := map[string]int{
"a": 1,
"b": 2,
}
result1 := HasKey(m, "a")
result2 := HasKey(m, "c")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}

View File

@@ -458,3 +458,17 @@ func TestMapValues(t *testing.T) {
assert.Equal(expected, result)
}
func TestHasKey(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestHasKey")
m := map[string]int{
"a": 1,
"b": 2,
}
assert.Equal(true, HasKey(m, "a"))
assert.Equal(false, HasKey(m, "c"))
}

View File

@@ -9,6 +9,7 @@ import (
"math/rand"
"reflect"
"sort"
"strings"
"time"
"golang.org/x/exp/constraints"
@@ -436,8 +437,8 @@ func flattenRecursive(value reflect.Value, result reflect.Value) reflect.Value {
// ForEach iterates over elements of slice and invokes function for each element.
// Play: https://go.dev/play/p/DrPaa4YsHRF
func ForEach[T any](slice []T, iteratee func(index int, item T)) {
for i, v := range slice {
iteratee(i, v)
for i := 0; i < len(slice); i++ {
iteratee(i, slice[i])
}
}
@@ -445,8 +446,8 @@ func ForEach[T any](slice []T, iteratee func(index int, item T)) {
// when iteratee return false, will break the for each loop.
// Play: https://go.dev/play/p/qScs39f3D9W
func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
for i, v := range slice {
if !iteratee(i, v) {
for i := 0; i < len(slice); i++ {
if !iteratee(i, slice[i]) {
break
}
}
@@ -457,8 +458,8 @@ func ForEachWithBreak[T any](slice []T, iteratee func(index int, item T) bool) {
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U {
result := make([]U, len(slice), cap(slice))
for i, v := range slice {
result[i] = iteratee(i, v)
for i := 0; i < len(slice); i++ {
result[i] = iteratee(i, slice[i])
}
return result
@@ -1188,3 +1189,13 @@ func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T {
return result
}
// Join the slice item with specify separator.
// Play: todo
func Join[T any](s []T, separator string) string {
str := Map(s, func(_ int, item T) string {
return fmt.Sprint(item)
})
return strings.Join(str, separator)
}

View File

@@ -1032,3 +1032,17 @@ func ExampleKeyBy() {
// Output:
// map[1:a 2:ab 3:abc]
}
func ExampleJoin() {
nums := []int{1, 2, 3, 4, 5}
result1 := Join(nums, ",")
result2 := Join(nums, "-")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 1,2,3,4,5
// 1-2-3-4-5
}

View File

@@ -1161,3 +1161,17 @@ func TestRepeat(t *testing.T) {
assert.Equal([]string{}, Repeat("a", 0))
assert.Equal([]string{"a", "a", "a", "a", "a", "a"}, Repeat("a", 6))
}
func TestJoin(t *testing.T) {
t.Parallel()
assert := internal.NewAssert(t, "TestJoin")
nums := []int{1, 2, 3, 4, 5}
result1 := Join(nums, ",")
result2 := Join(nums, "-")
assert.Equal("1,2,3,4,5", result1)
assert.Equal("1-2-3-4-5", result2)
}