view

package
v2.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2022 License: MIT Imports: 2 Imported by: 0

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 Iter

type Iter[T any] interface {
	Next() (T, bool)
}

func IterPair

func IterPair[K comparable, V any](it iter.It[iter.Item[K, V]]) Iter[Pair[K, V]]

IterPair returns an Iter of Pair elements from an iter.It of iter.Item elements.

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.

func (Mapper[K, V, ToK, ToV]) View

func (m Mapper[K, V, ToK, ToV]) View() View[ToK, ToV]

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

type Valuer

type Valuer[A any, B any] struct {
	To   func(A) B
	From func(B) A
}

Valuer defines a mapping between any types A and B.

func Value

func Value[A any, B any](AtoB func(A) B, BtoA func(B) A) Valuer[A, B]

Value is a shorthand to create a new Valuer.

type View

type View[K comparable, V any] interface {
	Get(K) (V, bool)
	Set(K, V)
	Delete(K)
	Iter() Iter[Pair[K, V]]
}

func FromMap

func FromMap[K comparable, V any, ToK comparable, ToV any](
	m map[K]V,
	filterer func(K, V) bool,
	Keyer Keyer[K, ToK],
	Valuer Valuer[V, ToV],
) View[ToK, ToV]

FromMap returns a View from a Go map type. See Mapper for details on the function arguments.

func FromSlice

func FromSlice[V any, ToV any](
	s []V,
	filterer func(int, V) bool,
	Valuer Valuer[V, ToV],
) View[int, ToV]

FromSlice returns a View from a Go list type. See Mapper for details on the function arguments.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL