values

package
v0.0.0-...-f3d8a94 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2018 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

The values package provides multiple-writer, multiple-listener access to changing values. It also provides (through the Transform function and the Lens type) the facility to have multiple, mutually updating views of the same value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sender

func Sender(v Value, c interface{})

Sender sends values from v down the channel c, which must be of type T where T is v.Type(). When the Value is closed, the channel is closed.

Types

type Getter

type Getter interface {
	// Get gets the most recent value. If the Value has not been Set
	// since Get was last called, it blocks until it is.
	// When the Value has been closed and the final
	// value has been got, ok will be false and x nil.
	Get() (x interface{}, ok bool)

	// Type returns the type associated with the Getter (and its Value)
	Type() reflect.Type
}

type Lens

type Lens struct {
	// contains filtered or unexported fields
}

A Lens can transform from values of type T to values of type T1. It can be reversed to transform in the other direction, and be combined with other Lenses.

func Float64Multiply

func Float64Multiply(x float64) *Lens

Float64Multiply returns a Lens that multiplies by x.

func Float64ToInt

func Float64ToInt() *Lens

Float64ToInt returns a Lens that transforms a float64 value to the nearest int.

func Float64ToString

func Float64ToString(printf, scanf string) *Lens

Float64ToString returns a Lens which transforms from float64 to string. The given formats are used to effect the conversion; fmt.Sprintf(printf, x) is used to convert the float64 value x to string; fmt.Sscanf(s, scanf, &x) is used to scan the string s into the float64 value x.

func NewLens

func NewLens(f, finv interface{}) *Lens

NewLens creates a new Lens instance that transforms values from type T to type T1. Both f and finv must be functions; their actual signatures must be: f func(T) (T1, os.Error) finv func(T1) (T, os.Error) for some types T and T1. finv is expected to be the inverse of f. If either function receives a value that cannot be successfully converted, it should return an error.

func NewReflectiveLens

func NewReflectiveLens(f, finv func(reflect.Value) (reflect.Value, error), t, t1 reflect.Type) *Lens

NewReflectiveLens creates a Lens from two dynamically typed functions. f should convert from type t to type t1; finv should convert in the other direction. The uses for this function are fairly esoteric, and can be used to break the type safety of the Value if used without care.

func UnitFloat64ToRangedFloat64

func UnitFloat64ToRangedFloat64(lo, hi float64) *Lens

UnitFloat64ToRangedFloat64 returns a Lens that peforms a linear transformation from a float64 value in [0, 1] to a float64 value in [lo, hi].

func (*Lens) Combine

func (m *Lens) Combine(m1 *Lens) *Lens

Combine layers m1 on top of m. If m transforms from type T to T1 and m1 transforms from type T1 to T2, then the returned Lens transforms from T to T2. Combine panics if m.Type1() != m1.Type().

func (*Lens) Reverse

func (m *Lens) Reverse() *Lens

Reverse returns a lens that transforms in the opposite direction to m, from m.Type1() to m.Type()

func (*Lens) Transform

func (m *Lens) Transform(val interface{}) (interface{}, error)

Transform transforms a value of type T into a value of type T1. The value val must be assignable to T.

func (*Lens) Type

func (m *Lens) Type() reflect.Type

Type returns the type of T - the type that the Lens transforms to.

func (*Lens) Type1

func (m *Lens) Type1() reflect.Type

Type1 returns the type of T1 - the type that the Lens transforms from.

type Value

type Value interface {
	// Set changes the value. It never blocks; it will panic if
	// the value is not assignable to the Value's type.
	Set(val interface{}) error

	// Getter returns a Getter that can be used to listen
	// for changes to the value.
	Getter() Getter

	// Get gets the most recently set value. If the Value
	// has been Closed, ok will be false, but, unlike channels,
	// the value will still be the last set value.
	Get() (x interface{}, ok bool)

	// Type returns the type associated with the Value.
	Type() reflect.Type

	// Close marks the value as closed; all blocked Getters
	// will return.
	Close() error
}

A Value represents a changing value of a given type. Note that watchers should not change a Value in response to a value received from that channel - this could lead to an infinite loop.

func NewConst

func NewConst(val interface{}, t reflect.Type) Value

NewConst returns a Value of type t which always returns the value v, and gives an error when set.

func NewValue

func NewValue(initial interface{}, t reflect.Type) Value

NewValue creates a new Value with the given initial value and type. If t is nil, the type will be taken from initial; if initial is also nil, the type will be interface{}. If initial is nil, any Getter will block until a value is first set.

func Transform

func Transform(v Value, m *Lens) (v1 Value)

Transform returns a Value, v1, that mirrors an existing Value, v, by running m.Transform(x) on each value received from v.Iter(), and m.Reverse.Transform(x) on each value passed to v.Set(). m.Type() must equal v.Type(). The Type of the resulting value is m.Type1().

Jump to

Keyboard shortcuts

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