diff --git a/README.md b/README.md index a0f6167..41f1d74 100644 --- a/README.md +++ b/README.md @@ -800,20 +800,28 @@ import "github.com/duke-git/lancet/v2/function" [[play](https://go.dev/play/p/hbON-Xeyn5N)] - **Pipeline** : takes a list of functions and returns a function whose param will be passed into the functions one by one. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Pipeline)] + [[play](https://go.dev/play/p/mPdUVvj6HD6)] - **AcceptIf** : returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#AcceptIf)] + [[play](https://go.dev/play/p/XlXHHtzCf7d)] - **And** : returns a composed predicate that represents the logical AND of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#And)] + [[play](https://go.dev/play/p/dTBHJMQ0zD2)] - **Or** : returns a composed predicate that represents the logical OR of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Or)] + [[play](https://go.dev/play/p/LitCIsDFNDA)] - **Negate** : returns a predicate that represents the logical negation of this predicate. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Negate)] + [[play](https://go.dev/play/p/jbI8BtgFnVE)] - **Nor** : returns a composed predicate that represents the logical NOR of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nor)] + [[play](https://go.dev/play/p/2KdCoBEOq84)] - **Nand** : returns a composed predicate that represents the logical Nand of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Nand)] + [[play](https://go.dev/play/p/Rb-FdNGpgSO)] - **Xnor** : returns a composed predicate that represents the logical XNOR of a list of predicates. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Xnor)] + [[play](https://go.dev/play/p/FJxko8SFbqc)] - **Watcher** : Watcher is used for record code execution time. can start/stop/reset the watch timer. get the elapsed time of function execution. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)] [[play](https://go.dev/play/p/l2yrOpCLd1I)] @@ -1423,6 +1431,8 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/UzpGQptWppw)] - **SetToDefaultIf** : set elements to their default value if they match the given predicate. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#SetToDefaultIf)] + [[play](https://go.dev/play/p/9AXGlPRC0-A)] +
返回hashmap所有值的切片 (随机顺序).
+返回hashmap所有值的切片 (随机顺序)。
函数签名: @@ -306,3 +307,40 @@ func main() { ``` +### FilterByValue + +返回一个过滤后的HashMap。 如果任何值与 perdicate 函数不匹配,则返回 nil,否则返回包含选定值的 HashMap。
+ +函数签名: + +```go +func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap +``` + +示例: + +```go +package main + +import ( + "fmt" + hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" +) + +func main() { + hm := hashmap.NewHashMap() + + hm.Put("a", 1) + hm.Put("b", 2) + hm.Put("c", 3) + hm.Put("d", 4) + hm.Put("e", 5) + hm.Put("f", 6) + + filteredHM := hm.FilterByValue(func(value any) bool { + return value.(int) == 1 || value.(int) == 3 + }) + + fmt.Println(filteredHM.Size()) //2 +} +``` diff --git a/docs/api/packages/function.md b/docs/api/packages/function.md index 0d45096..770116d 100644 --- a/docs/api/packages/function.md +++ b/docs/api/packages/function.md @@ -424,7 +424,7 @@ func longRunningTask() { func And[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/dTBHJMQ0zD2) ```go package main @@ -461,7 +461,7 @@ func main() { func Or[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/LitCIsDFNDA) ```go package main @@ -496,7 +496,7 @@ func main() { func Negate[T any](predicate func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/jbI8BtgFnVE) ```go package main @@ -536,7 +536,7 @@ func main() { func Nor[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/2KdCoBEOq84) ```go package main @@ -578,7 +578,7 @@ func main() { func Nand[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/Rb-FdNGpgSO) ```go package main @@ -615,7 +615,7 @@ func main() { func Xnor[T any](predicates ...func(T) bool) func(T) bool ``` -示例: +示例:[运行](https://go.dev/play/p/FJxko8SFbqc) ```go package main @@ -652,7 +652,7 @@ func main() { func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) ``` -示例: +示例:[运行](https://go.dev/play/p/XlXHHtzCf7d) ```go package main @@ -663,9 +663,8 @@ import ( ) func main() { - - adder := AcceptIf( - And( + adder := function.AcceptIf( + function.And( func(x int) bool { return x > 10 }, func(x int) bool { diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 2b41f7a..ef05877 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -2581,7 +2581,7 @@ func main() { func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) ``` -示例: +示例:[运行](https://go.dev/play/p/9AXGlPRC0-A) ```go import ( diff --git a/docs/en/api/packages/datastructure/hashmap.md b/docs/en/api/packages/datastructure/hashmap.md index de0fd3c..c8fab3a 100644 --- a/docs/en/api/packages/datastructure/hashmap.md +++ b/docs/en/api/packages/datastructure/hashmap.md @@ -32,6 +32,7 @@ import ( - [Iterate](#Iterate) - [Keys](#Keys) - [Values](#Values) +- [FilterByValue](#FilterByValue) @@ -311,4 +312,42 @@ func main() { } ``` +### FilterByValue + +Returns a filtered HashMap.
+ +Signature: + +```go +func (hm *HashMap) FilterByValue(perdicate func(value any) bool) *HashMap +``` + +Example: + +```go +package main + +import ( + "fmt" + hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap" +) + +func main() { + hm := hashmap.NewHashMap() + + hm.Put("a", 1) + hm.Put("b", 2) + hm.Put("c", 3) + hm.Put("d", 4) + hm.Put("e", 5) + hm.Put("f", 6) + + filteredHM := hm.FilterByValue(func(value any) bool { + return value.(int) == 1 || value.(int) == 3 + }) + + fmt.Println(filteredHM.Size()) //2 +} +``` + diff --git a/docs/en/api/packages/function.md b/docs/en/api/packages/function.md index f4cf6d1..4c31cf1 100644 --- a/docs/en/api/packages/function.md +++ b/docs/en/api/packages/function.md @@ -423,7 +423,7 @@ func longRunningTask() { func And[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/dTBHJMQ0zD2) ```go package main @@ -460,7 +460,7 @@ func main() { func Or[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/LitCIsDFNDA) ```go package main @@ -495,7 +495,7 @@ func main() { func Negate[T any](predicate func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/jbI8BtgFnVE) ```go package main @@ -535,7 +535,7 @@ func main() { func Nor[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/2KdCoBEOq84) ```go package main @@ -577,7 +577,7 @@ func main() { func Nand[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/Rb-FdNGpgSO) ```go package main @@ -614,7 +614,7 @@ func main() { func Xnor[T any](predicates ...func(T) bool) func(T) bool ``` -Example: +Example:[Run](https://go.dev/play/p/FJxko8SFbqc) ```go package main @@ -643,9 +643,7 @@ func main() { ### AcceptIf -AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. -A predicate function that takes an argument of type T and returns a bool. -An apply function that also takes an argument of type T and returns a modified value of the same type.
+AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. A predicate function that takes an argument of type T and returns a bool. An apply function that also takes an argument of type T and returns a modified value of the same type.
Signature: @@ -653,7 +651,7 @@ An apply function that also takes an argument of type T and returns a modified v func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) ``` -Example: +Example:[Run](https://go.dev/play/p/XlXHHtzCf7d) ```go package main @@ -665,8 +663,8 @@ import ( func main() { - adder := AcceptIf( - And( + adder := function.AcceptIf( + function.And( func(x int) bool { return x > 10 }, func(x int) bool { diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 9c46ce5..743b5fa 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -2577,7 +2577,7 @@ func main() { func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) ``` -Example: +Example:[Run](https://go.dev/play/p/9AXGlPRC0-A) ```go import ( diff --git a/function/function.go b/function/function.go index 0840983..3100467 100644 --- a/function/function.go +++ b/function/function.go @@ -139,6 +139,7 @@ func Pipeline[T any](funcs ...func(T) T) func(T) T { // AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. // A predicate function that takes an argument of type T and returns a bool. // An apply function that also takes an argument of type T and returns a modified value of the same type. +// Play: https://go.dev/play/p/XlXHHtzCf7d func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool) { if predicate == nil { panic("programming error: predicate must be not nil") diff --git a/function/predicate.go b/function/predicate.go index 6d76d0a..c31eeb2 100644 --- a/function/predicate.go +++ b/function/predicate.go @@ -2,6 +2,7 @@ package function // And returns a composed predicate that represents the logical AND of a list of predicates. // It evaluates to true only if all predicates evaluate to true for the given value. +// Play: https://go.dev/play/p/dTBHJMQ0zD2 func And[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -18,6 +19,7 @@ func And[T any](predicates ...func(T) bool) func(T) bool { // Nand returns a composed predicate that represents the logical NAND of a list of predicates. // It evaluates to true only if all predicates evaluate to false for the given value. +// Play: https://go.dev/play/p/Rb-FdNGpgSO func Nand[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -33,6 +35,7 @@ func Nand[T any](predicates ...func(T) bool) func(T) bool { } // Negate returns a predicate that represents the logical negation of this predicate. +// Play: https://go.dev/play/p/jbI8BtgFnVE func Negate[T any](predicate func(T) bool) func(T) bool { return func(value T) bool { return !predicate(value) @@ -41,6 +44,7 @@ func Negate[T any](predicate func(T) bool) func(T) bool { // Or returns a composed predicate that represents the logical OR of a list of predicates. // It evaluates to true if at least one of the predicates evaluates to true for the given value. +// Play: https://go.dev/play/p/LitCIsDFNDA func Or[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -57,6 +61,7 @@ func Or[T any](predicates ...func(T) bool) func(T) bool { // Nor returns a composed predicate that represents the logical NOR of a list of predicates. // It evaluates to true only if all predicates evaluate to false for the given value. +// Play: https://go.dev/play/p/2KdCoBEOq84 func Nor[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") @@ -73,6 +78,7 @@ func Nor[T any](predicates ...func(T) bool) func(T) bool { // Xnor returns a composed predicate that represents the logical XNOR of a list of predicates. // It evaluates to true only if all predicates evaluate to true or false for the given value. +// Play: https://go.dev/play/p/FJxko8SFbqc func Xnor[T any](predicates ...func(T) bool) func(T) bool { if len(predicates) < 2 { panic("programming error: predicates count must be at least 2") diff --git a/slice/slice.go b/slice/slice.go index 5893a9f..c858ede 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1173,7 +1173,7 @@ func AppendIfAbsent[T comparable](slice []T, item T) []T { // SetToDefaultIf sets elements to their default value if they match the given predicate. // It retains the positions of the elements in the slice. // It returns slice of T and the count of modified slice items -// Play: todo +// Play: https://go.dev/play/p/9AXGlPRC0-A func SetToDefaultIf[T any](slice []T, predicate func(T) bool) ([]T, int) { var count int for i := 0; i < len(slice); i++ {