NFun 1.0.1

dotnet add package NFun --version 1.0.1
NuGet\Install-Package NFun -Version 1.0.1
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="NFun" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NFun --version 1.0.1
#r "nuget: NFun, 1.0.1"
#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.
// Install NFun as a Cake Addin
#addin nuget:?package=NFun&version=1.0.1

// Install NFun as a Cake Tool
#tool nuget:?package=NFun&version=1.0.1

NFun. Expressions Evaluator for .NET

To install NFun, run the following command in the Package Manager Console:

PM> Install-Package NFun 

What is the NFun?

This is an expression evaluator or a mini-script language for .net. It supports working with mathematical expressions as well as with collections, strings, hi-order functions and structures. NFun is quite similar to NCalc but with a rich type system and linq support. See the 'How to / specifications' section for details

Nfun can perform simple evaluations

  double d = Funny.Calc<double>(" 2 * 10 + 1 ") // 21  
  bool b   = Funny.Calc<bool>("false and (2 > 1)") // false

  // 'age' and 'name' are properties from input 'User' model 
  string userAlias = Funny.Calc<User,string> (
                       "if(age < 18) name else 'Mr. {name}' ", 
                        inputUser)  

as well as complex, with multiple composite inputs and outputs

  // Evaluate many values and set them into 'Person' object's properties 
  // inputs and outputs 'age', 'cars' and 'birthYear' are properties of 'Person' object 
  var personModel = new Person(birthYear: 2000);
  
  Funny.CalcContext(@"   
     age = 2022 - birthYear 
     cars = [
   	    { name = 'lada',   cost = 1200, power = 102 },
   	    { name = 'camaro', cost = 5000, power = 275 }
     ]
     ", personModel);
  
  Assert.Equal(22,   personModel.Age);
  Assert.Equal(2,    personModel.Cars.Count());
  Assert.Equal(1200, personModel.Cars[0].Cost);
  

Low-level hardcore API is also supported

  var runtime = Funny.Hardcore.Build("y = 2x+1");

  runtime["x"].Value = 42; //Write input data
  runtime.Run(); //Run script
  var result = runtime["y"].Value //collect results
  
  Console.WriteLine("Script contains these variables:"
  foreach(var variable in runtime.Variables)
     Console.WriteLine(
        "{variable.Name}:{variable.Type} {variable.IsOutput?"[OUTPUT]":"[INPUT]"}");

Key features

  • Arithmetic, Bitwise, Discreet operators
  # Arithmetic operators: + - * / % // ** 
  y1 = 2*(x//2 + 1) / (x % 3 -1)**0.5 + 3x
  
  # Bitwise:     ~ | & ^ << >> 
  y2 = (x | y & 0xF0FF << 2) ^ 0x1234
	
  # Discreet:    and or not > >= < <= == !=
  y3 = x and false or not (y>0)
  • If-expression
  simple  = if (x>0) x else if (x==0) 0 else -1
  complex = if (age>18)
                if (weight>100) 1
                if (weight>50)  2
                else 3
            if (age>16) 0
            else       -1     
  • User functions and generic arithmetics
  sum3(a,b,c) = a+b+c
  
  r:real = sum3(1,2,3)
  i:int  = sum3(1,2,3)
  • Array, string, numbers, structs and hi-order fun support
  out = {
    name = 'etaK'.reverse()
    values = [0xFF0F, 2_000_000, 0b100101]
    items = [1,2,3].map(rule 'item {it}')
  }
  • Strict type system and type-inference algorithm
  y = 2x
  z:int = y*x
  m:real[] = [1,2,3].map(rule it/2)
  • Double or decimal arithmetics
  • Syntax and semantic customization
  • Built-in functions
  • Comments

How to

API - guide and examples

Syntax - guide and examples

Built in functions


Boring specification is better than no specification

Boring specification: Basics

Boring specification: Operators

Boring specification: Arrays

Boring specification: Texts (Strings)

Boring specification: Structs

Boring specification: Rules (Anonymous functions)

Boring specification: Types

     ';,                                                                                ;;      
   'lO0l                                                                               'dKkc    
  c0Xk;                                                                                  cOXk:  
'dXKc                 ';;;,'                                        ',;;;'                'dXKl 
dNKc                     ',;;;;,'                              ',;;;;,'                     oXXl
XNo                          '',;;;,'                      ',;;;,''                         'kW0
WK:                               ,:::,                  ,:::,                               lNX
W0;                            ',;;;;,                    ,;;;;,'                            cXN
W0;                        ',;;;,'                            ',;;;,'                        lXN
NXl                    ,;;;;,'                                    ',;;;;,                    dWK
OWO,                  ','        ',;;;,                  ,;;;,'        ','                  :KNd
:0Nk,                        ',;;;,'                        ',;;;,'                        :0Nx,
 ,xX0c                  ',;;;,,'                                ',,;;;,'                 'oKKo' 
   cOKO:               ,;,'                                          ',;,               l0Kx;   
     :dc                                                                               'lo;     
                                                                                                
                                                                                                
                                                                                                
                                     ,;;,              ';;,                                     
                                    ;:;;:,            ,:;;:;                                    
                                  ';:,  ,:;          ;:,  ,:;'                                   
                                 ';:'    ,:;'      ';:,    '::'                                  
                                 ',       ','      ','      ','                                  

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.1 6,631 7/17/2023
1.0.0 830 12/1/2022
0.9.9 455 7/6/2022
0.9.8 434 5/29/2022
0.9.7 497 4/2/2022
0.9.6 447 4/2/2022
0.9.5 490 1/15/2022
0.9.4 321 12/26/2021
0.8.2 579 5/30/2020

Minior bugs fixed. Non-Generic calculators