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

doc: update doc

This commit is contained in:
dudaodong
2023-08-24 19:07:24 +08:00
parent 095cfc0aab
commit fbeb031b40
16 changed files with 90 additions and 2083 deletions

View File

@@ -12,6 +12,8 @@ Package algorithm implements some basic algorithm. eg. sort, search.
<div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="../api_doc.css">
## Usage
```go
@@ -51,7 +53,7 @@ import (
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/GNdv7Jg2Taj)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/GNdv7Jg2Taj)</span></b>
```go
package main
@@ -99,7 +101,7 @@ func main() {
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/G5LJiWgJJW6)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/G5LJiWgJJW6)</span></b>
```go
package main
@@ -162,7 +164,7 @@ func main() {
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/oXovbkekayS)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/oXovbkekayS)</span></b>
```go
package main
@@ -210,7 +212,7 @@ func main() {
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/3ibkszpJEu3)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/3ibkszpJEu3)</span></b>
```go
package main
@@ -258,7 +260,7 @@ func main() {
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
```
<b>Example:<span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/7Y7c1Elk3ax)</span></b>
<b>Example:<span class="run-container">[Run](https://go.dev/play/p/7Y7c1Elk3ax)</span></b>
```go
package main
@@ -306,7 +308,7 @@ func main() {
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/u6Iwa1VZS_f)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/u6Iwa1VZS_f)</span></b>
```go
package main
@@ -354,7 +356,7 @@ func main() {
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/ydinn9YzUJn)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/ydinn9YzUJn)</span></b>
```go
package main
@@ -402,7 +404,7 @@ func main() {
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/tB-Umgm0DrP)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/tB-Umgm0DrP)</span></b>
```go
package main
@@ -451,7 +453,7 @@ func main() {
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/t6MeGiUSN47)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/t6MeGiUSN47)</span></b>
```go
package main
@@ -502,7 +504,7 @@ func main() {
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/Anozfr8ZLH3)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/Anozfr8ZLH3)</span></b>
```go
package main
@@ -553,7 +555,7 @@ func main() {
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
```go
package main
@@ -596,7 +598,7 @@ func (l *LRUCache[K, V]) Delete(key K) bool
func (l *LRUCache[K, V]) Len() int
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/IsS7rgn5s3x)</span></b>
```go
package main

View File

@@ -34,13 +34,15 @@ import (
<div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="../api_doc.css">
## Documentation
### <span id="Equal">Equal</span>
<p>Checks if two values are equal or not. (check both type and value)</p>
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/wmVxR-to4lz)</span></b>
<b>Signature: <span class="run-container">[Run](https://go.dev/play/p/wmVxR-to4lz)</span></b>
```go
func Equal(left, right any) bool
@@ -89,7 +91,7 @@ func main() {
<p>Checks if two values are equal or not. (check value only)</p>
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/fxnna_LLD9u)</span></b>
<b>Signature: <span class="run-container">[Run](https://go.dev/play/p/fxnna_LLD9u)</span></b>
```go
func EqualValue(left, right any) bool
@@ -128,7 +130,7 @@ func main() {
<p>Checks if value `left` less than value `right`.</p>
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/cYh7FQQj0ne)</span></b>
<b>Signature: <span class="run-container">[Run](https://go.dev/play/p/cYh7FQQj0ne)</span></b>
```go
func LessThan(left, right any) bool
@@ -183,7 +185,7 @@ func main() {
func GreaterThan(left, right any) bool
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/9-NYDFZmIMp)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/9-NYDFZmIMp)</span></b>
```go
package main
@@ -235,7 +237,7 @@ func main() {
func LessOrEqual(left, right any) bool
```
<b>Example: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/e4T_scwoQzp)</span></b>
<b>Example: <span class="run-container">[Run](https://go.dev/play/p/e4T_scwoQzp)</span></b>
```go
package main
@@ -278,7 +280,7 @@ func main() {
<p>Checks if value `left` less greater or equal than value `right`.</p>
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/vx8mP0U8DFk)</span></b>
<b>Signature: <span class="run-container">[Run](https://go.dev/play/p/vx8mP0U8DFk)</span></b>
```go
func GreaterOrEqual(left, right any) bool
@@ -330,7 +332,7 @@ func main() {
<p>Checks if two values are equal or not within a delta.</p>
<b>Signature: <span style="display:inline-block;float:right;">[Run](https://go.dev/play/p/TuDdcNtMkjo)</span></b>
<b>Signature: <span class="run-container">[Run](https://go.dev/play/p/TuDdcNtMkjo)</span></b>
```go
func InDelta[T constraints.Integer | constraints.Float](left, right T, delta float64) bool

View File

@@ -0,0 +1,442 @@
# Concurrency
Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel.
<div STYLE="page-break-after: always;"></div>
## Source:
- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go)
<div STYLE="page-break-after: always;"></div>
## Usage:
```go
import (
"github.com/duke-git/lancet/v2/concurrency"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
### Channel
- [NewChannel](#NewChannel)
- [Bridge](#Bridge)
- [FanIn](#FanIn)
- [Generate](#Generate)
- [Or](#Or)
- [OrDone](#OrDone)
- [Repeat](#Repeat)
- [RepeatFn](#RepeatFn)
- [Take](#Take)
- [Tee](#Tee)
<div STYLE="page-break-after: always;"></div>
<link rel="stylesheet" type="text/css" href="../api_doc.css">
## Documentation
## Channel
### <span id="NewChannel">NewChannel</span>
<p>Create a Channel pointer instance.</p>
<b>Signature:</b>
```go
type Channel[T any] struct
func NewChannel[T any]() *Channel[T]
```
<b>Example: <span class="run-container">[运行](https://go.dev/play/p/7aB4KyMMp9A)</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
c := concurrency.NewChannel[int]()
}
```
### <span id="Bridge">Bridge</span>
<p>Link multiple channels into one channel until cancel the context.</p>
<b>Signature:</b>
```go
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T
```
<b>Example: <span class="run-container">[运行](https://go.dev/play/p/qmWSy1NVF-Y)</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
genVals := func() <-chan <-chan int {
out := make(chan (<-chan int))
go func() {
defer close(out)
for i := 1; i <= 5; i++ {
stream := make(chan int, 1)
stream <- i
close(stream)
out <- stream
}
}()
return out
}
for v := range c.Bridge(ctx, genVals()) {
fmt.Println(v)
}
// Output:
// 1
// 2
// 3
// 4
// 5
}
```
### <span id="FanIn">FanIn</span>
<p>Merge multiple channels into one channel until cancel the context.</p>
<b>Signature:</b>
```go
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T
```
<b>Example: <span class="run-container">[运行](https://go.dev/play/p/2VYFMexEvTm)</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
channels := make([]<-chan int, 2)
for i := 0; i < 2; i++ {
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 2)
}
chs := c.FanIn(ctx, channels...)
for v := range chs {
fmt.Println(v) //1 1 0 0 or 0 0 1 1
}
}
```
### <span id="Repeat">Repeat</span>
<p>Create channel, put values into the channel repeatly until cancel the context.</p>
<b>Signature: <span class="run-container">[运行](https://go.dev/play/p/k5N_ALVmYjE)</b>
```go
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
for v := range intStream {
fmt.Println(v)
}
// Output:
// 1
// 2
// 1
// 2
}
```
### <span id="Generate">Generate</span>
<p>Creates a channel, then put values into the channel.</p>
<b>Signature:</b>
```go
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
```
<b>Example: <span class="run-container">[运行](https://go.dev/play/p/7aB4KyMMp9A)</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
intStream := c.Generate(ctx, 1, 2, 3)
fmt.Println(<-intStream)
fmt.Println(<-intStream)
fmt.Println(<-intStream)
// Output:
// 1
// 2
// 3
}
```
### <span id="RepeatFn">RepeatFn</span>
<p>Create a channel, excutes fn repeatly, and put the result into the channel, until close context.</p>
<b>Signature: <span class="run-container">[运行](https://go.dev/play/p/4J1zAWttP85)</b>
```go
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fn := func() string {
return "hello"
}
c := concurrency.NewChannel[string]()
intStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
for v := range intStream {
fmt.Println(v)
}
// Output:
// hello
// hello
// hello
}
```
### <span id="Or">Or</span>
<p>Read one or more channels into one channel, will close when any readin channel is closed.</p>
<b>Signature: <span class="run-container">[运行](https://go.dev/play/p/Wqz9rwioPww)</b>
```go
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
sig := func(after time.Duration) <-chan any {
c := make(chan any)
go func() {
defer close(c)
time.Sleep(after)
}()
return c
}
start := time.Now()
c := concurrency.NewChannel[any]()
<-c.Or(
sig(1*time.Second),
sig(2*time.Second),
sig(3*time.Second),
)
fmt.Println("done after %v", time.Since(start)) //1.003s
}
```
### <span id="OrDone">OrDone</span>
<p>Read a channel into another channel, will close until cancel context.</p>
<b>Signature: <span class="run-container">[运行](https://go.dev/play/p/lm_GoS6aDjo)</b>
```go
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
for v := range c.OrDone(ctx, intStream) {
fmt.Println(v)
}
// Output:
// 1
// 1
// 1
}
```
### <span id="Take">Take</span>
<p>Create a channel whose values are taken from another channel with limit number.</p>
<b>Signature: <span class="run-container">[运行](https://go.dev/play/p/9Utt-1pDr2J)</b>
```go
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
numbers := make(chan int, 5)
numbers <- 1
numbers <- 2
numbers <- 3
numbers <- 4
numbers <- 5
defer close(numbers)
c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, numbers, 3)
for v := range intStream {
fmt.Println(v)
}
// Output:
// 1
// 2
// 3
}
```
### <span id="Tee">Tee</span>
<p>Split one chanel into two channels, until cancel the context.</p>
<b>Signature: <span class="run-container">[运行](https://go.dev/play/p/3TQPKnCirrP)</b>
```go
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel[int]()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 2)
ch1, ch2 := c.Tee(ctx, intStream)
for v := range ch1 {
fmt.Println(v)
fmt.Println(<-ch2)
}
// Output:
// 1
// 1
// 1
// 1
}
```

View File

@@ -0,0 +1,310 @@
# Condition
Package condition contains some functions for conditional judgment. eg. And, Or, TernaryOperator... The implementation of this package refers to the implementation of carlmjohnson's truthy package, you may find more useful information in [truthy](https://github.com/carlmjohnson/truthy), thanks carlmjohnson.
<div STYLE="page-break-after: always;"></div>
## Source:
- [https://github.com/duke-git/lancet/blob/main/condition/condition.go](https://github.com/duke-git/lancet/blob/main/condition/condition.go)
<div STYLE="page-break-after: always;"></div>
## Usage:
```go
import (
"github.com/duke-git/lancet/v2/condition"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [Bool](#Bool)
- [And](#And)
- [Or](#Or)
- [Xor](#Generate)
- [Nor](#Nor)
- [Xnor](#Xnor)
- [Nand](#Nand)
- [TernaryOperator](#TernaryOperator)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="Bool">Bool</span>
<p>Returns the truthy value of anything.<br/>
If the value's type has a Bool() bool method, the method is called and returned.<br/>
If the type has an IsZero() bool method, the opposite value is returned.<br/>
Slices and maps are truthy if they have a length greater than zero.<br/>
All other types are truthy if they are not their zero value.</p>
<b>Signature:</b>
```go
func Bool[T any](value T) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
// bool
result1 := condition.Bool(false)
result2 := condition.Bool(true)
fmt.Println(result1) // false
fmt.Println(result2) // true
// integer
result3 := condition.Bool(0)
result4 := condition.Bool(1)
fmt.Println(result3) // false
fmt.Println(result4) // true
// string
result5 := condition.Bool("")
result6 := condition.Bool(" ")
fmt.Println(result5) // false
fmt.Println(result6) // true
// slice
nums := []int{}
result7 := condition.Bool(nums)
nums = append(nums, 1, 2)
result8 := condition.Bool(nums)
fmt.Println(result7) // false
fmt.Println(result8) // true
// struct
result9 = condition.Bool(struct{}{})
fmt.Println(result8) // false
// Output:
// false
// true
// false
// true
// false
// true
// false
// true
// false
}
```
### <span id="And">And</span>
<p>Returns true if both a and b are truthy.</p>
<b>Signature:</b>
```go
func And[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.And(0, 1)) // false
fmt.Println(condition.And(0, "")) // false
fmt.Println(condition.And(0, "0")) // false
fmt.Println(condition.And(1, "0")) // true
}
```
### <span id="Or">Or</span>
<p>Returns false if neither a nor b is truthy.</p>
<b>Signature:</b>
```go
func Or[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Or(0, "")) // false
fmt.Println(condition.Or(0, 1)) // true
fmt.Println(condition.Or(0, "0")) // true
fmt.Println(condition.Or(1, "0")) // true
}
```
### <span id="Xor">Xor</span>
<p>Returns true if a or b but not both is truthy.</p>
<b>Signature:</b>
```go
func Xor[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Xor(0, 0)) // false
fmt.Println(condition.Xor(0, 1)) // true
fmt.Println(condition.Xor(1, 0)) // true
fmt.Println(condition.Xor(1, 1)) // false
}
```
### <span id="Nor">Nor</span>
<p>Returns true if neither a nor b is truthy.</p>
<b>Signature:</b>
```go
func Nor[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nor(0, 0)) // true
fmt.Println(condition.Nor(0, 1)) // false
fmt.Println(condition.Nor(1, 0)) // false
fmt.Println(condition.Nor(1, 1)) // false
}
```
### <span id="Xnor">Xnor</span>
<p>Returns true if both a and b or neither a nor b are truthy.</p>
<b>Signature:</b>
```go
func Xnor[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Xnor(0, 0)) // true
fmt.Println(condition.Xnor(0, 1)) // false
fmt.Println(condition.Xnor(1, 0)) // false
fmt.Println(condition.Xnor(1, 1)) // true
}
```
### <span id="Nand">Nand</span>
<p>Returns false if both a and b are truthy</p>
<b>Signature:</b>
```go
func Nand[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nand(0, 0)) // true
fmt.Println(condition.Nand(0, 1)) // true
fmt.Println(condition.Nand(1, 0)) // true
fmt.Println(condition.Nand(1, 1)) // false
}
```
### <span id="TernaryOperator">TernaryOperator</span>
<p>Checks the value of param `isTrue`, if true return ifValue else return elseValue</p>
<b>Signature:</b>
```go
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
conditionTrue := 2 > 1
result1 := condition.TernaryOperator(conditionTrue, 0, 1)
conditionFalse := 2 > 3
result2 := condition.TernaryOperator(conditionFalse, 0, 1)
fmt.Println(result1)
fmt.Println(result2)
// Output:
// 0
// 1
}
```