go-adaptive-functions: github.com/spatialcurrent/go-adaptive-functions/pkg/af Index | Examples | Files

package af

import "github.com/spatialcurrent/go-adaptive-functions/pkg/af"

Package af contains a set of adapative functions.

Code:

out, err := af.Coalesce.ValidateRun(nil, "", "foo")
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

"foo"

Code:

out, err := af.Concat.ValidateRun("foo", "bar")
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

"foobar"

Code:

out, err := af.Concat.ValidateRun("1", "2", 3)
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

"123"

Code:

in := []interface{}{
    []interface{}{
        "a",
        "b",
        "c",
        []interface{}{
            "d",
            "e",
            "f",
        },
        []interface{}{
            "g",
            "h",
            "i",
        },
        "j",
        "k",
    },
    "l",
    "m",
    "n",
}
out, err := af.Concat.ValidateRun(in)
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

"abcdefghijklmn"

Code:

in := []interface{}{
    []interface{}{"a", "b"},
    []interface{}{"c", "d"},
    []interface{}{"e", "f"},
}
out, err := af.Flat.ValidateRun(in)
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]interface {}{"a", "b", "c", "d", "e", "f"}

This examples shows the use of the iin function to test if a key exists in a map.

Code:

out, err := af.IIn.ValidateRun("A", map[string]interface{}{"a": "x", "b": "y"})
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

true

This examples shows the use of the in function to test if an element is in a slice.

Code:

out, err := af.IIn.ValidateRun("A", []string{"a", "b", "c"})
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

true

This examples shows the use of the in function checking if an IP address is in an IP range.

Code:

addr := net.ParseIP("192.0.2.100")
n := net.IPNet{IP: net.ParseIP("192.0.2.0"), Mask: net.CIDRMask(24, 32)}
out, err := af.In.ValidateRun(addr, n)
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

true

This examples shows the use of the in function to test if a key exists in a map.

Code:

out, err := af.In.ValidateRun("a", map[string]interface{}{"a": "x", "b": "y"})
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

true

This examples shows the use of the in function to test if an element is in a slice.

Code:

out, err := af.In.ValidateRun("a", []string{"a", "b", "c"})
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

true

This examples shows the use of the join function.

Code:

out, err := af.Join.ValidateRun([]interface{}{"a", "b", "c"}, ",")
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

"a,b,c"

This examples shows the use of the limit function to limit the number of elements in a slice.

Code:

out, err := af.Limit.ValidateRun([]string{"a", "b", "c"}, 2)
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]string{"a", "b"}

Code:

out, err := af.Max.ValidateRun(1, 2, 4)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

4

Code:

out, err := af.Min.ValidateRun(1, 2, 4)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

1

Code:

out, err := af.Repeat.ValidateRun("ciao", 2)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

ciaociao

Code:

out, err := af.Sort.ValidateRun([]string{"foo", "bar"})
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

[bar foo]

Code:

in := "foo,bar"
out, err := af.Split.ValidateRun(in, ",")
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]string{"foo", "bar"}

Code:

out, err := af.Sum.ValidateRun(1, 2, 4)
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

7

Code:

out, err := af.ToKeys.ValidateRun(map[string]interface{}{"a": "x"})
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]string{"a"}

Code:

out, err := af.ToLower.ValidateRun("Hello World")
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

hello world

Code:

out, err := af.ToTitle.ValidateRun("hello world")
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

Hello World

Code:

out, err := af.ToUpper.ValidateRun("Hello World")
if err != nil {
    panic(err)
}
fmt.Println(out)

Output:

HELLO WORLD

Code:

out, err := af.ToValues.ValidateRun(map[string]string{"a": "x"})
if err != nil {
    panic(err)
}
fmt.Println(fmt.Sprintf("%#v", out))

Output:

[]string{"x"}

Index

Examples

Package Files

Add.go Base.go Bottom.go BoundingBox.go Clean.go Coalesce.go Concat.go Definition.go Divide.go ErrInvalidArguments.go ErrInvalidCapacity.go ErrOutOfRange.go ErrorOverflow.go First.go Flat.go Fold.go Format.go Function.go Functions.go IIn.go In.go Intersects.go JSON.go Join.go Last.go Length.go Limit.go Lookup.go Make.go Max.go Mean.go Min.go Multiply.go Now.go Pow.go Prefix.go Repeat.go Replace.go Second.go Slice.go Sort.go Split.go Stringify.go Subtract.go Suffix.go Sum.go TestCase.go TileX.go TileY.go ToBigEndian.go ToBytes.go ToFloat32.go ToFloat64.go ToFloat64Slice.go ToInt.go ToInt16.go ToInt32.go ToInt64.go ToInt8.go ToIntSlice.go ToItems.go ToKeys.go ToLittleEndian.go ToLower.go ToSet.go ToString.go ToStringSet.go ToStringSlice.go ToTime.go ToTitle.go ToUpper.go ToValues.go Top.go Trim.go Types.go Within.go Words.go af.go

Variables

var Add = Function{
    Name:    "Add",
    Aliases: []string{"add"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Int, reflect.Int}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int, reflect.Int64}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int, reflect.Float64}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.Int}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.Int64}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.Float64}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.Int}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.Int64}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.Float64}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Int, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Int32, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, reflect.Int}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, reflect.Int32}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, reflect.Int64}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, reflect.Float64}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Array}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Slice}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Array, reflect.Array}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Array, reflect.Slice}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Map, reflect.Map}, Output: reflect.Map},
    },
    Function: add,
}
var Base = Function{
    Name:    "Base",
    Aliases: []string{"base"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType}, Output: stringType},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if str, ok := args[0].(string); ok {
            return path.Base(str), nil
        }
        return nil, &ErrInvalidArguments{Function: "Base", Arguments: args}
    },
}
var Bottom = Function{
    Name:    "Bottom",
    Aliases: []string{"bottom"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringIntMapType, intType}, Output: stringSliceType},
        Definition{Inputs: []interface{}{stringIntMapType, intType, intType}, Output: stringSliceType},
    },
    Function: bottom,
}
var BoundingBox = Function{
    Name:    "BoundingBox",
    Aliases: []string{"boundingBox", "bbox"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array}, Output: float64ArrayType},
        Definition{Inputs: []interface{}{reflect.Slice}, Output: float64ArrayType},
    },
    Function: boundingBox,
}
var Clean = Function{
    Name:    "Clean",
    Aliases: []string{"clean"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType}, Output: stringType},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if str, ok := args[0].(string); ok {
            return filepath.Clean(str), nil
        }
        return nil, &ErrInvalidArguments{Function: "Clean", Arguments: args}
    },
}
var Coalesce = Function{
    Name:        "Coalesce",
    Aliases:     []string{"coalesce"},
    Definitions: []Definition{},
    Function:    coalesce,
}
var Concat = Function{
    Name:    "Concat",
    Aliases: []string{"concat"},
    Definitions: []Definition{
        Definition{Inputs: nil, Output: reflect.String},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        return stringify.Concat(args, stringify.NewDefaultStringer())
    },
}
var Divide = Function{
    Name:        "Divide",
    Aliases:     []string{"divide"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) != 2 {
            return nil, &ErrInvalidArguments{Function: "Divide", Arguments: args}
        }
        return math.Divide(args[0], args[1])
    },
}
var First = Function{
    Name:    "First",
    Aliases: []string{"first"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{byteSliceType}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{uint8SliceType}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{intSliceType}, Output: reflect.Int},
        Definition{Inputs: []interface{}{int64ArrayType}, Output: reflect.Int64},
        Definition{Inputs: []interface{}{float64ArrayType}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{stringSliceType}, Output: reflect.String},
        Definition{Inputs: []interface{}{stringIntMapSliceType}, Output: stringIntMapType},
        Definition{Inputs: []interface{}{interfaceArrayType}, Output: nil},
        Definition{Inputs: []interface{}{reflect.String}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{reflect.Map}, Output: nil},
    },
    Function: first,
}
var Flat = Function{
    Name:        "Flat",
    Aliases:     []string{"flat", "flatten"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        return flat.Flat(args), nil
    },
}

Flat is a function that flattens an array of arrays.

var Fold = Function{
    Name:    "Fold",
    Aliases: []string{"fold"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.String}, Output: reflect.String},
    },
    Function: fold,
}
var Format = Function{
    Name:    "Format",
    Aliases: []string{"fmt", "format"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{timeType, reflect.String}, Output: reflect.String},
        Definition{Inputs: nil, Output: reflect.String},
    },
    Function: format,
}
var Functions = []Function{
    Add,
    Base,
    Bottom,
    BoundingBox,
    Clean,
    Coalesce,
    Concat,
    Divide,
    First,
    Flat,
    Fold,
    Format,
    Length,
    In,
    IIn,
    Intersects,
    Join,
    JSON,
    Last,
    Limit,
    Lookup,
    Make,
    Max,
    Mean,
    Min,
    Multiply,
    Now,
    Prefix,
    Pow,
    Repeat,
    Replace,
    Second,
    Slice,
    Split,
    Sort,
    Stringify,
    Subtract,
    Suffix,
    Sum,
    TileX,
    TileY,
    ToBigEndian,
    ToBytes,
    ToInt,
    ToInt8,
    ToInt16,
    ToInt32,
    ToInt64,
    ToIntSlice,
    ToFloat32,
    ToFloat64,
    ToFloat64Slice,
    ToItems,
    ToKeys,
    ToLittleEndian,
    ToLower,
    ToSet,
    ToString,
    ToStringSet,
    ToTitle,
    ToTime,
    Top,
    ToUpper,
    ToValues,
    Trim,
    Within,
    Words,
}

Functions is an array of all the adapative functions defined in this package.

var IIn = Function{
    Name:    "IIn",
    Aliases: []string{"iin"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Array}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Slice}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Map}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Struct}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{stringSliceType, stringSliceType}, Output: reflect.Bool},
    },
    Function: iin,
}
var In = Function{
    Name:    "In",
    Aliases: []string{"in"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{ipType, ipNetType}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{ipType, ipNetPtrType}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{intSliceType, intSliceType}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{stringSliceType, stringSliceType}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{byteSliceType, byteSliceType}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Array}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Slice}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Map}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.String, reflect.Struct}, Output: reflect.Bool},
    },
    Function: in,
}
var Intersects = Function{
    Name:    "intersects",
    Aliases: []string{"intersects"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Map, reflect.Map}, Output: boolType},
        Definition{Inputs: []interface{}{float64ArrayType, float64ArrayType}, Output: boolType},
    },
    Function: intersects,
}
var JSON = Function{
    Name:    "json",
    Aliases: []string{"json"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{nil}, Output: reflect.String},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) != 1 {
            return nil, &ErrInvalidArguments{Function: "Json", Arguments: args}
        }
        out, err := json.Marshal(args[0])
        if err != nil {
            return err, errors.Wrap(err, "error marshaling to json")
        }
        return out, nil
    },
}
var Join = Function{
    Name:    "Join",
    Aliases: []string{"join"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{uint82DSliceType, uint8Type}, Output: reflect.String},
        Definition{Inputs: []interface{}{uint82DSliceType, uint8SliceType}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Array, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{stringSetType, reflect.String}, Output: reflect.String},
    },
    Function: join,
}
var Last = Function{
    Name:    "Last",
    Aliases: []string{"last"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{byteSliceType}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{uint8SliceType}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{intSliceType}, Output: reflect.Int},
        Definition{Inputs: []interface{}{int64ArrayType}, Output: reflect.Int64},
        Definition{Inputs: []interface{}{float64ArrayType}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{stringSliceType}, Output: reflect.String},
        Definition{Inputs: []interface{}{stringIntMapSliceType}, Output: stringIntMapType},
        Definition{Inputs: []interface{}{interfaceArrayType}, Output: nil},
        Definition{Inputs: []interface{}{reflect.String}, Output: reflect.Uint8},
    },
    Function: last,
}
var Length = Function{
    Name:    "Length",
    Aliases: []string{"len", "length"},
    Definitions: []Definition{
        Definition{
            Inputs: []interface{}{reflect.Array},
            Output: reflect.Int,
        },
        Definition{
            Inputs: []interface{}{reflect.Slice},
            Output: reflect.Int,
        },
        Definition{
            Inputs: []interface{}{reflect.Map},
            Output: reflect.Int,
        },
        Definition{
            Inputs: []interface{}{reflect.String},
            Output: reflect.Int,
        },
    },
    Function: length,
}
var Limit = Function{
    Name:    "Limit",
    Aliases: []string{"limit"},
    Definitions: []Definition{
        Definition{
            Inputs: []interface{}{reflect.Array, reflect.Int},
            Output: reflect.Slice,
        },
        Definition{
            Inputs: []interface{}{reflect.Slice, reflect.Int},
            Output: reflect.Slice,
        },
    },
    Function: limit,
}
var Lookup = Function{
    Name:    "Lookup",
    Aliases: []string{"lookup"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array, nil}, Output: nil},
        Definition{Inputs: []interface{}{reflect.Array, nil, nil}, Output: nil},
        Definition{Inputs: []interface{}{reflect.Slice, nil}, Output: nil},
        Definition{Inputs: []interface{}{reflect.Slice, nil, nil}, Output: nil},
        Definition{Inputs: []interface{}{reflect.Map, nil}, Output: nil},
        Definition{Inputs: []interface{}{reflect.Map, nil, nil}, Output: nil},
    },
    Function: lookup,
}
var Make = Function{
    Name:    "Make",
    Aliases: []string{"make"},
    Definitions: []Definition{

        Definition{Inputs: []interface{}{reflect.String}, Output: nil},

        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: nil},

        Definition{Inputs: []interface{}{reflect.String, reflect.String, reflect.Int}, Output: nil},

        Definition{Inputs: []interface{}{reflect.String, reflect.String, reflect.Int, reflect.Int}, Output: nil},

        Definition{Inputs: []interface{}{reflect.String, reflect.String, reflect.String}, Output: nil},

        Definition{Inputs: []interface{}{reflect.String, reflect.String, reflect.String, reflect.Int}, Output: nil},
    },
    Function: makeObject,
}
var Max = Function{
    Name:        "Max",
    Aliases:     []string{"max"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        return math.Max(flat.Flat(args))
    },
}
var Mean = Function{
    Name:        "Mean",
    Aliases:     []string{"mean"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        return math.Mean(flat.Flat(args))
    },
}
var Min = Function{
    Name:        "Min",
    Aliases:     []string{"min"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        return math.Min(flat.Flat(args))
    },
}
var Multiply = Function{
    Name:        "Multiply",
    Aliases:     []string{"multiply", "mul"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        return math.Product(flat.Flat(args))
    },
}
var Now = Function{
    Name:    "Now",
    Aliases: []string{"now"},
    Definitions: []Definition{
        Definition{Inputs: make([]interface{}, 0), Output: timeType},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) != 0 {
            return nil, &ErrInvalidArguments{Function: "Now", Arguments: args}
        }
        return time.Now(), nil
    },
}
var Pow = Function{
    Name:        "Pow",
    Aliases:     []string{"pow"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) != 2 {
            return nil, &ErrInvalidArguments{Function: "Pow", Arguments: args}
        }
        return math.Pow(args[0], args[1])
    },
}
var Prefix = Function{
    Name:    "Prefix",
    Aliases: []string{"prefix"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Array, reflect.Array}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Array, reflect.Slice}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Array}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Slice}, Output: reflect.Bool},
    },
    Function: prefix,
}
var Repeat = Function{
    Name:    "Repeat",
    Aliases: []string{"repeat"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array, reflect.Int}, Output: reflect.Array},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Int}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.String, reflect.Int}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.Uint8, reflect.Int}, Output: reflect.Slice},
    },
    Function: repeat,
}
var Replace = Function{
    Name:    "Replace",
    Aliases: []string{"replace"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.String, reflect.String, reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, reflect.String, reflect.String, reflect.Int}, Output: reflect.String},
        Definition{Inputs: []interface{}{uint8SliceType, uint8Type, uint8Type}, Output: uint8SliceType},
        Definition{Inputs: []interface{}{uint8SliceType, uint8SliceType, uint8SliceType}, Output: uint8SliceType},
        Definition{Inputs: []interface{}{uint8SliceType, uint8SliceType, uint8SliceType, reflect.Int}, Output: uint8SliceType},
    },
    Function: replace,
}
var Second = Function{
    Name:    "Second",
    Aliases: []string{"second"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{byteSliceType}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{uint8SliceType}, Output: reflect.Uint8},
        Definition{Inputs: []interface{}{intSliceType}, Output: reflect.Int},
        Definition{Inputs: []interface{}{int64ArrayType}, Output: reflect.Int64},
        Definition{Inputs: []interface{}{float64ArrayType}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{stringSliceType}, Output: reflect.String},
        Definition{Inputs: []interface{}{stringIntMapSliceType}, Output: stringIntMapType},
        Definition{Inputs: []interface{}{interfaceArrayType}, Output: nil},
        Definition{Inputs: []interface{}{reflect.String}, Output: reflect.Uint8},
    },
    Function: second,
}
var Slice = Function{
    Name:    "Slice",
    Aliases: []string{"slice"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array, intType}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Array, intType, intType}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Slice, intType}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.Slice, intType, intType}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{reflect.String, intType}, Output: reflect.String},
        Definition{Inputs: []interface{}{reflect.String, intType, intType}, Output: reflect.String},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) == 2 {
            if start, ok := args[1].(int); ok {
                v := reflect.ValueOf(args[0])
                switch v.Type().Kind() {
                case reflect.Array, reflect.Slice, reflect.String:
                    return v.Slice(start, v.Len()).Interface(), nil
                }
            }
        } else if len(args) == 3 {
            if start, ok := args[1].(int); ok {
                if end, ok := args[2].(int); ok {
                    v := reflect.ValueOf(args[0])
                    switch v.Type().Kind() {
                    case reflect.Array, reflect.Slice, reflect.String:
                        return v.Slice(start, end).Interface(), nil
                    }
                }
            }
        }
        return nil, &ErrInvalidArguments{Function: "Slice", Arguments: args}
    },
}
var Sort = Function{
    Name:    "Sort",
    Aliases: []string{"sort"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringSliceType}, Output: stringSliceType},
        Definition{Inputs: []interface{}{intSliceType}, Output: intSliceType},
        Definition{Inputs: []interface{}{stringSliceType, boolType}, Output: stringSliceType},
        Definition{Inputs: []interface{}{intSliceType, boolType}, Output: intSliceType},
    },
    Function: sortArray,
}
var Split = Function{
    Name:    "Split",
    Aliases: []string{"split"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType, stringType}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{stringType, stringType, intType}, Output: reflect.Slice},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if str, ok := args[0].(string); ok {
            if delim, ok := args[1].(string); ok {
                if len(args) == 2 {
                    return strings.Split(str, delim), nil
                } else if len(args) == 3 {
                    if n, ok := args[2].(int); ok {
                        return strings.SplitN(str, delim, n), nil
                    }
                }
            }
        }
        return nil, &ErrInvalidArguments{Function: "Split", Arguments: args}
    },
}
var Stringify = Function{
    Name:    "Stringify",
    Aliases: []string{"stringify"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{nil}, Output: nil},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) != 1 {
            return nil, &ErrInvalidArguments{Function: "Stringify", Arguments: args}
        }
        return stringify.StringifyMapKeys(args[0], stringify.NewDefaultStringer())
    },
}
var Subtract = Function{
    Name:    "Subtract",
    Aliases: []string{"subtract", "sub"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Int, reflect.Int}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int, reflect.Int32}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int, reflect.Int64}, Output: reflect.Int64},
        Definition{Inputs: []interface{}{reflect.Int, reflect.Float64}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.Int}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.Int64}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Int64, reflect.Float64}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.Int}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.Int64}, Output: reflect.Int},
        Definition{Inputs: []interface{}{reflect.Float64, reflect.Float64}, Output: reflect.Float64},
        Definition{Inputs: []interface{}{reflect.Map, reflect.Map}, Output: reflect.Map},
    },
    Function: subtract,
}
var Suffix = Function{
    Name:    "Suffix",
    Aliases: []string{"suffix"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.String, reflect.String}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Array, reflect.Array}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Array, reflect.Slice}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Array}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{reflect.Slice, reflect.Slice}, Output: reflect.Bool},
    },
    Function: suffix,
}
var Sum = Function{
    Name:        "Sum",
    Aliases:     []string{"sum"},
    Definitions: []Definition{},
    Function: func(args ...interface{}) (interface{}, error) {
        return math.Sum(flat.Flat(args))
    },
}
var TileX = Function{
    Name:    "TileX",
    Aliases: []string{"tileX"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{float64Type, intType}, Output: intType},
        Definition{Inputs: []interface{}{intType, intType}, Output: intType},
    },
    Function: tileX,
}
var TileY = Function{
    Name:    "TileY",
    Aliases: []string{"tileY"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{float64Type, intType}, Output: intType},
        Definition{Inputs: []interface{}{intType, intType}, Output: intType},
    },
    Function: tileY,
}
var ToBigEndian = Function{
    Name:    "ToBigEndian",
    Aliases: []string{"big"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int8Type}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int16Type}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int32Type}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int64Type}, Output: byteSliceType},
    },
    Function: toBigEndian,
}
var ToBytes = Function{
    Name:    "ToBytes",
    Aliases: []string{"bytes"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{byteType}, Output: byteSliceType},
        Definition{Inputs: []interface{}{byteSliceType}, Output: byteSliceType},
        Definition{Inputs: []interface{}{stringType}, Output: byteSliceType},
    },
    Function: toBytes,
}
var ToFloat32 = Function{
    Name:    "ToFloat32",
    Aliases: []string{"float32"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: float32Type},
        Definition{Inputs: []interface{}{int8Type}, Output: float32Type},
        Definition{Inputs: []interface{}{int16Type}, Output: float32Type},
        Definition{Inputs: []interface{}{int32Type}, Output: float32Type},
        Definition{Inputs: []interface{}{int64Type}, Output: float32Type},
        Definition{Inputs: []interface{}{float32Type}, Output: float32Type},
        Definition{Inputs: []interface{}{float64Type}, Output: float32Type},
        Definition{Inputs: []interface{}{reflect.String}, Output: float32Type},
    },
    Function: toFloat32,
}
var ToFloat64 = Function{
    Name:    "ToFloat64",
    Aliases: []string{"float64"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: float64Type},
        Definition{Inputs: []interface{}{int8Type}, Output: float64Type},
        Definition{Inputs: []interface{}{int16Type}, Output: float64Type},
        Definition{Inputs: []interface{}{int32Type}, Output: float64Type},
        Definition{Inputs: []interface{}{int64Type}, Output: float64Type},
        Definition{Inputs: []interface{}{float64Type}, Output: float64Type},
        Definition{Inputs: []interface{}{reflect.String}, Output: float64Type},
    },
    Function: toFloat64,
}
var ToFloat64Slice = Function{
    Name:    "Float64Slice",
    Aliases: []string{"float64Array", "float64Slice"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array}, Output: float64ArrayType},
        Definition{Inputs: []interface{}{reflect.Slice}, Output: float64ArrayType},
    },
    Function: float64Slice,
}
var ToInt = Function{
    Name:    "ToInt",
    Aliases: []string{"int"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: intType},
        Definition{Inputs: []interface{}{int8Type}, Output: intType},
        Definition{Inputs: []interface{}{int16Type}, Output: intType},
        Definition{Inputs: []interface{}{int32Type}, Output: intType},
        Definition{Inputs: []interface{}{int64Type}, Output: intType},
        Definition{Inputs: []interface{}{float32Type}, Output: intType},
        Definition{Inputs: []interface{}{float64Type}, Output: intType},
        Definition{Inputs: []interface{}{reflect.String}, Output: intType},
    },
    Function: toInt,
}
var ToInt16 = Function{
    Name:    "ToInt16",
    Aliases: []string{"int16"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: int16Type},
        Definition{Inputs: []interface{}{int8Type}, Output: int16Type},
        Definition{Inputs: []interface{}{int16Type}, Output: int16Type},
        Definition{Inputs: []interface{}{int32Type}, Output: int16Type},
        Definition{Inputs: []interface{}{int64Type}, Output: int16Type},
        Definition{Inputs: []interface{}{reflect.String}, Output: int16Type},
    },
    Function: toInt16,
}
var ToInt32 = Function{
    Name:    "ToInt32",
    Aliases: []string{"int32"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: int32Type},
        Definition{Inputs: []interface{}{int8Type}, Output: int32Type},
        Definition{Inputs: []interface{}{int16Type}, Output: int32Type},
        Definition{Inputs: []interface{}{int32Type}, Output: int32Type},
        Definition{Inputs: []interface{}{int64Type}, Output: int32Type},
        Definition{Inputs: []interface{}{float32Type}, Output: int32Type},
        Definition{Inputs: []interface{}{float64Type}, Output: int32Type},
        Definition{Inputs: []interface{}{reflect.String}, Output: int32Type},
    },
    Function: toInt32,
}
var ToInt64 = Function{
    Name:    "ToInt64",
    Aliases: []string{"int64"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: int64Type},
        Definition{Inputs: []interface{}{int8Type}, Output: int64Type},
        Definition{Inputs: []interface{}{int16Type}, Output: int64Type},
        Definition{Inputs: []interface{}{int32Type}, Output: int64Type},
        Definition{Inputs: []interface{}{int64Type}, Output: int64Type},
        Definition{Inputs: []interface{}{float32Type}, Output: int64Type},
        Definition{Inputs: []interface{}{float64Type}, Output: int64Type},
        Definition{Inputs: []interface{}{reflect.String}, Output: int64Type},
    },
    Function: toInt64,
}
var ToInt8 = Function{
    Name:    "ToInt8",
    Aliases: []string{"int8"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: int8Type},
        Definition{Inputs: []interface{}{int8Type}, Output: int8Type},
        Definition{Inputs: []interface{}{int16Type}, Output: int8Type},
        Definition{Inputs: []interface{}{int32Type}, Output: int8Type},
        Definition{Inputs: []interface{}{int64Type}, Output: int8Type},
        Definition{Inputs: []interface{}{reflect.String}, Output: int8Type},
    },
    Function: toInt8,
}
var ToIntSlice = Function{
    Name:    "IntSlice",
    Aliases: []string{"intArray", "intSlice"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array}, Output: intSliceType},
        Definition{Inputs: []interface{}{reflect.Slice}, Output: intSliceType},
    },
    Function: intArray,
}
var ToItems = Function{
    Name:    "ToItems",
    Aliases: []string{"items"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Map}, Output: reflect.Slice},
    },
    Function: toItems,
}
var ToKeys = Function{
    Name:    "ToKeys",
    Aliases: []string{"keys"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Map}, Output: reflect.Slice},
    },
    Function: toKeys,
}
var ToLittleEndian = Function{
    Name:    "ToLittleEndian",
    Aliases: []string{"little"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{intType}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int8Type}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int16Type}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int32Type}, Output: byteSliceType},
        Definition{Inputs: []interface{}{int64Type}, Output: byteSliceType},
    },
    Function: toLittleEndian,
}
var ToLower = Function{
    Name:    "ToLower",
    Aliases: []string{"lower"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType}, Output: stringType},
        Definition{Inputs: []interface{}{byteSliceType}, Output: byteSliceType},
    },
    Function: toLower,
}
var ToSet = Function{
    Name:    "ToSet",
    Aliases: []string{"set"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array}, Output: reflect.Map},
        Definition{Inputs: []interface{}{reflect.Slice}, Output: reflect.Map},
        Definition{Inputs: []interface{}{reflect.Map}, Output: reflect.Map},
    },
    Function: toSet,
}
var ToString = Function{
    Name:    "ToString",
    Aliases: []string{"string", "str"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{nil}, Output: stringType},
    },
    Function: toString,
}
var ToStringSet = Function{
    Name:    "ToStringSet",
    Aliases: []string{"stringSet"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array}, Output: stringSetType},
        Definition{Inputs: []interface{}{reflect.Slice}, Output: stringSetType},
        Definition{Inputs: []interface{}{reflect.Map}, Output: stringSetType},
    },
    Function: stringSet,
}
var ToStringSlice = Function{
    Name:    "StringSlice",
    Aliases: []string{"stringArray", "stringSlice"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Array}, Output: stringSliceType},
        Definition{Inputs: []interface{}{reflect.Slice}, Output: stringSliceType},
        Definition{Inputs: []interface{}{reflect.String}, Output: stringSliceType},
    },
    Function: stringSlice,
}
var ToTime = Function{
    Name:    "ToTime",
    Aliases: []string{"time"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType}, Output: timeType},
        Definition{Inputs: []interface{}{timeType}, Output: timeType},
    },
    Function: toTime,
}
var ToTitle = Function{
    Name:    "ToTitle",
    Aliases: []string{"title"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType}, Output: stringType},
        Definition{Inputs: []interface{}{byteSliceType}, Output: byteSliceType},
    },
    Function: toTitle,
}
var ToUpper = Function{
    Name:    "ToUpper",
    Aliases: []string{"upper"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringType}, Output: stringType},
        Definition{Inputs: []interface{}{byteSliceType}, Output: byteSliceType},
    },
    Function: toUpper,
}
var ToValues = Function{
    Name:    "ToValues",
    Aliases: []string{"values"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.Map}, Output: interfaceArrayType},
    },
    Function: toValues,
}
var Top = Function{
    Name:    "Top",
    Aliases: []string{"top"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{stringIntMapType, intType}, Output: stringSliceType},
        Definition{Inputs: []interface{}{stringIntMapType, intType, intType}, Output: stringSliceType},
    },
    Function: top,
}
var Trim = Function{
    Name:    "Trim",
    Aliases: []string{"trim"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{reflect.String}, Output: reflect.String},
        Definition{Inputs: []interface{}{stringSliceType}, Output: reflect.String},
        Definition{Inputs: []interface{}{interfaceArrayType}, Output: reflect.String},
    },
    Function: trim,
}
var Within = Function{
    Name:    "Within",
    Aliases: []string{"within"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{float64ArrayType, float64ArrayType}, Output: reflect.Bool},
        Definition{Inputs: []interface{}{intSliceType, intSliceType}, Output: reflect.Bool},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if coordinate, ok := args[0].([]float64); ok {
            if extent, ok := args[1].([]float64); ok {
                dims := len(coordinate)
                if dims*2 == len(extent) {
                    for i, x := range coordinate {
                        if x < extent[i] || x > extent[i+dims] {
                            return false, nil
                        }
                    }
                    return true, nil
                }
            }
        } else if coordinate, ok := args[0].([]int); ok {
            if extent, ok := args[1].([]int); ok {
                dims := len(coordinate)
                if dims*2 == len(extent) {
                    for i, x := range coordinate {
                        if x < extent[i] || x > extent[i+dims] {
                            return false, nil
                        }
                    }
                    return true, nil
                }
            }
        }
        return nil, &ErrInvalidArguments{Function: "Within", Arguments: args}
    },
}
var Words = Function{
    Name:    "Words",
    Aliases: []string{"words"},
    Definitions: []Definition{
        Definition{Inputs: []interface{}{uint8SliceType}, Output: reflect.Slice},
        Definition{Inputs: []interface{}{stringType}, Output: reflect.Slice},
    },
    Function: func(args ...interface{}) (interface{}, error) {
        if len(args) == 1 {
            switch in := args[0].(type) {
            case string:
                scanner := bufio.NewScanner(strings.NewReader(in))
                scanner.Split(bufio.ScanWords)
                words := make([]string, 0)
                for scanner.Scan() {
                    words = append(words, scanner.Text())
                }
                if err := scanner.Err(); err != nil {
                    return words, err
                }
                return words, nil
            case []byte:
                scanner := bufio.NewScanner(bytes.NewReader(in))
                scanner.Split(bufio.ScanWords)
                words := make([]string, 0)
                for scanner.Scan() {
                    words = append(words, scanner.Text())
                }
                if err := scanner.Err(); err != nil {
                    return words, err
                }
                return words, nil
            }
        }
        return nil, &ErrInvalidArguments{Function: "Words", Arguments: args}
    },
}

type Definition Uses

type Definition struct {
    Inputs []interface{} // an array of types or kinds
    Output interface{}   // the type or kind returned by the defined function
}

Definition is a struct containing a function definition

func (Definition) IsValid Uses

func (d Definition) IsValid(args ...interface{}) bool

IsValid returns true if the args match the kinds or types or the definition.

type ErrInvalidArguments Uses

type ErrInvalidArguments struct {
    Function  string        // the name of the Function
    Arguments []interface{} // the arguments for the function
}

ErrInvalidArguments is an error returned when a function is called with invalid arguments.

func (ErrInvalidArguments) Error Uses

func (e ErrInvalidArguments) Error() string

Error returns the error as a string.

type ErrInvalidCapacity Uses

type ErrInvalidCapacity struct {
    Value  int
    Length int
}

ErrInvalidCapacity is an error returned when the capacity for a slice is too low since it must always be greater than or equal to the length.

func (ErrInvalidCapacity) Error Uses

func (e ErrInvalidCapacity) Error() string

Error returns the error as a string.

type ErrOutOfRange Uses

type ErrOutOfRange struct {
    Index int
    Max   int
}

ErrOutOfRange is an error returned when an array or slice index is out of range.

func (ErrOutOfRange) Error Uses

func (e ErrOutOfRange) Error() string

Error returns the error as a string.

type ErrOverflow Uses

type ErrOverflow struct {
    Original interface{}  // the original value
    Target   reflect.Type // the target type
}

ErrOverflow is an error returned a number can't be converted to a lower bitsize.

func (ErrOverflow) Error Uses

func (e ErrOverflow) Error() string

Error returns the error as a string.

type Function Uses

type Function struct {
    Name        string                                         // the name of the function
    Description string                                         // a description of the function
    Aliases     []string                                       // aliases for the function used when providing a public API
    Definitions []Definition                                   // the definitions of the function
    Function    func(args ...interface{}) (interface{}, error) // the underlying function to execute
}

Function is a struct used for giving an underlying function a name and definitions.

func (Function) IsValid Uses

func (f Function) IsValid(args ...interface{}) bool

IsValid returns true if the arguments match a definition of the function.

func (Function) Map Uses

func (f Function) Map() map[string]interface{}

Map returns a map of metadata about the function.

func (Function) MustRun Uses

func (f Function) MustRun(args ...interface{}) interface{}

MustRun executes the function with the provided arguments and returns the result. If there is any error, then panics.

func (Function) Run Uses

func (f Function) Run(args ...interface{}) (interface{}, error)

Run executes the function with the provided arguments and returns the result, and error if any. It accepts variadic input.

func (Function) Validate Uses

func (f Function) Validate(args ...interface{}) error

Validate returns a ErrInvalidArguments error if the arguments do not match a definition of the function.

func (Function) ValidateRun Uses

func (f Function) ValidateRun(args ...interface{}) (interface{}, error)

ValidateRun validates the function arguments and then runs the function if valid. It accepts variadic input.

type TestCase Uses

type TestCase struct {
    Inputs   []interface{} // the inputs to the function
    Function Function      // the function to evaluate
    Output   interface{}   // the result of the evaluation
}

TestCase is a struct containing the variables for a unit test of function evaluation

func NewTestCase Uses

func NewTestCase(inputs []interface{}, function Function, output interface{}) TestCase

NewTestCase returns a new TestCase

Package af imports 19 packages (graph) and is imported by 2 packages. Updated 2019-10-16. Refresh now. Tools for package owners.