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

Compare commits

...

16 Commits

Author SHA1 Message Date
dudaodong
9cd9d1aeb5 release v2.1.14 2023-02-06 17:42:13 +08:00
dudaodong
71b27c0aa9 feat: add ForEach, Reduce for stream 2023-02-06 17:40:54 +08:00
dudaodong
09a379ec6d feat: add ForEach, Reduce for stream 2023-02-06 17:38:33 +08:00
dudaodong
b4b9b03835 feat: add NoneMatch, AllMatch, AnyMatch for stream 2023-02-06 17:24:17 +08:00
dudaodong
48c7794b01 feat: add Limit method of stream 2023-02-06 17:08:36 +08:00
dudaodong
8e3911833d doc: normalize documents 2023-02-06 11:42:03 +08:00
dudaodong
ebe494051b doc: add doc for some new methods of list 2023-02-06 11:35:32 +08:00
dudaodong
c35bda6a65 feat: add ListToMap for list 2023-02-06 11:06:46 +08:00
dudaodong
1fe4cdc429 feat: add Difference and SymmetricDifference for list 2023-02-06 10:55:42 +08:00
dudaodong
6a79e322e3 Merge branch 'main' into v2 2023-02-06 10:00:02 +08:00
dudaodong
ea0f96a8c0 feat: add Skip for stream 2023-01-30 16:56:33 +08:00
dudaodong
82cbb54787 feat: add FromChannel for create stream 2023-01-17 16:47:20 +08:00
dudaodong
585d33cafa feat: add Generate for create stream 2023-01-17 16:31:44 +08:00
dudaodong
bc4cf35e15 feat: add Filter, Map, Count for stream 2023-01-17 14:59:47 +08:00
dudaodong
a3bc20af1d feat: add Distinct 2023-01-17 11:39:05 +08:00
dudaodong
61338b6b46 feat: add Stream package 2023-01-17 11:25:15 +08:00
29 changed files with 1415 additions and 353 deletions

View File

@@ -4,7 +4,7 @@
<br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.13-green.svg)](https://github.com/duke-git/lancet/releases)
[![Release](https://img.shields.io/badge/release-2.1.14-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)

View File

@@ -4,7 +4,7 @@
<br/>
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.13-green.svg)](https://github.com/duke-git/lancet/releases)
[![Release](https://img.shields.io/badge/release-2.1.14-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -51,7 +51,7 @@ lancet 是以包的结构组织代码的,使用时需要导入相应的包名
import "github.com/duke-git/lancet/v2/strutil"
```
## 例
##
此处以字符串工具函数 Reverse逆序字符串为例需要导入 strutil 包:

View File

@@ -5,8 +5,9 @@
package datastructure
import (
"github.com/duke-git/lancet/v2/iterator"
"reflect"
"github.com/duke-git/lancet/v2/iterator"
)
// List is a linear table, implemented with slice.
@@ -317,6 +318,43 @@ func (l *List[T]) Intersection(other *List[T]) *List[T] {
return result
}
// Difference returns the difference between two collections.
// return a list whose element in the original list, not in the given list.
func (l *List[T]) Difference(other *List[T]) *List[T] {
result := NewList(make([]T, 0))
intersectList := l.Intersection(other)
for _, v := range l.data {
if !intersectList.Contain(v) {
result.data = append(result.data, v)
}
}
return result
}
// SymmetricDifference oppoiste operation of intersection function.
func (l *List[T]) SymmetricDifference(other *List[T]) *List[T] {
result := NewList(make([]T, 0))
intersectList := l.Intersection(other)
for _, v := range l.data {
if !intersectList.Contain(v) {
result.data = append(result.data, v)
}
}
for _, v := range other.data {
if !intersectList.Contain(v) {
result.data = append(result.data, v)
}
}
return result
}
// SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] {
data := l.data[fromIndex:toIndex]
@@ -367,3 +405,14 @@ func (l *List[T]) batchRemove(list *List[T], complement bool) bool {
func (l *List[T]) Iterator() iterator.Iterator[T] {
return iterator.FromSlice(l.data)
}
// ListToMap convert a list to a map based on iteratee function.
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V {
result := make(map[K]V, list.Size())
for _, item := range list.data {
k, v := iteratee(item)
result[k] = v
}
return result
}

View File

@@ -328,6 +328,28 @@ func TestIntersection(t *testing.T) {
assert.Equal(true, expected.Equal(list3))
}
func TestDifference(t *testing.T) {
assert := internal.NewAssert(t, "TestDifference")
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
expected := NewList([]int{3})
list3 := list1.Difference(list2)
assert.Equal(true, expected.Equal(list3))
}
func TestSymmetricDifference(t *testing.T) {
assert := internal.NewAssert(t, "TestSymmetricDifference")
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
expected := NewList([]int{3, 4})
list3 := list1.SymmetricDifference(list2)
assert.Equal(true, expected.Equal(list3))
}
func TestSubSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestSubSlice")
@@ -425,3 +447,16 @@ func TestIterator(t *testing.T) {
assert.Equal([]int{1, 2, 3, 4}, rs)
}
func TestListToMap(t *testing.T) {
assert := internal.NewAssert(t, "ListToMap")
list := NewList([]int{1, 2, 3, 4})
result := ListToMap(list, func(n int) (int, bool) {
return n, n > 1
})
expected := map[int]bool{1: false, 2: true, 3: true, 4: true}
assert.Equal(expected, result)
}

View File

@@ -46,7 +46,7 @@ import (
type Channel[T any] struct
func NewChannel[T any]() *Channel[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -70,7 +70,7 @@ func main() {
```go
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -122,7 +122,7 @@ func main() {
```go
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -161,7 +161,7 @@ func main() {
```go
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -199,7 +199,7 @@ func main() {
```go
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -238,7 +238,7 @@ func main() {
```go
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -279,7 +279,7 @@ func main() {
```go
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -322,7 +322,7 @@ func main() {
```go
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -360,7 +360,7 @@ func main() {
```go
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -406,7 +406,7 @@ func main() {
```go
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -45,7 +45,7 @@ slices和map的length大于0时返回true否则返回false<br/>
```go
func Bool[T any](value T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -109,7 +109,7 @@ func main() {
```go
func And[T, U any](a T, b U) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -135,7 +135,7 @@ func main() {
```go
func Or[T, U any](a T, b U) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -161,7 +161,7 @@ func main() {
```go
func Xor[T, U any](a T, b U) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -187,7 +187,7 @@ func main() {
```go
func Nor[T, U any](a T, b U) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -213,7 +213,7 @@ func main() {
```go
func Xnor[T, U any](a T, b U) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -239,7 +239,7 @@ func main() {
```go
func Nand[T, U any](a T, b U) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -265,7 +265,7 @@ func main() {
```go
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -46,7 +46,7 @@ import (
func NewHashMap() *HashMap
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -72,7 +72,7 @@ func main() {
func NewHashMapWithCapacity(size, capacity uint64) *HashMap
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -98,7 +98,7 @@ func main() {
func (hm *HashMap) Get(key any) any
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -126,7 +126,7 @@ func main() {
func (hm *HashMap) Put(key any, value any) any
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -155,7 +155,7 @@ func main() {
func (hm *HashMap) Delete(key any)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -187,7 +187,7 @@ func main() {
func (hm *HashMap) Contains(key any) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -217,7 +217,7 @@ func main() {
func (hm *HashMap) Iterate(iteratee func(key, value any))
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -252,7 +252,7 @@ func main() {
func (hm *HashMap) Keys() []any
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -284,7 +284,7 @@ func main() {
func (hm *HashMap) Values() []any
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -48,7 +48,7 @@ type MaxHeap[T any] struct {
}
func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -89,7 +89,7 @@ func main() {
```go
func (h *MaxHeap[T]) Push(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -136,7 +136,7 @@ func main() {
```go
func (h *MaxHeap[T]) Pop() (T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -184,7 +184,7 @@ func main() {
```go
func (h *MaxHeap[T]) Peek() (T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -232,7 +232,7 @@ func main() {
```go
func (h *MaxHeap[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -277,7 +277,7 @@ func main() {
```go
func (h *MaxHeap[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -323,7 +323,7 @@ func main() {
```go
func (h *MaxHeap[T]) PrintStructure()
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -81,7 +81,7 @@ type SinglyLink[T any] struct {
}
func NewSinglyLink[T any]() *SinglyLink[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -107,7 +107,7 @@ func main() {
```go
func (link *SinglyLink[T]) Values() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -139,7 +139,7 @@ func main() {
```go
func (link *SinglyLink[T]) InsertAt(index int, value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -174,7 +174,7 @@ func main() {
```go
func (link *SinglyLink[T]) InsertAtHead(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -206,7 +206,7 @@ func main() {
```go
func (link *SinglyLink[T]) InsertAtTail(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -237,7 +237,7 @@ func main() {
```go
func (link *SinglyLink[T]) DeleteAt(index int)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -271,7 +271,7 @@ func main() {
```go
func (link *SinglyLink[T]) DeleteAtHead()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -306,7 +306,7 @@ func main() {
```go
func (link *SinglyLink[T]) DeleteAtTail()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -339,7 +339,7 @@ func main() {
```go
func (link *SinglyLink[T]) DeleteValue(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -373,7 +373,7 @@ func main() {
```go
func (link *SinglyLink[T]) Reverse()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -405,7 +405,7 @@ func main() {
```go
func (link *SinglyLink[T]) GetMiddleNode() *datastructure.LinkNode[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -437,7 +437,7 @@ func main() {
```go
func (link *SinglyLink[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -468,7 +468,7 @@ func main() {
```go
func (link *SinglyLink[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -500,7 +500,7 @@ func main() {
```go
func (link *SinglyLink[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -533,7 +533,7 @@ func main() {
```go
func (link *SinglyLink[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -576,7 +576,7 @@ type DoublyLink[T any] struct {
}
func NewDoublyLink[T any]() *DoublyLink[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -602,7 +602,7 @@ func main() {
```go
func (link *DoublyLink[T]) Values() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -634,7 +634,7 @@ func main() {
```go
func (link *DoublyLink[T]) InsertAt(index int, value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -669,7 +669,7 @@ func main() {
```go
func (link *DoublyLink[T]) InsertAtHead(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -701,7 +701,7 @@ func main() {
```go
func (link *DoublyLink[T]) InsertAtTail(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -732,7 +732,7 @@ func main() {
```go
func (link *DoublyLink[T]) DeleteAt(index int)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -766,7 +766,7 @@ func main() {
```go
func (link *DoublyLink[T]) DeleteAtHead()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -801,7 +801,7 @@ func main() {
```go
func (link *DoublyLink[T]) DeleteAtTail()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -835,7 +835,7 @@ func main() {
```go
func (link *DoublyLink[T]) Reverse()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -867,7 +867,7 @@ func main() {
```go
func (link *DoublyLink[T]) GetMiddleNode() *datastructure.LinkNode[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -899,7 +899,7 @@ func main() {
```go
func (link *DoublyLink[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -930,7 +930,7 @@ func main() {
```go
func (link *DoublyLink[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -962,7 +962,7 @@ func main() {
```go
func (link *DoublyLink[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -995,7 +995,7 @@ func main() {
```go
func (link *DoublyLink[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -47,6 +47,13 @@ import (
- [Unique](#Unique)
- [Union](#Union)
- [Intersection](#Intersection)
- [Difference](#Difference)
- [SymmetricDifference](#SymmetricDifference)
- [RetainAll](#RetainAll)
- [DeleteAll](#DeleteAll)
- [ForEach](#ForEach)
- [Iterator](#Iterator)
- [ListToMap](#ListToMap)
- [SubList](#SubList)
- [DeleteIf](#DeleteIf)
@@ -62,7 +69,7 @@ NewList function return a list pointer</p>
```go
type List[T any] struct {
data []T
data []T
}
func NewList[T any](data []T) *List[T]
```
@@ -671,8 +678,8 @@ import (
)
func main() {
data := make([]int, 0, 100)
data := make([]int, 0, 100)
li := list.NewList(data)
fmt.Println(li.Cap()) // 100
@@ -828,6 +835,233 @@ func main() {
### <span id="Difference">Difference</span>
<p>Return a list whose element in the original list, not in the given list.</p>
<b>Signature:</b>
```go
func (l *List[T]) Difference(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3
}
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>Oppoiste operation of intersection function.</p>
<b>Signature:</b>
```go
func (l *List[T]) SymmetricDifference(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3, 4
}
```
### <span id="RetainAll">RetainAll</span>
<p>Retains only the elements in this list that are contained in the given list.</p>
<b>Signature:</b>
```go
func (l *List[T]) RetainAll(list *List[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
retain := NewList([]int{1, 2})
retain1 := NewList([]int{2, 3})
retain2 := NewList([]int{1, 2, 5})
list.RetainAll(retain)
list1.RetainAll(retain1)
list2.RetainAll(retain2)
fmt.Println(list.Data()) //1, 2
fmt.Println(list1.Data()) //2, 3
fmt.Println(list2.Data()) //1, 2
}
```
### <span id="DeleteAll">DeleteAll</span>
<p>Removes from this list all of its elements that are contained in the given list.</p>
<b>Signature:</b>
```go
func (l *List[T]) DeleteAll(list *List[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
del := NewList([]int{1})
del1 := NewList([]int{2, 3})
del2 := NewList([]int{1, 2, 5})
list.DeleteAll(del)
list1.DeleteAll(del1)
list2.DeleteAll(del2)
fmt.Println(list.Data()) //2,3,4
fmt.Println(list1.Data()) //1,4
fmt.Println(list2.Data()) //3,4
}
```
### <span id="ForEach">ForEach</span>
<p>Performs the given action for each element of the list.</p>
<b>Signature:</b>
```go
func (l *List[T]) ForEach(consumer func(T))
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := make([]int, 0)
list.ForEach(func(i int) {
result = append(result, i)
})
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="Iterator">Iterator</span>
<p>Returns an iterator over the elements in this list in proper sequence.</p>
<b>Signature:</b>
```go
func (l *List[T]) Iterator() iterator.Iterator[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
iterator := list.Iterator()
result := make([]int, 0)
for iterator.HasNext() {
item, _ := iterator.Next()
result = append(result, item)
}
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="ListToMap">ListToMap</span>
<p>Converts a list to a map based on iteratee function.</p>
<b>Signature:</b>
```go
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := ListToMap(list, func(n int) (int, bool) {
return n, n > 1
})
fmt.Println(result) //map[int]bool{1: false, 2: true, 3: true, 4: true}
}
```
### <span id="SubList">SubList</span>
<p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p>
@@ -876,9 +1110,9 @@ import (
)
func main() {
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4}
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4}
}
```

View File

@@ -34,7 +34,6 @@ import (
- [PopLast](#PopLast)
- [DeleteAt](#DeleteAt)
- [InsertAt](#InsertAt)
- [UpdateAt](#UpdateAt)
- [Equal](#Equal)
- [IsEmpty](#IsEmpty)
@@ -48,6 +47,13 @@ import (
- [Unique](#Unique)
- [Union](#Union)
- [Intersection](#Intersection)
- [Difference](#Difference)
- [SymmetricDifference](#SymmetricDifference)
- [RetainAll](#RetainAll)
- [DeleteAll](#DeleteAll)
- [ForEach](#ForEach)
- [Iterator](#Iterator)
- [ListToMap](#ListToMap)
- [SubList](#SubList)
- [DeleteIf](#DeleteIf)
@@ -62,11 +68,11 @@ import (
```go
type List[T any] struct {
data []T
data []T
}
func NewList[T any](data []T) *List[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -92,7 +98,7 @@ func main() {
```go
func (l *List[T]) Contain(value T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -121,7 +127,7 @@ func main() {
```go
func (l *List[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -150,7 +156,7 @@ func main() {
```go
func (l *List[T]) ValueOf(index int) (*T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -180,7 +186,7 @@ func main() {
```go
func (l *List[T]) IndexOf(value T) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -207,7 +213,7 @@ func main() {
```go
func (l *List[T]) LastIndexOf(value T) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -233,7 +239,7 @@ func main() {
```go
func (l *List[T]) IndexOfFunc(f func(T) bool) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -259,7 +265,7 @@ func main() {
```go
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -287,7 +293,7 @@ func main() {
```go
func (l *List[T]) Push(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -316,7 +322,7 @@ func main() {
```go
func (l *List[T]) PopFirst() (*T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -348,7 +354,7 @@ func main() {
```go
func (l *List[T]) PopLast() (*T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -379,7 +385,7 @@ func main() {
```go
func (l *List[T]) DeleteAt(index int)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -417,7 +423,7 @@ func main() {
```go
func (l *List[T]) InsertAt(index int, value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -454,7 +460,7 @@ func main() {
```go
func (l *List[T]) UpdateAt(index int, value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -487,7 +493,7 @@ func main() {
```go
func (l *List[T]) Equal(other *List[T]) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -517,7 +523,7 @@ func main() {
```go
func (l *List[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -547,7 +553,7 @@ func main() {
```go
func (l *List[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -575,7 +581,7 @@ func main() {
```go
func (l *List[T]) Clone() *List[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -604,7 +610,7 @@ func main() {
```go
func (l *List[T]) Merge(other *List[T]) *List[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -633,7 +639,7 @@ func main() {
```go
func (l *List[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -660,7 +666,7 @@ func main() {
```go
func (l *List[T]) Cap() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -671,8 +677,8 @@ import (
)
func main() {
data := make([]int, 0, 100)
data := make([]int, 0, 100)
li := list.NewList(data)
fmt.Println(li.Cap()) // 100
@@ -689,7 +695,7 @@ func main() {
```go
func (l *List[T]) Swap(i, j int)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -718,7 +724,7 @@ func main() {
```go
func (l *List[T]) Reverse()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -747,7 +753,7 @@ func main() {
```go
func (l *List[T]) Unique()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -776,7 +782,7 @@ func main() {
```go
func (l *List[T]) Union(other *List[T]) *List[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -806,7 +812,7 @@ func main() {
```go
func (l *List[T]) Intersection(other *List[T]) *List[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -826,16 +832,244 @@ func main() {
```
### <span id="Difference">Difference</span>
<p>差集运算。</p>
<b>函数签名:</b>
```go
func (l *List[T]) Difference(other *List[T]) *List[T]
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3
}
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>对称差集运算。</p>
<b>函数签名:</b>
```go
func (l *List[T]) SymmetricDifference(other *List[T]) *List[T]
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list1 := NewList([]int{1, 2, 3})
list2 := NewList([]int{1, 2, 4})
list3 := list1.Intersection(list2)
fmt.Println(list3.Data()) //3, 4
}
```
### <span id="RetainAll">RetainAll</span>
<p>仅保留列表中包含在给定列表中的元素。</p>
<b>函数签名:</b>
```go
func (l *List[T]) RetainAll(list *List[T]) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
retain := NewList([]int{1, 2})
retain1 := NewList([]int{2, 3})
retain2 := NewList([]int{1, 2, 5})
list.RetainAll(retain)
list1.RetainAll(retain1)
list2.RetainAll(retain2)
fmt.Println(list.Data()) //1, 2
fmt.Println(list1.Data()) //2, 3
fmt.Println(list2.Data()) //1, 2
}
```
### <span id="DeleteAll">DeleteAll</span>
<p>从列表中删除给定列表中包含的所有元素。</p>
<b>函数签名:</b>
```go
func (l *List[T]) DeleteAll(list *List[T]) bool
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
del := NewList([]int{1})
del1 := NewList([]int{2, 3})
del2 := NewList([]int{1, 2, 5})
list.DeleteAll(del)
list1.DeleteAll(del1)
list2.DeleteAll(del2)
fmt.Println(list.Data()) //2,3,4
fmt.Println(list1.Data()) //1,4
fmt.Println(list2.Data()) //3,4
}
```
### <span id="ForEach">ForEach</span>
<p>对列表的每个元素执行给定的操作。</p>
<b>函数签名:</b>
```go
func (l *List[T]) ForEach(consumer func(T))
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := make([]int, 0)
list.ForEach(func(i int) {
result = append(result, i)
})
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="Iterator">Iterator</span>
<p>按顺序返回列表中元素的迭代器。</p>
<b>函数签名:</b>
```go
func (l *List[T]) Iterator() iterator.Iterator[T]
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
iterator := list.Iterator()
result := make([]int, 0)
for iterator.HasNext() {
item, _ := iterator.Next()
result = append(result, item)
}
fmt.Println(result.Data()) //1,2,3,4
}
```
### <span id="ListToMap">ListToMap</span>
<p>基于iteratee函数将列表转换为映射map。</p>
<b>函数签名:</b>
```go
func ListToMap[T any, K comparable, V any](list *List[T], iteratee func(T) (K, V)) map[K]V
```
<b>示例:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
list := NewList([]int{1, 2, 3, 4})
result := ListToMap(list, func(n int) (int, bool) {
return n, n > 1
})
fmt.Println(result) //map[int]bool{1: false, 2: true, 3: true, 4: true}
}
```
### <span id="SubList">SubList</span>
<p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p>
<p>返回指定的fromIndex包含和toIndex不包含之间的原始列表的子列表。</p>
<b>函数签名:</b>
```go
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -863,7 +1097,7 @@ func main() {
```go
func (l *List[T]) DeleteIf(f func(T) bool) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -874,9 +1108,9 @@ import (
)
func main() {
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4}
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4}
}
```

View File

@@ -99,7 +99,7 @@ type ArrayQueue[T any] struct {
size int
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -125,7 +125,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -152,7 +152,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Enqueue(item T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -183,7 +183,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Dequeue() (T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -215,7 +215,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Front() T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -247,7 +247,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Back() T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -278,7 +278,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -308,7 +308,7 @@ func main() {
```go
func (q *ArrayQueue[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -341,7 +341,7 @@ func main() {
```go
func (q *ArrayQueue[T]) IsFull() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -373,7 +373,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -404,7 +404,7 @@ func main() {
```go
func (q *ArrayQueue[T]) Contain(value T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -448,7 +448,7 @@ type QueueNode[T any] struct {
Next *QueueNode[T]
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -474,7 +474,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -501,7 +501,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Enqueue(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -532,7 +532,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Dequeue() (T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -564,7 +564,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Front() (*T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -596,7 +596,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Back() (*T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -627,7 +627,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -657,7 +657,7 @@ func main() {
```go
func (q *LinkedQueue[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -690,7 +690,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -721,7 +721,7 @@ func main() {
```go
func (q *LinkedQueue[T]) Contain(value T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -763,7 +763,7 @@ type CircularQueue[T any] struct {
capacity int
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -789,7 +789,7 @@ func main() {
```go
func (q *CircularQueue[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -816,7 +816,7 @@ func main() {
```go
func (q *CircularQueue[T]) Enqueue(value T) error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -847,7 +847,7 @@ func main() {
```go
func (q *CircularQueue[T]) Dequeue() (*T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -880,7 +880,7 @@ func main() {
```go
func (q *CircularQueue[T]) Front() T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -912,7 +912,7 @@ func main() {
```go
func (q *CircularQueue[T]) Back() T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -943,7 +943,7 @@ func main() {
```go
func (q *CircularQueue[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -973,7 +973,7 @@ func main() {
```go
func (q *CircularQueue[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1006,7 +1006,7 @@ func main() {
```go
func (q *CircularQueue[T]) IsFull() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1038,7 +1038,7 @@ func main() {
```go
func (q *CircularQueue[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1069,7 +1069,7 @@ func main() {
```go
func (q *CircularQueue[T]) Contain(value T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1108,7 +1108,7 @@ type PriorityQueue[T any] struct {
comparator lancetconstraints.Comparator
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1134,7 +1134,7 @@ func main() {
```go
func (q *PriorityQueue[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1161,7 +1161,7 @@ func main() {
```go
func (q *PriorityQueue[T]) Enqueue(item T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1207,7 +1207,7 @@ func main() {
```go
func (q *PriorityQueue[T]) Dequeue() (T, bool)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1254,7 +1254,7 @@ func main() {
```go
func (q *PriorityQueue[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1301,7 +1301,7 @@ func main() {
```go
func (q *PriorityQueue[T]) IsFull() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -1348,7 +1348,7 @@ func main() {
```go
func (q *PriorityQueue[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -55,7 +55,7 @@ import (
type Set[T comparable] map[T]bool
func NewSet[T comparable](items ...T) Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -81,7 +81,7 @@ func main() {
```go
func NewSetFromSlice[T comparable](items []T) Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -107,7 +107,7 @@ func main() {
```go
func (s Set[T]) Values() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -134,7 +134,7 @@ func main() {
```go
func (s Set[T]) Add(items ...T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -161,7 +161,7 @@ func main() {
```go
func (s Set[T]) AddIfNotExist(item T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -193,7 +193,7 @@ func main() {
```go
func (s Set[T]) AddIfNotExistBy(item T, checker func(element T) bool) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -232,7 +232,7 @@ func main() {
```go
func (s Set[T]) Delete(items ...T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -261,7 +261,7 @@ func main() {
```go
func (s Set[T]) Contain(item T) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -291,7 +291,7 @@ func main() {
```go
func (s Set[T]) ContainAll(other Set[T]) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -321,7 +321,7 @@ func main() {
```go
func (s Set[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -348,7 +348,7 @@ func main() {
```go
func (s Set[T]) Clone() Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -378,7 +378,7 @@ func main() {
```go
func (s Set[T]) Equal(other Set[T]) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -408,7 +408,7 @@ func main() {
```go
func (s Set[T]) Iterate(fn func(item T))
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -439,7 +439,7 @@ func main() {
```go
func (s Set[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -468,7 +468,7 @@ func main() {
```go
func (s Set[T]) Union(other Set[T]) Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -497,7 +497,7 @@ func main() {
```go
func (s Set[T]) Intersection(other Set[T]) Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -525,7 +525,7 @@ func main() {
```go
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -556,7 +556,7 @@ func main() {
```go
func (s Set[T]) Minus(comparedSet Set[T]) Set[T]
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -64,7 +64,7 @@ type ArrayStack[T any] struct {
}
func NewArrayStack[T any]() *ArrayStack[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -91,7 +91,7 @@ func main() {
```go
func (s *ArrayStack[T]) Push(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -122,7 +122,7 @@ func main() {
```go
func (s *ArrayStack[T]) Pop() (*T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -157,7 +157,7 @@ func main() {
```go
func (s *ArrayStack[T]) Peak() (*T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -192,7 +192,7 @@ func main() {
```go
func (s *ArrayStack[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -223,7 +223,7 @@ func main() {
```go
func (s *ArrayStack[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -254,7 +254,7 @@ func main() {
```go
func (s *ArrayStack[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -287,7 +287,7 @@ func main() {
```go
func (s *ArrayStack[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -331,7 +331,7 @@ type LinkedStack[T any] struct {
}
func NewLinkedStack[T any]() *LinkedStack[T]
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -358,7 +358,7 @@ func main() {
```go
func (s *LinkedStack[T]) Push(value T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -389,7 +389,7 @@ func main() {
```go
func (s *LinkedStack[T]) Pop() (*T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -424,7 +424,7 @@ func main() {
```go
func (s *LinkedStack[T]) Peak() (*T, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -459,7 +459,7 @@ func main() {
```go
func (s *LinkedStack[T]) Data() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -490,7 +490,7 @@ func main() {
```go
func (s *LinkedStack[T]) Size() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -521,7 +521,7 @@ func main() {
```go
func (s *LinkedStack[T]) IsEmpty() bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -554,7 +554,7 @@ func main() {
```go
func (s *LinkedStack[T]) Clear()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -588,7 +588,7 @@ func main() {
```go
func (s *LinkedStack[T]) Print()
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -62,7 +62,7 @@ type TreeNode[T any] struct {
Right *TreeNode[T]
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -103,7 +103,7 @@ func main() {
```go
func (t *BSTree[T]) Insert(data T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -149,7 +149,7 @@ func main() {
```go
func (t *BSTree[T]) Delete(data T)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -197,7 +197,7 @@ func main() {
```go
func (t *BSTree[T]) PreOrderTraverse() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -243,7 +243,7 @@ func main() {
```go
func (t *BSTree[T]) InOrderTraverse() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -289,7 +289,7 @@ func main() {
```go
func (t *BSTree[T]) PostOrderTraverse() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -335,7 +335,7 @@ func main() {
```go
func (t *BSTree[T]) LevelOrderTraverse() []T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -381,7 +381,7 @@ func main() {
```go
func (t *BSTree[T]) Depth() int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -427,7 +427,7 @@ func main() {
```go
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -479,7 +479,7 @@ func main() {
```go
func (t *BSTree[T]) Print()
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -52,7 +52,7 @@ import (
func ClearFile(path string) error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -80,7 +80,7 @@ func main() {
func CreateFile(path string) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -132,7 +132,7 @@ func main() {
func CopyFile(srcFilePath string, dstFilePath string) error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -160,7 +160,7 @@ func main() {
func FileMode(path string) (fs.FileMode, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -189,7 +189,7 @@ func main() {
func MiMeType(file any) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -220,7 +220,7 @@ func main() {
func IsExist(path string) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -247,7 +247,7 @@ func main() {
func IsLink(path string) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -273,7 +273,7 @@ func main() {
func IsDir(path string) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -302,7 +302,7 @@ func main() {
func ListFileNames(path string) ([]string, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -328,7 +328,7 @@ func main() {
func RemoveFile(path string) error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -356,7 +356,7 @@ func main() {
func ReadFileToString(path string) (string, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -389,7 +389,7 @@ func main() {
func ReadFileByLine(path string)([]string, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -423,7 +423,7 @@ func main() {
func Zip(fpath string, destPath string) error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -451,7 +451,7 @@ func main() {
func UnZip(zipFile string, destPath string) error
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -38,7 +38,7 @@ import (
func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -46,7 +46,7 @@ import (
func After(n int, fn any) func(args ...any) []reflect.Value
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -79,7 +79,7 @@ func main() {
func Before(n int, fn any) func(args ...any) []reflect.Value
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -117,7 +117,7 @@ type CurryFn[T any] func(...T) T
func (cf CurryFn[T]) New(val T) func(...T) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -156,7 +156,7 @@ func main() {
func Compose[T any](fnList ...func(...T) T) func(...T) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -194,7 +194,7 @@ func main() {
func Debounced(fn func(), duration time.Duration) func()
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -244,7 +244,7 @@ func main() {
func Delay(delay time.Duration, fn any, args ...any)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -276,7 +276,7 @@ func main() {
func Schedule(d time.Duration, fn any, args ...any) chan bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -315,7 +315,7 @@ func main() {
func Pipeline[T any](funcs ...func(T) T) func(T) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -366,7 +366,7 @@ func (w *Watcher) Reset() //reset the watcher
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -45,7 +45,7 @@ import (
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -86,7 +86,7 @@ func main() {
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -131,7 +131,7 @@ func main() {
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -186,7 +186,7 @@ func main() {
func Keys[K comparable, V any](m map[K]V) []K
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -225,7 +225,7 @@ func main() {
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -264,7 +264,7 @@ func main() {
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -306,7 +306,7 @@ func main() {
func Values[K comparable, V any](m map[K]V) []V
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -343,7 +343,7 @@ func main() {
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -49,7 +49,7 @@ import (
func Average[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -84,7 +84,7 @@ func main() {
func Exponent(x, n int64) int64
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -120,7 +120,7 @@ func main() {
func Fibonacci(first, second, n int) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -156,7 +156,7 @@ func main() {
func Factorial(x uint) uint
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -192,7 +192,7 @@ func main() {
func Max[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -225,7 +225,7 @@ func main() {
func MaxBy[T any](slice []T, comparator func(T, T) bool) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -269,7 +269,7 @@ func main() {
func Min[T constraints.Integer | constraints.Float](numbers ...T) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -302,7 +302,7 @@ func main() {
func MinBy[T any](slice []T, comparator func(T, T) bool) T
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -346,7 +346,7 @@ func main() {
func Percent(val, total float64, n int) float64
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -379,7 +379,7 @@ func main() {
func RoundToFloat(x float64, n int) float64
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -415,7 +415,7 @@ func main() {
func RoundToString(x float64, n int) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -451,7 +451,7 @@ func main() {
func TruncRound(x float64, n int) float64
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -59,7 +59,7 @@ import (
func ConvertMapToQueryString(param map[string]any) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -94,7 +94,7 @@ func main() {
func EncodeUrl(urlStr string) (string, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -129,7 +129,7 @@ func main() {
func GetInternalIp() string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -161,7 +161,7 @@ func main() {
func GetIps() []string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -191,7 +191,7 @@ func main() {
func GetMacAddrs() []string {
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -235,7 +235,7 @@ type PublicIpInfo struct {
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -265,7 +265,7 @@ func main() {
func GetRequestPublicIp(req *http.Request) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -303,7 +303,7 @@ func main() {
func IsPublicIP(IP net.IP) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -340,7 +340,7 @@ func main() {
func IsInternalIP(IP net.IP) bool
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -384,7 +384,7 @@ type HttpRequest struct {
}
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -441,7 +441,7 @@ func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -472,7 +472,7 @@ func main() {
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -526,7 +526,7 @@ func main() {
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -580,7 +580,7 @@ func main() {
func StructToUrlValues(targetStruct any) url.Values
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -624,7 +624,7 @@ func main() {
func HttpGet(url string, params ...any) (*http.Response, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -666,7 +666,7 @@ func main() {
func HttpPost(url string, params ...any) (*http.Response, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -715,7 +715,7 @@ func main() {
func HttpPut(url string, params ...any) (*http.Response, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -765,7 +765,7 @@ func main() {
func HttpDelete(url string, params ...any) (*http.Response, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -804,7 +804,7 @@ func main() {
func HttpPatch(url string, params ...any) (*http.Response, error)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -850,7 +850,7 @@ func main() {
func ParseHttpResponse(resp *http.Response, obj any) error
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -45,7 +45,7 @@ import (
func RandBytes(length int) []byte
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -71,7 +71,7 @@ func main() {
func RandInt(min, max int) int
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -97,7 +97,7 @@ func main() {
func RandString(length int) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -123,7 +123,7 @@ func main() {
func RandUpper(length int) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -149,7 +149,7 @@ func main() {
func RandLower(length int) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -175,7 +175,7 @@ func main() {
func RandNumeral(length int) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -201,7 +201,7 @@ func main() {
func RandNumeralOrLetter(length int) string
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -227,7 +227,7 @@ func main() {
func UUIdV4() (string, error)
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -42,7 +42,7 @@ import (
func Context(ctx context.Context)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -89,7 +89,7 @@ func main() {
type RetryFunc func() error
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -135,7 +135,7 @@ func main() {
func RetryTimes(n uint)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -178,7 +178,7 @@ func main() {
func RetryDuration(d time.Duration)
```
<b>例:</b>
<b>例:</b>
```go
package main
@@ -224,7 +224,7 @@ func main() {
func Retry(retryFunc RetryFunc, opts ...Option) error
```
<b>例:</b>
<b>例:</b>
```go
package main

View File

@@ -90,7 +90,7 @@ import (
func AppendIfAbsent[T comparable](slice []T, item T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -121,7 +121,7 @@ func main() {
func Contain[T comparable](slice []T, target T) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -152,7 +152,7 @@ func main() {
func ContainSubSlice[T comparable](slice, subSlice []T) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -183,7 +183,7 @@ func main() {
func Chunk[T any](slice []T, size int) [][]T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -225,7 +225,7 @@ func main() {
func Compact[T comparable](slice []T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -262,7 +262,7 @@ func main() {
func Concat[T any](slice []T, slices ...[]T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -293,7 +293,7 @@ func main() {
func Count[T comparable](slice []T, item T) int
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -326,7 +326,7 @@ func main() {
func CountBy[T any](slice []T, predicate func(index int, item T) bool) int
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -360,7 +360,7 @@ func main() {
func Difference[T comparable](slice, comparedSlice []T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -391,7 +391,7 @@ func main() {
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -426,7 +426,7 @@ func main() {
func DifferenceWith[T any](slice []T, comparedSlice []T, comparator func(value, otherValue T) bool) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -461,7 +461,7 @@ func main() {
func DeleteAt[T any](slice []T, start int, end ...int)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -496,7 +496,7 @@ func main() {
func Drop[T any](slice []T, n int) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -533,7 +533,7 @@ func main() {
func Every[T any](slice []T, predicate func(index int, item T) bool) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -567,7 +567,7 @@ func main() {
func Equal[T comparable](slice1, slice2 []T) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -602,7 +602,7 @@ func main() {
func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -637,7 +637,7 @@ func main() {
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -671,7 +671,7 @@ func main() {
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -707,7 +707,7 @@ func main() {
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -743,7 +743,7 @@ func main() {
func Flatten(slice any) any
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -773,7 +773,7 @@ func main() {
func FlattenDeep(slice any) any
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -803,7 +803,7 @@ func main() {
func ForEach[T any](slice []T, iteratee func(index int, item T))
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -838,7 +838,7 @@ func main() {
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -874,7 +874,7 @@ func main() {
func GroupWith[T any, U comparable](slice []T, iteratee func(T) U) map[U][]T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -908,7 +908,7 @@ func main() {
func IntSlice(slice any) []int
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -937,7 +937,7 @@ func main() {
func InterfaceSlice(slice any) []any
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -966,7 +966,7 @@ func main() {
func Intersection[T comparable](slices ...[]T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -997,7 +997,7 @@ func main() {
func InsertAt[T any](slice []T, index int, value any) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1037,7 +1037,7 @@ func main() {
func IndexOf[T comparable](slice []T, item T) int
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1070,7 +1070,7 @@ func main() {
func LastIndexOf[T comparable](slice []T, item T) int
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1103,7 +1103,7 @@ func main() {
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1137,7 +1137,7 @@ func main() {
func Merge[T any](slices ...[]T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1168,7 +1168,7 @@ func main() {
func Reverse[T any](slice []T)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1198,7 +1198,7 @@ func main() {
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1232,7 +1232,7 @@ func main() {
func Replace[T comparable](slice []T, old T, new T, n int) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1274,7 +1274,7 @@ func main() {
func ReplaceAll[T comparable](slice []T, old T, new T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1302,7 +1302,7 @@ func main() {
func Repeat[T any](item T, n int) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1330,7 +1330,7 @@ func main() {
func Shuffle[T any](slice []T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1359,7 +1359,7 @@ func main() {
func Sort[T constraints.Ordered](slice []T, sortOrder ...string)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1396,7 +1396,7 @@ func main() {
func SortBy[T any](slice []T, less func(a, b T) bool)
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1443,7 +1443,7 @@ func main() {
func SortByField(slice any, field string, sortType ...string) error
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1484,7 +1484,7 @@ func main() {
func Some[T any](slice []T, predicate func(index int, item T) bool) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1518,7 +1518,7 @@ func main() {
func StringSlice(slice any) []string
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1547,7 +1547,7 @@ func main() {
func SymmetricDifference[T comparable](slices ...[]T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1578,7 +1578,7 @@ func main() {
func ToSlice[T any](items ...T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1606,7 +1606,7 @@ func main() {
func ToSlicePointer[T any](items ...T) []*T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1641,7 +1641,7 @@ func main() {
func Unique[T comparable](slice []T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1668,7 +1668,7 @@ func main() {
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1699,7 +1699,7 @@ func main() {
func Union[T comparable](slices ...[]T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1730,7 +1730,7 @@ func main() {
func UnionBy[T any, V comparable](predicate func(item T) V, slices ...[]T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1763,7 +1763,7 @@ func main() {
func UpdateAt[T any](slice []T, index int, value T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1803,7 +1803,7 @@ func main() {
func Without[T comparable](slice []T, items ...T) []T
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -1831,7 +1831,7 @@ func main() {
func KeyBy[T any, U comparable](slice []T, iteratee func(item T) U) map[U]T
```
<b>例:</b>
<b>例:</b>
```go
import (

View File

@@ -289,7 +289,7 @@ func main() {
func GetOsBits() int
```
<b>例:</b>
<b>例:</b>
```go
import (

View File

@@ -65,7 +65,7 @@ import (
func ContainChinese(s string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -99,7 +99,7 @@ func main() {
func ContainLetter(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -133,7 +133,7 @@ func main() {
func ContainLower(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -167,7 +167,7 @@ func main() {
func ContainUpper(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -201,7 +201,7 @@ func main() {
func IsAlpha(s string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -235,7 +235,7 @@ func main() {
func IsAllUpper(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -269,7 +269,7 @@ func main() {
func IsAllLower(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -303,7 +303,7 @@ func main() {
func IsBase64(base64 string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -334,7 +334,7 @@ func main() {
func IsChineseMobile(mobileNum string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -365,7 +365,7 @@ func main() {
func IsChineseIdNum(id string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -396,7 +396,7 @@ func main() {
func IsChinesePhone(phone string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -427,7 +427,7 @@ func main() {
func IsCreditCard(creditCart string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -458,7 +458,7 @@ func main() {
func IsDns(dns string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -492,7 +492,7 @@ func main() {
func IsEmail(email string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -523,7 +523,7 @@ func main() {
func IsEmptyString(s string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -557,7 +557,7 @@ func main() {
func IsFloatStr(s string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -594,7 +594,7 @@ func main() {
func IsNumberStr(s string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -631,7 +631,7 @@ func main() {
func IsJSON(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -668,7 +668,7 @@ func main() {
func IsRegexMatch(s, regex string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -699,7 +699,7 @@ func main() {
func IsIntStr(s string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -736,7 +736,7 @@ func main() {
func IsIp(ipstr string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -773,7 +773,7 @@ func main() {
func IsIpV4(ipstr string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -804,7 +804,7 @@ func main() {
func IsIpV6(ipstr string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -835,7 +835,7 @@ func main() {
func IsStrongPassword(password string, length int) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -866,7 +866,7 @@ func main() {
func IsUrl(str string) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -901,7 +901,7 @@ func main() {
func IsWeakPassword(password string, length int) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -932,7 +932,7 @@ func main() {
func IsZeroValue(value any) bool
```
<b>例:</b>
<b>例:</b>
```go
import (
@@ -969,7 +969,7 @@ func main() {
func IsGBK(data []byte) bool
```
<b>例:</b>
<b>例:</b>
```go
import (

View File

@@ -38,7 +38,7 @@ import (
func Unwrap[T any](val T, err error) T
```
<b>例:</b>
<b>例:</b>
```go
package main

261
stream/stream.go Normal file
View File

@@ -0,0 +1,261 @@
// Copyright 2023 dudaodong@gmail.com. All rights resulterved.
// Use of this source code is governed by MIT license
// Package stream implements a sequence of elements supporting sequential and parallel aggregate operations.
// this package is an experiment to explore if stream in go can work as the way java does. it's complete, but not
// powerful like other libs
package stream
import (
"bytes"
"encoding/gob"
"golang.org/x/exp/constraints"
)
// A stream should implements methods:
// type StreamI[T any] interface {
// // part methods of Java Stream Specification.
// Distinct() StreamI[T]
// Filter(predicate func(item T) bool) StreamI[T]
// FlatMap(mapper func(item T) StreamI[T]) StreamI[T]
// Map(mapper func(item T) T) StreamI[T]
// Peek(consumer func(item T)) StreamI[T]
// Sort(less func(a, b T) bool) StreamI[T]
// Max(less func(a, b T) bool) (T, bool)
// Min(less func(a, b T) bool) (T, bool)
// Limit(maxSize int) StreamI[T]
// Skip(n int) StreamI[T]
// AllMatch(predicate func(item T) bool) bool
// AnyMatch(predicate func(item T) bool) bool
// NoneMatch(predicate func(item T) bool) bool
// ForEach(consumer func(item T))
// Reduce(init T, accumulator func(a, b T) T) T
// Count() int
// FindFirst() (T, bool)
// ToSlice() []T
// // part of methods custom extension
// Reverse() StreamI[T]
// Range(start, end int64) StreamI[T]
// Concat(streams ...StreamI[T]) StreamI[T]
// }
type stream[T any] struct {
source []T
}
// Of creates a stream stream whose elements are the specified values.
func Of[T any](elems ...T) stream[T] {
return FromSlice(elems)
}
// Generate stream where each element is generated by the provided generater function
// generater function: func() func() (item T, ok bool) {}
func Generate[T any](generator func() func() (item T, ok bool)) stream[T] {
source := make([]T, 0)
var zeroValue T
for next, item, ok := generator(), zeroValue, true; ok; {
item, ok = next()
if ok {
source = append(source, item)
}
}
return FromSlice(source)
}
// FromSlice create stream from slice.
func FromSlice[T any](source []T) stream[T] {
return stream[T]{source: source}
}
// FromChannel create stream from channel.
func FromChannel[T any](source <-chan T) stream[T] {
s := make([]T, 0)
for v := range source {
s = append(s, v)
}
return FromSlice(s)
}
// FromRange create a number stream from start to end. both start and end are included. [start, end]
func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T] {
if end < start {
panic("stream.FromRange: param start should be before param end")
} else if step <= 0 {
panic("stream.FromRange: param step should be positive")
}
l := int((end-start)/step) + 1
source := make([]T, l, l)
for i := 0; i < l; i++ {
source[i] = start + (T(i) * step)
}
return FromSlice(source)
}
// Distinct returns a stream that removes the duplicated items.
func (s stream[T]) Distinct() stream[T] {
source := make([]T, 0)
distinct := map[string]bool{}
for _, v := range s.source {
// todo: performance issue
k := hashKey(v)
if _, ok := distinct[k]; !ok {
distinct[k] = true
source = append(source, v)
}
}
return FromSlice(source)
}
func hashKey(data any) string {
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(data)
if err != nil {
panic("stream.hashKey: get hashkey failed")
}
return buffer.String()
}
// Filter returns a stream consisting of the elements of this stream that match the given predicate.
func (s stream[T]) Filter(predicate func(item T) bool) stream[T] {
source := make([]T, 0)
for _, v := range s.source {
if predicate(v) {
source = append(source, v)
}
}
return FromSlice(source)
}
// Map returns a stream consisting of the elements of this stream that apply the given function to elements of stream.
func (s stream[T]) Map(mapper func(item T) T) stream[T] {
source := make([]T, s.Count(), s.Count())
for i, v := range s.source {
source[i] = mapper(v)
}
return FromSlice(source)
}
// Peek returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.
func (s stream[T]) Peek(consumer func(item T)) stream[T] {
for _, v := range s.source {
consumer(v)
}
return s
}
// Skip returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.
// If this stream contains fewer than n elements then an empty stream will be returned.
func (s stream[T]) Skip(n int) stream[T] {
if n <= 0 {
return s
}
source := make([]T, 0)
l := len(s.source)
if n > l {
return FromSlice(source)
}
for i := n; i < l; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// Limit returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
func (s stream[T]) Limit(maxSize int) stream[T] {
if s.source == nil {
return s
}
if maxSize < 0 {
return FromSlice([]T{})
}
source := make([]T, 0, maxSize)
for i := 0; i < len(s.source) && i < maxSize; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// AllMatch returns whether all elements of this stream match the provided predicate.
func (s stream[T]) AllMatch(predicate func(item T) bool) bool {
for _, v := range s.source {
if !predicate(v) {
return false
}
}
return true
}
// AnyMatch returns whether any elements of this stream match the provided predicate.
func (s stream[T]) AnyMatch(predicate func(item T) bool) bool {
for _, v := range s.source {
if predicate(v) {
return true
}
}
return false
}
// NoneMatch returns whether no elements of this stream match the provided predicate.
func (s stream[T]) NoneMatch(predicate func(item T) bool) bool {
return !s.AnyMatch(predicate)
}
// ForEach performs an action for each element of this stream.
func (s stream[T]) ForEach(action func(item T)) {
for _, v := range s.source {
action(v)
}
}
// Reduce performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
func (s stream[T]) Reduce(init T, accumulator func(a, b T) T) T {
for _, v := range s.source {
init = accumulator(init, v)
}
return init
}
// Count returns the count of elements in the stream.
func (s stream[T]) Count() int {
return len(s.source)
}
// ToSlice return the elements in the stream.
func (s stream[T]) ToSlice() []T {
return s.source
}

249
stream/stream_test.go Normal file
View File

@@ -0,0 +1,249 @@
package stream
import (
"fmt"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestOf(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
stream := Of(1, 2, 3)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestGenerate(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
n := 0
max := 4
generator := func() func() (int, bool) {
return func() (int, bool) {
n++
return n, n < max
}
}
stream := Generate(generator)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestFromSlice")
stream := FromSlice([]int{1, 2, 3})
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromChannel(t *testing.T) {
assert := internal.NewAssert(t, "TestFromChannel")
ch := make(chan int)
go func() {
for i := 1; i < 4; i++ {
ch <- i
}
close(ch)
}()
stream := FromChannel(ch)
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
}
func TestFromRange(t *testing.T) {
assert := internal.NewAssert(t, "TestFromRange")
s1 := FromRange(1, 5, 1)
s2 := FromRange(1.1, 5.0, 1.0)
assert.Equal([]int{1, 2, 3, 4, 5}, s1.ToSlice())
assert.Equal([]float64{1.1, 2.1, 3.1, 4.1}, s2.ToSlice())
}
func TestStream_Distinct(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Distinct")
nums := FromSlice([]int{1, 2, 2, 3, 3, 3})
distinctNums := nums.Distinct()
assert.Equal([]int{1, 2, 2, 3, 3, 3}, nums.ToSlice())
assert.Equal([]int{1, 2, 3}, distinctNums.ToSlice())
type Person struct {
Id string
Name string
Age uint
}
people := []Person{
{Id: "001", Name: "Tom", Age: 10},
{Id: "001", Name: "Tom", Age: 10},
{Id: "002", Name: "Jim", Age: 20},
{Id: "003", Name: "Mike", Age: 30},
}
stream := FromSlice(people)
distinctStream := stream.Distinct()
// {[{001 Tom 10} {001 Tom 10} {002 Jim 20} {003 Mike 30}]}
t.Log(stream)
// {[{001 Tom 10} {002 Jim 20} {003 Mike 30}]}
t.Log(distinctStream)
}
func TestStream_Filter(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Filter")
stream := FromSlice([]int{1, 2, 3, 4, 5})
isEven := func(n int) bool {
return n%2 == 0
}
even := stream.Filter(isEven)
assert.Equal([]int{2, 4}, even.ToSlice())
}
func TestStream_Map(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Map")
stream := FromSlice([]int{1, 2, 3})
addOne := func(n int) int {
return n + 1
}
s := stream.Map(addOne)
assert.Equal([]int{2, 3, 4}, s.ToSlice())
}
func TestStream_Peek(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Peek")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
result := []string{}
stream = stream.Filter(func(n int) bool {
return n <= 3
}).Peek(func(n int) {
result = append(result, fmt.Sprint("current: ", n))
})
assert.Equal([]int{1, 2, 3}, stream.ToSlice())
assert.Equal([]string{
"current: 1", "current: 2", "current: 3",
}, result)
}
func TestStream_Skip(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Peek")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
s1 := stream.Skip(-1)
s2 := stream.Skip(0)
assert.Equal([]int{1, 2, 3, 4, 5, 6}, s1.ToSlice())
assert.Equal([]int{1, 2, 3, 4, 5, 6}, s2.ToSlice())
}
func TestStream_Limit(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Limit")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
s1 := stream.Limit(-1)
s2 := stream.Limit(0)
s3 := stream.Limit(1)
s4 := stream.Limit(6)
assert.Equal([]int{}, s1.ToSlice())
assert.Equal([]int{}, s2.ToSlice())
assert.Equal([]int{1}, s3.ToSlice())
assert.Equal([]int{1, 2, 3, 4, 5, 6}, s4.ToSlice())
}
func TestStream_AllMatch(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_AllMatch")
stream := FromSlice([]int{1, 2, 3, 4, 5, 6})
result1 := stream.AllMatch(func(item int) bool {
return item > 0
})
result2 := stream.AllMatch(func(item int) bool {
return item > 1
})
assert.Equal(true, result1)
assert.Equal(false, result2)
}
func TestStream_AnyMatch(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_AnyMatch")
stream := FromSlice([]int{1, 2, 3})
result1 := stream.AnyMatch(func(item int) bool {
return item > 3
})
result2 := stream.AnyMatch(func(item int) bool {
return item > 1
})
assert.Equal(false, result1)
assert.Equal(true, result2)
}
func TestStream_NoneMatch(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_NoneMatch")
stream := FromSlice([]int{1, 2, 3})
result1 := stream.NoneMatch(func(item int) bool {
return item > 3
})
result2 := stream.NoneMatch(func(item int) bool {
return item > 1
})
assert.Equal(true, result1)
assert.Equal(false, result2)
}
func TestStream_ForEach(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_ForEach")
stream := FromSlice([]int{1, 2, 3})
result := 0
stream.ForEach(func(item int) {
result += item
})
assert.Equal(6, result)
}
func TestStream_Reduce(t *testing.T) {
assert := internal.NewAssert(t, "TestStream_Reduce")
stream := FromSlice([]int{1, 2, 3})
result := stream.Reduce(0, func(a, b int) int {
return a + b
})
assert.Equal(6, result)
}