NeonBlue.Expressions 0.1.5

dotnet add package NeonBlue.Expressions --version 0.1.5
                    
NuGet\Install-Package NeonBlue.Expressions -Version 0.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NeonBlue.Expressions" Version="0.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NeonBlue.Expressions" Version="0.1.5" />
                    
Directory.Packages.props
<PackageReference Include="NeonBlue.Expressions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NeonBlue.Expressions --version 0.1.5
                    
#r "nuget: NeonBlue.Expressions, 0.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NeonBlue.Expressions@0.1.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NeonBlue.Expressions&version=0.1.5
                    
Install as a Cake Addin
#tool nuget:?package=NeonBlue.Expressions&version=0.1.5
                    
Install as a Cake Tool

Introduction

Overview

ExpressionEvaluator is a comprehensive library designed to efficiently parse, evaluate, and interpret mathematical expressions in C#. It provides a flexible framework for handling various data types, operators, functions, and variables within expressions.

Target Audience

This library is primarily aimed at developers and data analysts who need to incorporate expression evaluation capabilities into their applications. It is particularly useful for building calculators, scripting languages, data analysis tools, and custom domain-specific languages.

Installation

NuGet:

  • Open your Visual Studio project.
  • Right-click on the project and select "Manage NuGet Packages."
  • Search for "NeonBlue.Expressions" and install the package.

Alternatively:

  • Download the library's NuGet package from the official repository.
  • Add the package to your project using the Package Manager Console or by manually adding a reference.
  • Install using CLI.
   dotnet add package NeonBlue.Expressions

Getting Started

To get started you first need to import the library namespcae:

    using NeonBlue.Expressions;

Basic Usage:

    Evaluator evaluator = new();
    Expression expression = "2 * (3 + 4) / 5";
    var result = evaluator.Evaluate<int>(expression);
    Console.WriteLine("Result: " + result);

Parameterized expression:

        //creating the expression.
        Expression expression = "-1 + (-sum(x )+countd(x2) + (y -2) ) ";
    
        //define values.
        double[] x = [2, 2];
        double[] x2 = [20, 45, 60];
        double y = 10;
        //creating the expression parameters.
        var parameters =  new ExpressionParameters(new ExpressionParameter("x", x),
        new ExpressionParameter("x2", x2),
        new ExpressionParameter("y", y));

        //creating the evaluator object.
        var evaluator = new Evaluator(new ExecutionOptions(NullStrategy.Throw));

        //evaluate the expression given the parameters and expect double result.
        var result = evaluator.Evaluate<double?>(expression,parameters);

        //display the result
        Console.WriteLine($"the result of the expression is ({result})");

Create Custom Function:

        Evaluator evaluator = new();   
        Func<int,int,int> myFunction=(a,b) => a+b;
        // add a custom function
        evaluator.AddCustomFunction("myFunction", myFunction);
        Expression expression = "myFunction(1, 2)"; 
        var result = evaluator.Evaluate(expression);

        Console.WriteLine("Result: " + result);

Common Use Cases:

  • Calculator applications: Create advanced calculators with support for various mathematical operations and functions.
  • Scripting and automation: Implement scripting languages or automation tools that rely on expression evaluation.
  • Data analysis and processing: Perform complex calculations and transformations on data.
  • Custom domain-specific languages: Develop DSLs that use expression evaluation for defining rules or logic.
  • Business Rules:: Implement dynamic decision-making logic based on custom conditions.

Core Concepts

Expressions:

Expressions are constructed using a combination of variables, constants, operators, and functions. The library supports a wide range of expression syntax, including arithmetic operations, logical comparisons, function calls, and parentheses for grouping.

Evaluation:

The Evaluate method on the evaluator object is used to parse and evaluate an expression. It returns a Token object representing the result of the evaluation. The result can be a numerical value, a string, a datetime, a boolean or null depending on the expression.

Functions and Operators: The library supports a variety of built-in functions and operators, including:

  • Arithmetic operators: +, -, *, /, %, ^
  • Logical operators: &&, ||, !
  • Comparison operators: ==, !=, <, >, >=, ⇐
  • Mathematical functions: sin, cos, tan, log, sqrt, etc.
  • Datetime functions: adddays, addhours, dayname, etc.
  • Boolean functions: iif, and, isnull, etc.
  • String functions: concat, contain, ltrim, left, etc.
  • You can also define and use custom functions within your expressions.

For more information, please refer to our Wiki.

License

NeonBlue.Expressions is licensed under the MIT License and the Apache License, Version 2.0 (the "Licenses"). You may obtain a copy of the MIT License at https://opensource.org/license/mit and the Apache License, Version 2.0 at https://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the Licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licenses for the specific language governing permissions and limitations under the Licenses.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.5 5,890 9/24/2024
0.1.3 127 9/21/2024
0.1.1 125 9/19/2024