Documentation
¶
Overview ¶
Package view provides customisable abstractions over collections. Changes to an underlying collection are reflected in its views.
Example ¶
package main import ( "fmt" "strings" "github.com/tawesoft/golib/v2/view" ) func main() { // original map of lowercase animals to sounds quacks := map[string]string{ "cat": "meow", "dog": "woof", "cow": "moo", "duck": "quack", "duckling": "quack", } // define a dynamic mapping that only shows things that quack and also // shows names in title-case and emotes in uppercase with an exclamation // mark... onlyQuackers := func(k string, v string) bool { return v == "quack" } keyer := view.Key(strings.Title, strings.ToLower) valuer := view.Valuer[string, string]{ To: func(x string) string { return strings.ToUpper(x) + "!" }, From: func(x string) string { return strings.ToLower(x)[0 : len(x)-1] }, } titleCaseOnlyQuackers := view.FromMap(quacks, onlyQuackers, keyer, valuer) // Update the view, which also updates the original titleCaseOnlyQuackers.Set("Duck impersonator", "QUACK!") // Update the original, which also updates the view quacks["another duckling"] = "quack" // Iterate over the view fmt.Printf("View:\n") it := titleCaseOnlyQuackers.Iter() for { quacker, ok := it.Next() if !ok { break } fmt.Printf("%s: %s\n", quacker.Key, quacker.Value) } fmt.Printf("\nOriginal (modified):\n") for k, v := range quacks { fmt.Printf("%s: %s\n", k, v) } /* Prints something like: View: Duckling: QUACK! Duck Impersonator: QUACK! Another Duckling: QUACK! Duck: QUACK! Original (modified): duck: quack duckling: quack duck impersonator: quack another duckling: quack cat: meow dog: woof cow: moo */ }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Keyer ¶
type Keyer[A comparable, B comparable] struct { To func(A) B From func(B) A }
Keyer defines a mapping between comparable types A and B.
func Key ¶
func Key[A comparable, B comparable](AtoB func(A) B, BtoA func(B) A) Keyer[A, B]
Key is a shorthand to create a new Keyer.
type Mapper ¶
type Mapper[K comparable, V any, ToK comparable, ToV any] struct { // Filterer defines a function that "hides" the given value from the view // when accessed through Get or Iter. It is implemented in terms of the // types of the underlying collection, and should not implement the keyer // or valuer logic. Filterer func(K, V) bool // Getter defines a function that accesses a given value from the // underlying collection. It is implemented in terms of the types of the // underlying collection, and should not implement the filtering, keyer or // valuer logic. Getter func(K) (V, bool) // Setter defines a function that accesses a given value from the // underlying collection. It is implemented in terms of the types of the // underlying collection, and should not implement the filtering, keyer or // valuer logic. Note that a Setter ignores filtering. Setter func(K, V) // Deleter defines a function that deletes a value in the underlying // collection. It is implemented in terms of the types of the // underlying collection, and should not implement the filtering, keyer or // valuer logic. Note that a Deleter ignores filtering. Deleter func(K) // Keyer defines a mapping to and from the keys in the underlying // collection to the view. In the case of a list, the keys will be simple // integers. Keyer Keyer[K, ToK] // Valuer defines a mapping to and from the values in the underlying // collection to the view. Valuer Valuer[V, ToV] // Iterer defines a function that returns a new iterator over the // underlying collection. It is implemented in terms of the types of the // underlying collection, and should not implement the filtering, keyer or // valuer logic. Iterer func() Iter[Pair[K, V]] }
Mapper defines how a collection is mapped to and from a view. Call the View method to get a new view based on this map.
type Pair ¶
type Pair[K comparable, V any] struct { Key K Value V }
type View ¶
type View[K comparable, V any] interface { Get(K) (V, bool) Set(K, V) Delete(K) Iter() Iter[Pair[K, V]] }
Click to show internal directories.
Click to hide internal directories.