Git Product home page Git Product logo

go-ff's People

Contributors

ajm188 avatar

Watchers

 avatar  avatar  avatar

go-ff's Issues

Provide a standard set of functions to be available in any `EXPRESSION` feature

Things like:

  • rand(n float64) float64
  • ............. idk that's all i can come up with right now.

Abstractly I thought this was a good idea, but now that I'm playing around with this, anything that's idempotent can be precomputed and be provided via the parameters map in Evaluate, so there may not be a huge need for this.

There's also the whole thing where govaluate only actually deals in four types: float64, bool, string, and arrays, so the "correct" implementation a rand function is:

map[string]govaluate.ExpressionFunction{
	"rand": func(arguments ...interface{}) (interface{}, error) {
		if len(arguments) != 1 {
			return nil, fmt.Errorf("must provide one argument to rand, got %d", len(arguments))
		}

		n, ok := arguments[0].(float64)
		if !ok {
			return nil, fmt.Errorf("rand takes a numeric value, got %v (%T)", arguments[0], arguments[0])
		}

		return float64(rand.Intn(int(n))), nil
	}
}

If we don't explicitly cast the result of rand.Intn to a float64 then that value will be treated as a string in the expression, so an expression like rand(10) < 2 will fail because it's comparing a string and a float. So the whole thing also becomes kind of annoying to implement correctly.

Allow user-defined functions to be available in expression evaluation

An important note from the govaluate manual (emphasis mine):

During expression parsing (not evaluation), a map of functions can be given to govaluate.NewEvaluableExpressionWithFunctions (the lengthiest and finest of function names). The resultant expression will be able to invoke those functions during evaluation. Once parsed, an expression cannot have functions added or removed - a new expression will need to be created if you want to change the functions, or behavior of said functions.

So, managing that could get a bit tricky, depending on whether we want users to provide functions globally (pro: automatically available in every feature, don't have to remember which features have which functions available; con: have to re-parse every single feature anytime a function is added or removed, even if that feature doesn't use that function) or per-feature (same pro/con list but reversed). In any case, I wanted to get this written down before I inevitably forget.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.