Add refactored filter

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

@ -1,31 +1,11 @@
// Copyright 2022 Robert S. Muhlestein.
// SPDX-License-Identifier: Apache-2.0
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
string | []byte
}
// HasPrefix filters the Text input set and returns only those elements
@ -39,48 +19,3 @@ func HasPrefix[T Text](set []T, pre string) []T {
}
return m
}
// Minus performs a set "minus" operation by returning a new set with
// the elements of the second set removed from it.
func Minus[T Text, M Text](set []T, min []M) []T {
m := []T{}
for _, i := range set {
var seen bool
for _, n := range min {
if string(n) == string(i) {
seen = true
break
}
}
if !seen {
m = append(m, i)
}
}
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
}
// Prefix returns a new slice with prefix added to each string.
func Prefix(in []string, pre string) []string {
list := []string{}
for _, i := range in {
list = append(list, pre+i)
}
return list
}

@ -17,42 +17,3 @@ func ExampleHasPrefix() {
// Output:
// [two three]
}
func ExampleMinus() {
set := []string{
"one", "two", "three", "four", "five", "six", "seven",
}
fmt.Println(filter.Minus(set, []string{"two", "four", "six"}))
// 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]
}
func ExamplePrefix() {
fmt.Println(filter.Prefix([]string{"foo", "bar"}, "my"))
// Output:
// [myfoo mybar]
}

Loading…
Cancel
Save