utils.go: Copy and Clone maps with filter function. Also added isIterator().

This commit is contained in:
Celestino Amoroso 2024-05-01 05:45:10 +02:00
parent aa66d07caa
commit 056d42d328

View File

@ -4,7 +4,9 @@
// utils.go
package expr
import "reflect"
import (
"reflect"
)
func isString(v any) (ok bool) {
_, ok = v.(string)
@ -44,6 +46,11 @@ func isFunctor(v any) (ok bool) {
return
}
func isIterator(v any) (ok bool) {
_, ok = v.(Iterator)
return
}
func numAsFloat(v any) (f float64) {
var ok bool
if f, ok = v.(float64); !ok {
@ -141,3 +148,23 @@ func CloneMap[K comparable, V any](source map[K]V) map[K]V {
dest := make(map[K]V, len(source))
return CopyMap(dest, source)
}
func CopyFilteredMap[K comparable, V any](dest, source map[K]V, filter func(key K) (accept bool)) map[K]V {
// fmt.Printf("--- Clone with filter %p\n", filter)
if filter == nil {
return CopyMap(dest, source)
} else {
for k, v := range source {
if filter(k) {
// fmt.Printf("\tClone var %q\n", k)
dest[k] = v
}
}
}
return dest
}
func CloneFilteredMap[K comparable, V any](source map[K]V, filter func(key K) (accept bool)) map[K]V {
dest := make(map[K]V, len(source))
return CopyFilteredMap(dest, source, filter)
}