AI prompts
base on NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. It supports a wide range of mathematical and logical operations. <div align="center">
<img src="NCalc.png" alt="NCalc" style="width:100px;"/>
<h1>NCalc</h1>
<a href="https://github.com/ncalc/ncalc/actions/workflows/build-test.yml">
<img src="https://img.shields.io/github/actions/workflow/status/ncalc/ncalc/build-test.yml" alt="GitHub Actions Workflow Status" />
</a>
<a href="https://codecov.io/gh/ncalc/ncalc">
<img src="https://img.shields.io/codecov/c/github/ncalc/ncalc.svg" alt="Coverage" />
</a>
<a href="https://nuget.org/packages/NCalcSync.signed">
<img src="https://img.shields.io/nuget/v/NCalcSync.signed.svg?label=nuget&color=004880" alt="NuGet" />
</a>
<a href="https://nuget.org/packages/NCalcSync.signed">
<img src="https://img.shields.io/nuget/dt/NCalcSync.svg?color=004880" alt="NuGet Downloads" />
</a>
<a href="https://discord.gg/TeJkmXbqFk">
<img src="https://img.shields.io/discord/1237181265426387005?color=5b62ef&label=discord" alt="Discord" />
</a>
</div>
<br>
NCalc is a fast and lightweight expression evaluator library for .NET, designed for flexibility and high performance. It
supports a wide range of mathematical and logical operations. NCalc can parse any expression and evaluate the result,
including static or dynamic parameters and custom functions. NCalc targets .NET 8, .NET Standard 2.0 and NET Framework
4.8.
## Docs
Need help or want to learn more? [Check our docs.](https://ncalc.github.io/ncalc)
## Learn more
For additional information on the technique we used to create this framework please
read [this article.](https://www.codeproject.com/Articles/18880/State-of-the-Art-Expression-Evaluation)
## Help
> [!IMPORTANT]
> If you need help, [please open an issue](https://github.com/ncalc/ncalc/issues/new/choose) and include the expression
> to help us better understand the problem.
> Providing this information will aid in resolving the issue effectively.
## Getting Started
If you want to evaluate simple expressions:
```
dotnet add package NCalcSync
```
Want `async` support at your functions and parameters?
```
dotnet add package NCalcAsync
```
Dependency Injection? We got you covered:
```
dotnet add package NCalc.DependencyInjection
```
## Functionalities
### Simple Expressions
```c#
var expression = new Expression("2 + 3 * 5");
Debug.Assert(17 == expression.Evaluate());
```
**Evaluates .NET data types**
```c#
Debug.Assert(123456 == new Expression("123456").Evaluate()); // integers
Debug.Assert(new DateTime(2001, 01, 01) == new Expression("#01/01/2001#").Evaluate()); // date and times
Debug.Assert(123.456 == new Expression("123.456").Evaluate()); // floating point numbers
Debug.Assert(true == new Expression("true").Evaluate()); // booleans
Debug.Assert("azerty" == new Expression("'azerty'").Evaluate()); // strings
```
**Handles mathematical functional from System.Math**
```c#
Debug.Assert(0 == new Expression("Sin(0)").Evaluate());
Debug.Assert(2 == new Expression("Sqrt(4)").Evaluate());
Debug.Assert(0 == new Expression("Tan(0)").Evaluate());
```
**Evaluates custom functions**
```c#
var expression = new Expression("SecretOperation(3, 6)");
expression.Functions["SecretOperation"] = (args) => {
return (int)args[0].Evaluate() + (int)args[1].Evaluate();
};
Debug.Assert(9 == expression.Evaluate());
```
**Handles unicode characters**
```c#
Debug.Assert("経済協力開発機構" == new Expression("'経済協力開発機構'").Evaluate());
Debug.Assert("Hello" == new Expression(@"'\u0048\u0065\u006C\u006C\u006F'").Evaluate());
Debug.Assert("だ" == new Expression(@"'\u3060'").Evaluate());
Debug.Assert("\u0100" == new Expression(@"'\u0100'").Evaluate());
```
**Define parameters, even dynamic or expressions**
```c#
var expression = new Expression("Round(Pow([Pi], 2) + Pow([Pi2], 2) + [X], 2)");
expression.Parameters["Pi2"] = new Expression("Pi * [Pi]");
expression.Parameters["X"] = 10;
expression.DynamicParameters["Pi"] = _ => {
Console.WriteLine("I'm evaluating π!");
return 3.14;
};
Debug.Assert(117.07 == expression.Evaluate());
```
**JSON Serialization**
At .NET 8+, NCalc have built-in support to polymorphic JSON serialization using [System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json).
```c#
const string expressionString = "{waterLevel} > 4.0";
var logicalExpression = LogicalExpressionFactory.Create(expressionString, ExpressionOptions.NoCache); //Created a BinaryExpression object.
var jsonExpression = JsonSerializer.Serialize(parsedExpression);
var deserializedLogicalExpression = JsonSerializer.Deserialize<LogicalExpression>(jsonExpression); //The object is still a BinaryExpression.
var expression = new Expression(deserializedLogicalExpression);
expression.Parameters = new Dictionary<string, object> {
{"waterLevel", 4.0}
};
var result = expression.Evaluate();
```
**Caching**
NCalc automatically cache the parsing of strings using a [`ConcurrentDictionary`](https://learn.microsoft.com/pt-br/dotnet/api/system.collections.concurrent.concurrentdictionary-2).
You can also use our [Memory Cache plugin](https://ncalc.github.io/ncalc/articles/plugins/memory_cache.html).
**Lambda Expressions**
```cs
var expression = new Expression("1 + 2");
Func<int> function = expression.ToLambda<int>();
Debug.Assert(function()); //3
```
## Related projects
### [Parlot](https://github.com/sebastienros/parlot)
Fast and lightweight parser creation tools by [Sébastien Ros](https://github.com/sebastienros) that NCalc uses at its
parser.
### [PanoramicData.NCalcExtensions](https://github.com/panoramicdata/PanoramicData.NCalcExtensions)
Extension functions for NCalc to handle many general functions,
including string functions, switch, if, in, typeOf, cast etc.
Developed by David, Dan and all at [Panoramic Data](https://github.com/panoramicdata).
### [Jint](https://github.com/sebastienros/jint)
JavaScript Interpreter for .NET by [Sébastien Ros](https://github.com/sebastienros), the author of NCalc library.
Runs on any modern .NET platform as it supports .NET Standard 2.0 and .NET 4.6.1 targets (and up).
### [NCalcJS](https://github.com/thomashambach/ncalcjs)
A TypeScript/JavaScript port of NCalc.
### [NCalc101](https://ncalc101.magicsuite.net)
NCalc 101 is a simple web application that allows you to try out the NCalc expression evaluator, developed
by [Panoramic Data](https://github.com/panoramicdata).
### [JJMasterData](https://github.com/JJConsulting/JJMasterData/)
JJMasterData is a runtime form generator from database metadata. It uses NCalc to evaluate expressions used in field
visibility and other dynamic behaviors.
## NCalc versioning
The project uses [Nerdbank.GitVersioning](https://github.com/dotnet/Nerdbank.GitVersioning) tool to manage versions.
Each library build can be traced back to the original git commit.
Read more about [versioning here.](https://ncalc.github.io/ncalc/articles/new_release.html)
## Discord Server
If you want to talk with us, get support or just get the latest NCalc
news, [come to our discord server](https://discord.gg/TeJkmXbqFk).
## Star History
<a href="https://star-history.com/#ncalc/ncalc&Date">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=ncalc/ncalc&type=Date&theme=dark" />
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=ncalc/ncalc&type=Date" />
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=ncalc/ncalc&type=Date" />
</picture>
</a>
", Assign "at most 3 tags" to the expected json: {"id":"12247","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"