regel / expr

Expression language for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expr

License Go Report Card Build codecov

Expression language for Go

Expr package provides an engine that can compile and evaluate math expressions. An expression is a one-liner that returns a value (mostly, but not limited to, float64 and []float64). It is designed for simplicity, speed and safety.

The purpose of the package is to allow users to use expressions for fast vector math operations. It is a perfect candidate for the foundation of a time series database and machine learning feature engineering. The idea is to let configure things in a dynamic way without recompile of a program:

# Get a new vector from the addition of cos() and sin() of other variables
cos(X) + 0.33 * sin(Y)

# Get a new norm vector from vector X
(X - nanmin(X)) / (nanmax(X) - nanmin(X))

Features

  • Seamless integration with Go (no need to redefine types)
  • Static typing
    out, err := expr.Compile(`name + age`)
    // err: invalid operation + (mismatched types string and int)
    // | name + age
    // | .....^
  • User-friendly error messages.
  • Reasonable set of basic operators.
  • Dozens of Numpy-like builtin math functions: abs, acos, acosh, asin, asinh, atan, atanh, cbrt, ceil, cos, cosh, erf, erfc, erfcinv, erfinv, exp, exp2, expm1, floor, gamma, j0, j1, log, log10, log1p, log2, logb, round, roundtoeven, sin, sinh, sqrt, tan, tanh, trunc, y0, y1, maximum, minimum, mod, pow, remainder, nanmin, nanmax, nanmean, nanstd, nansum, nanprod.
    2 * (nanmean(Scores) - minimum(Elevation, Temp))

Install

go get github.com/regel/expr

Examples

package main

import (
	"fmt"
	"github.com/regel/expr"
	"github.com/regel/expr/ast"
)

func main() {
	code := `1 + (sum(Features) / 2)`

	program, err := expr.Compile(code)
	if err != nil {
		panic(err)
	}

	env := &ast.Env{
		"Features": []float64{0.5, 1.0, 1.5},
	}
	output, err := expr.Run(program, env)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%v\n", output)
}

Who is using Expr?

Add your company too

License

MIT

About

Expression language for Go

License:MIT License


Languages

Language:Go 100.0%