Giter VIP home page Giter VIP logo

Comments (4)

mdmcconnell avatar mdmcconnell commented on August 11, 2024

There is no mechanism that works quite like the outline you suggest above, because the compiler does not maintain state from one compile to the next. Are you concerned about performance? Code you write in go is compiled once, when you go build; expr.Compile is more like linking those already-compiled go functions into your expression, so there is little performance cost. For performance improvement on an expression that you will run many times, you can break the process into expr.Compile (once), then expr.Run (many times).

from expr.

antonmedv avatar antonmedv commented on August 11, 2024

without the need to pass on each Compile?

Why this is a problem?

from expr.

sujit-baniya avatar sujit-baniya commented on August 11, 2024

@antonmedv It's not a problem but just with some ease to add custom functions in one place and use them for any patterns we pass. For e.g.

type customFunction struct {
	funcs map[string]func(params ...any) (any, error)
	mu    *sync.RWMutex
}

var customFunctions *customFunction

func init() {
	customFunctions = &customFunction{
		funcs: make(map[string]func(params ...any) (any, error)),
		mu:    &sync.RWMutex{},
	}
}

func AddFunction(name string, handler func(params ...any) (any, error)) {
	customFunctions.mu.Lock()
	defer customFunctions.mu.Unlock()
	customFunctions.funcs[name] = handler
}

func Parse(expr string) (*vm.Program, error) {
	var opts []Option
	for name, handler := range customFunctions.funcs {
		opts = append(opts, Function(name, handler))
	}
	return Compile(expr, opts...)
}

// vm/program.go
func (program *Program) Eval(param any) (any, error) {
	return Run(program, param)
}

from expr.

antonmedv avatar antonmedv commented on August 11, 2024

It is doable of course. But I prefer the API to be more simple and straightforward, without any shared state. It is anyway possible to create such API as a wrapper for the Expr package.

from expr.

Related Issues (20)

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.