NFun 1.0.1
dotnet add package NFun --version 1.0.1
NuGet\Install-Package NFun -Version 1.0.1
<PackageReference Include="NFun" Version="1.0.1" />
paket add NFun --version 1.0.1
#r "nuget: NFun, 1.0.1"
// 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
Boring specification is better than no specification
Boring specification: Operators
Boring specification: Texts (Strings)
Boring specification: Rules (Anonymous functions)
';, ;;
'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 | Versions 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. |
-
.NETStandard 2.0
- IndexRange (>= 1.0.2)
- System.Memory (>= 4.5.5)
-
net5.0
- IndexRange (>= 1.0.2)
- System.Memory (>= 4.5.5)
-
net6.0
- IndexRange (>= 1.0.2)
- System.Memory (>= 4.5.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Minior bugs fixed. Non-Generic calculators