Reconsolidate filter subpackage

pull/53/head
rwxrob 2 years ago
parent a3a454350a
commit 2c74b4dd18
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -4,9 +4,30 @@
package filter
import (
"fmt"
"sort"
"strings"
)
// Number combines the primitives generally considered numbers by JSON
// and other high-level structure data representations.
type Number interface {
int16 | int32 | int64 | float32 | float64
}
// Text combines byte slice and string.
type Text interface {
[]byte | string
}
// P is for "principle" in this case. These are the types that have
// representations in JSON and other high-level structured data
// representations.
type P interface {
int16 | int32 | int64 | float32 | float64 |
[]byte | string | bool
}
// HasPrefix filters the Text input set and returns only those elements
// that have the give prefix.
func HasPrefix[T Text](set []T, pre string) []T {
@ -37,3 +58,20 @@ func Minus[T Text, M Text](set []T, min []M) []T {
}
return m
}
// Println prints ever element of the set.
func Println[T P](set []T) {
for _, i := range set {
fmt.Println(i)
}
}
// Keys returns the keys in lexicographically sorted order.
func Keys[T any](m map[string]T) []string {
keys := []string{}
for k, _ := range m {
keys = append(keys, k)
sort.Strings(keys)
}
return keys
}

@ -26,3 +26,27 @@ func ExampleMinus() {
// Output:
// [one three five seven]
}
func ExamplePrintln() {
set := []string{"doe", "ray", "mi"}
filter.Println(set)
bools := []bool{false, true, true}
filter.Println(bools)
// Output:
// doe
// ray
// mi
// false
// true
// true
}
func ExampleKeys() {
m1 := map[string]int{"two": 2, "three": 3, "one": 1}
m2 := map[string]string{"two": "two", "three": "three", "one": "one"}
fmt.Println(filter.Keys(m1))
fmt.Println(filter.Keys(m2))
// Output:
// [one three two]
// [one three two]
}

@ -1,26 +0,0 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package filter
import (
"fmt"
"sort"
)
// Println prints ever element of the set.
func Println[T P](set []T) {
for _, i := range set {
fmt.Println(i)
}
}
// Keys returns the keys in lexicographically sorted order.
func Keys[T any](m map[string]T) []string {
keys := []string{}
for k, _ := range m {
keys = append(keys, k)
sort.Strings(keys)
}
return keys
}

@ -1,34 +0,0 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package filter_test
import (
"fmt"
"github.com/rwxrob/bonzai/filter"
)
func ExamplePrintln() {
set := []string{"doe", "ray", "mi"}
filter.Println(set)
bools := []bool{false, true, true}
filter.Println(bools)
// Output:
// doe
// ray
// mi
// false
// true
// true
}
func ExampleKeys() {
m1 := map[string]int{"two": 2, "three": 3, "one": 1}
m2 := map[string]string{"two": "two", "three": "three", "one": "one"}
fmt.Println(filter.Keys(m1))
fmt.Println(filter.Keys(m2))
// Output:
// [one three two]
// [one three two]
}

@ -1,23 +0,0 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
package filter
// Number combines the primitives generally considered numbers by JSON
// and other high-level structure data representations.
type Number interface {
int16 | int32 | int64 | float32 | float64
}
// Text combines byte slice and string.
type Text interface {
[]byte | string
}
// P is for "principle" in this case. These are the types that have
// representations in JSON and other high-level structured data
// representations.
type P interface {
int16 | int32 | int64 | float32 | float64 |
[]byte | string | bool
}
Loading…
Cancel
Save