Add filter.Keys

pull/2/merge
rwxrob 2 years ago
parent f970c86c8e
commit d58d18ec83
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -3,7 +3,10 @@
package filter
import "fmt"
import (
"fmt"
"sort"
)
// Println prints ever element of the set.
func Println[T P](set []T) {
@ -11,3 +14,13 @@ func Println[T P](set []T) {
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
}

@ -3,7 +3,11 @@
package filter_test
import "github.com/rwxrob/bonzai/filter"
import (
"fmt"
"github.com/rwxrob/bonzai/filter"
)
func ExamplePrintln() {
set := []string{"doe", "ray", "mi"}
@ -18,3 +22,13 @@ func ExamplePrintln() {
// 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]
}

Loading…
Cancel
Save