From 7e9b0cf8727cdbac08b5633d15ae8e5fded882e5 Mon Sep 17 00:00:00 2001 From: rwxrob Date: Fri, 25 Feb 2022 21:22:08 -0500 Subject: [PATCH] Add refactored filter --- filter/filter.go | 67 +------------------------------------------ filter/filter_test.go | 39 ------------------------- 2 files changed, 1 insertion(+), 105 deletions(-) diff --git a/filter/filter.go b/filter/filter.go index 98a2881..d89b325 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -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 -} diff --git a/filter/filter_test.go b/filter/filter_test.go index d1b47d4..5187aab 100644 --- a/filter/filter_test.go +++ b/filter/filter_test.go @@ -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] -}