vals

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2018 License: BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package vals contains basic facilities for manipulating values used in the Elvish runtime.

Index

Constants

View Source
const NoPretty = util.MinInt

NoPretty can be passed to Repr to suppress pretty-printing.

Variables

View Source
var EmptyList = vector.Empty

EmptyList is an empty list.

View Source
var EmptyMap = hashmap.New(Equal, Hash)

EmptyMap is an empty map.

View Source
var ErrConcatNotImplemented = errors.New("cat not implemented")

ErrConcatNotImplemented is a special error value used to signal that concatenation is not implemented. See Concat for how it is used.

View Source
var (
	ErrIndexMustBeString = errors.New("index must be string")
)
View Source
var ErrOnlyStrOrRat = errors.New("only str or rat may be converted to rat")

Functions

func Assoc

func Assoc(a, k, v interface{}) (interface{}, error)

Assoc takes a container, a key and value, and returns a modified version of the container, in which the key associated with the value. It is implemented for the builtin type string, and types satisfying the listAssocable, mapAssocable or Assocer interface. For other types, it returns an error.

func Bool

func Bool(v interface{}) bool

Bool converts a value to bool. It is implemented for the builtin bool type and types implementing the Booler interface. For all other values, it returns true.

func Collect

func Collect(it interface{}) ([]interface{}, error)

Collect collects all elements of an iterable value into a slice.

func Concat

func Concat(lhs, rhs interface{}) (interface{}, error)

Concat concatenates two values. If both operands are strings, it returns lhs + rhs, nil. If the left operand implements Concatter, it calls lhs.Concat(rhs). If lhs doesn't implement the interface or returned ErrConcatNotImplemented, it then calls rhs.RConcat(lhs). If all attempts fail, it returns nil and an error.

func Dissoc

func Dissoc(a, k interface{}) interface{}

Dissoc takes a container and a key, and returns a modified version of the container, with the given key dissociated with any value. It is implemented for types satisfying the mapDissocable or Dissocer interface. For other types, it returns nil.

func Equal

func Equal(x, y interface{}) bool

Equal returns whether two values are equal. It is implemented for the builtin types bool and string, and types satisfying the listEqualable, mapEqualable or Equaler interface. For other types, it uses reflect.DeepEqual to compare the two values.

func Feed

func Feed(f func(interface{}) bool, values ...interface{})

Feed calls the function with given values, breaking earlier if the function returns false.

func FromGo

func FromGo(a interface{}) interface{}

FromGo converts a Go value to an Elvish value. Conversion happens when the argument is int, float64 or rune (this is consistent with ScanToGo). In other cases, this function just returns the argument.

func Hash

func Hash(v interface{}) uint32

Hash returns the 32-bit hash of a value. It is implemented for the builtin types bool and string, and types satisfying the listHashable, mapHashable or Hasher interface. For other values, it returns 0 (which is OK in terms of correctness).

func Index

func Index(a, k interface{}) (interface{}, error)

Index indexes a value with the given key. It is implemented for types satisfying the ErrIndexer or Indexer interface, types satisfying the listIndexable interface (which covers Vector), and the builtin string type. For other types, it returns a nil value and a non-nil error.

func Iterate

func Iterate(v interface{}, f func(interface{}) bool) error

Iterate iterates the supplied value, and calls the supplied function in each of its elements. The function can return false to break the iteration. It is implemented for the builtin type string, and types satisfying the listIterable or Iterator interface. For these types, it always returns a nil error. For other types, it doesn't do anything and returns an error.

func IterateKeys

func IterateKeys(v interface{}, f func(interface{}) bool) error

IterateKeys iterates the keys of the supplied value, calling the supplied function for each key. The function can return false to break the iteration. It is implemented for the mapKeysIterable type and types satisfying the IterateKeyser interface. For these types, it always returns a nil error. For other types, it doesn't do anything and returns an error.

func Kind

func Kind(v interface{}) string

Kind returns the "kind" of the value, a concept similar to type but not yet very well defined. It is implemented for the builtin types bool and string, the Vector and Map types, and types implementing the Kinder interface. For other types, it returns the Go type name of the argument preceeded by "!!".

func Len

func Len(v interface{}) int

Len returns the length of the value, or -1 if the value does not have a well-defined length. It is implemented for the builtin string type and types satisfying the Lener interface. For other types, it returns -1.

func MakeList

func MakeList(vs ...interface{}) vector.Vector

MakeList creates a new List from values.

func MakeMap

func MakeMap(raw map[interface{}]interface{}) hashmap.Map

MakeMap converts a native Go map to Map.

func MakeMapFromKV

func MakeMapFromKV(a ...interface{}) hashmap.Map

MakeMapFromKV creates a map from arguments that are alternately keys and values. It panics if the number of arguments is odd.

func MakeStringList

func MakeStringList(vs ...string) vector.Vector

MakeList creates a new List from strings.

func NoSuchKey

func NoSuchKey(k interface{}) error

NoSuchKey returns an error indicating that a key is not found in a map-like value.

func Repr

func Repr(v interface{}, indent int) string

Repr returns the representation for a value, a string that is preferably (but not necessarily) an Elvish expression that evaluates to the argument. If indent >= 0, the representation is pretty-printed. It is implemented for the builtin types bool and string, and types satisfying the listReprable, mapReprable or Reprer interface. For other types, it uses fmt.Sprint with the format "<unknown %v>".

func ScanToGo

func ScanToGo(src interface{}, ptr interface{}) error

ScanToGo converts an Elvish value to a Go value. the pointer points to. It uses the type of the pointer to determine the destination type, and puts the converted value in the location the pointer points to. Conversion only happens when the destination type is int, float64 or rune; in other cases, this function just checks that the source value is already assignable to the destination.

func ToString

func ToString(v interface{}) string

ToString converts a Value to string. When the Value type implements String(), it is used. Otherwise Repr(NoPretty) is used.

Types

type Assocer

type Assocer interface {
	// Assoc returns a slightly modified version of the receiver with key k
	// associated with value v.
	Assoc(k, v interface{}) (interface{}, error)
}

Assocer wraps the Assoc method.

type Booler

type Booler interface {
	// Bool computes the truth value of the receiver.
	Bool() bool
}

Booler wraps the Bool method.

type Concatter

type Concatter interface {
	// Concat concatenates the receiver with another value, the receiver being
	// the left operand. If concatenation is not supported for the given value,
	// the method can return the special error type ErrCatNotImplemented.
	Concat(v interface{}) (interface{}, error)
}

Concatter wraps the Concat method. See Concat for how it is used.

type Dissocer

type Dissocer interface {
	// Dissoc returns a slightly modified version of the receiver with key k
	// dissociated with any value.
	Dissoc(k interface{}) interface{}
}

Dissocer wraps the Dissoc method.

type Equaler

type Equaler interface {
	// Equal compares the receiver to another value. Two equal values must have
	// the same hash code.
	Equal(other interface{}) bool
}

Equaler wraps the Equal method.

type ErrIndexer

type ErrIndexer interface {
	// Index retrieves one value from the receiver at the specified index.
	Index(k interface{}) (interface{}, error)
}

ErrIndexer wraps the Index method.

type File

type File struct {
	Inner *os.File
}

File wraps a pointer to os.File.

func NewFile

func NewFile(inner *os.File) File

NewFile creates a new File value.

func (File) Equal

func (f File) Equal(rhs interface{}) bool

func (File) Hash

func (f File) Hash() uint32

func (File) Kind

func (File) Kind() string

func (File) Repr

func (f File) Repr(int) string

type Hasher

type Hasher interface {
	// Hash computes the hash code of the receiver.
	Hash() uint32
}

Hasher wraps the Hash method.

type Indexer

type Indexer interface {
	// Index retrieves the value corresponding to the specified key in the
	// container. It returns the value (if any), and whether it actually exists.
	Index(k interface{}) (v interface{}, ok bool)
}

Indexer wraps the Index method.

type Iterator

type Iterator interface {
	// Iterate calls the passed function with each value within the receiver.
	// The iteration is aborted if the function returns false.
	Iterate(func(v interface{}) bool)
}

Iterator wraps the Iterate method.

type KeysIterator

type KeysIterator interface {
	// IterateKeys calls the passed function with each key within the receiver.
	// The iteration is aborted if the function returns false.
	IterateKeys(func(v interface{}) bool)
}

KeysIterator wraps the IterateKeys method.

type Kinder

type Kinder interface {
	Kind() string
}

Kinder wraps the Kind method.

type Lener

type Lener interface {
	// Len computes the length of the receiver.
	Len() int
}

Lener wraps the Len method.

type ListIndex

type ListIndex struct {
	Slice bool
	Lower int
	Upper int
}

ListIndex represents a (converted) list index.

func ConvertListIndex

func ConvertListIndex(rawIndex interface{}, n int) (*ListIndex, error)

ConvertListIndex parses a list index, check whether it is valid, and returns the converted structure.

type ListReprBuilder

type ListReprBuilder struct {
	Indent int
	// contains filtered or unexported fields
}

ListReprBuilder helps to build Repr of list-like Values.

func (*ListReprBuilder) String

func (b *ListReprBuilder) String() string

func (*ListReprBuilder) WriteElem

func (b *ListReprBuilder) WriteElem(v string)

type MapReprBuilder

type MapReprBuilder struct {
	ListReprBuilder
}

MapReprBuilder helps building the Repr of a Map. It is also useful for implementing other Map-like values. The zero value of a MapReprBuilder is ready to use.

func (*MapReprBuilder) String

func (b *MapReprBuilder) String() string

func (*MapReprBuilder) WritePair

func (b *MapReprBuilder) WritePair(k string, indent int, v string)

type Pipe

type Pipe struct {
	ReadEnd, WriteEnd *os.File
}

Pipe wraps a pair of pointers to os.File that are the two ends of the same pipe.

func NewPipe

func NewPipe(r, w *os.File) Pipe

NewPipe creates a new Pipe value.

func (Pipe) Equal

func (p Pipe) Equal(rhs interface{}) bool

func (Pipe) Hash

func (p Pipe) Hash() uint32

func (Pipe) Kind

func (Pipe) Kind() string

func (Pipe) Repr

func (p Pipe) Repr(int) string

type RConcatter

type RConcatter interface {
	RConcat(v interface{}) (interface{}, error)
}

RConcatter wraps the RConcat method. See Concat for how it is used.

type Rat

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

Rat is a rational number.

func ToRat

func ToRat(v interface{}) (Rat, error)

ToRat converts a Value to rat. A str can be converted to a rat if it can be parsed. A rat is returned as-is. Other types of values cannot be converted.

func (Rat) Equal

func (r Rat) Equal(a interface{}) bool

func (Rat) Hash

func (r Rat) Hash() uint32

func (Rat) Kind

func (Rat) Kind() string

func (Rat) Repr

func (r Rat) Repr(int) string

func (Rat) String

func (r Rat) String() string

type Reprer

type Reprer interface {
	// Repr returns a string that represents a Value. The string either be a
	// literal of that Value that is preferably deep-equal to it (like `[a b c]`
	// for a list), or a string enclosed in "<>" containing the kind and
	// identity of the Value(like `<fn 0xdeadcafe>`).
	//
	// If indent is at least 0, it should be pretty-printed with the current
	// indentation level of indent; the indent of the first line has already
	// been written and shall not be written in Repr. The returned string
	// should never contain a trailing newline.
	Repr(indent int) string
}

Reprer wraps the Repr method.

type Scanner

type Scanner interface {
	ScanElvish(interface{}) error
}

Scanner is implemented by types that can scan an Elvish value into itself.

type Stringer

type Stringer interface {
	// Stringer converts the receiver to a string.
	String() string
}

Stringer wraps the String method.

type Struct

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

Struct is like a Map with fixed keys.

func NewStruct

func NewStruct(descriptor *StructDescriptor, fields []interface{}) *Struct

NewStruct creates a new *Struct value.

func (*Struct) Assoc

func (s *Struct) Assoc(k, v interface{}) (interface{}, error)

func (*Struct) Equal

func (s *Struct) Equal(rhs interface{}) bool

Equal returns true if the rhs is MapLike and all pairs are equal.

func (*Struct) HasKey

func (s *Struct) HasKey(k interface{}) bool

func (*Struct) Hash

func (s *Struct) Hash() uint32

func (*Struct) Index

func (s *Struct) Index(k interface{}) (interface{}, bool)

func (*Struct) IterateKey

func (s *Struct) IterateKey(f func(interface{}) bool)

func (*Struct) IteratePair

func (s *Struct) IteratePair(f func(interface{}, interface{}) bool)

func (*Struct) Kind

func (*Struct) Kind() string

func (*Struct) Len

func (s *Struct) Len() int

func (*Struct) MarshalJSON

func (s *Struct) MarshalJSON() ([]byte, error)

MarshalJSON encodes the Struct to a JSON Object.

func (*Struct) Repr

func (s *Struct) Repr(indent int) string

type StructDescriptor

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

StructDescriptor contains information about the fields in a Struct.

func NewStructDescriptor

func NewStructDescriptor(fields ...string) *StructDescriptor

NewStructDescriptor creates a new struct descriptor from a list of field names.

Jump to

Keyboard shortcuts

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