Comments (4)
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.
without the need to pass on each Compile?
Why this is a problem?
from expr.
@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.
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)
- Parse math.MinInt64 error: invalid integer literal: strconv.ParseInt: parsing "9223372036854775808": value out of range (1:20) HOT 4
- will expr support operator bit operation? HOT 3
- How to get output result for simple arithmetic expressions?
- Return Nil For Invalid Slice Index HOT 7
- Is there an error in the document? HOT 2
- invalid char escape HOT 1
- up to 1.20 and Hold business error HOT 7
- `interface conversion: interface {} is int, not string` when trying to index slice type HOT 1
- expr.AsInt allows any, expr.AsBool does not allow any HOT 13
- Structs error HOT 4
- Method resolution fail HOT 5
- Inconsistent signature validation when using `expr.Function` HOT 1
- ast.IndexNode and ast.MethodNode undefined HOT 3
- Operating overloading does not work with functions defined using `expr.Function` HOT 2
- Some builtins may wrongly compile HOT 1
- Patcher example with IndexNode no longer works HOT 2
- Array comparison is no longer compatible with lower versions HOT 3
- Enhancement Request: support extracting expected program inputs HOT 4
- How function call function HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from expr.