F(x) Expressions
What is F(x)?
F(x) is a powerful expression evaluator integrated within Perigee, designed to enhance data manipulation and computation capabilities. Much like the "Formula" bar in Excel, F(x) allows users to input complex formulas incorporating functions, mathematical operations, column references, and parentheses. However, F(x) goes beyond basic evaluation by offering advanced features:
Custom Functionality: Users can add custom classes with functions, expanding the evaluator's capabilities to suit specific needs.
Performance Optimization: Instead of repeatedly tokenizing, parsing, and evaluating expressions, F(x) compiles expressions into Intermediate Language (IL) code. This compilation results in a direct expression tree that can be executed swiftly, making it ideal for processing large datasets efficiently.
How can I use F(x)?
Possibilities are endless, but let's examine a few cool ideas.
Enable an input box to accept math expressions allowing the user to easily double a value, or figure out what a third of their original input was.
Execute an expression across an entire DataTable, modifying the values of each row. This allows for easy data manipulation and cross references to other columns.
Enabling users to create dynamic data output for a report.
Allow the user to write their own filter on data, tables, report output, or data ingestion.
Examples
Example 1) Math expression with identifiers
In this example, we'll see how to create an Fx
class, register functions, compile a formula, and get results. We can see what the identifier callback is and how to use it.
Notice we return FxValue
? This special class handles all of the data types and conversions for the evaluator. If you're passing data in or out of Fx
, it will be in the form of a FxValue
.
Example 2) Method overrides
Just like last time we'll be compiling an expression, but this time we'll add the method override. This special override allows you inject your own method handing. In this case, we'll inject a "custom"
method that we divide the value by 100. If the requested method isn't "custom"
, then we allow fx to call the methods as it usually would with fx.CallMethod(fn, arg)
.
Example 3) Input parameters
Sometimes you need input context when processing methods, the CompileWithInputParameter allows you to do exactly this, pass in anything that conforms to the object type (including a class) and it will be available for you in the callback. This is especially useful when processing lists, tables, or modifying behavior based on the index, row, user, etc.
This time we'll be returning a string value, and prepending "name: " to it. The passed in input parameter allows us to reference our lookup dictionary from within the method override call.
Example 4) Custom functions
This time we'll add a CustomFunctions class to our project, and allow Fx to call it. We added a square root method called "root". Running this example will call the method and return the value (2.449 ...)
Example 5) Table evaluation
In this example, we'll run over a data table with a 1000 rows. We can supply each columns expression allowing us to perform various operations on each column, sequentially.
We'll also use a partitioned sum to set the Calc
column (identical to a SUM(Amount) / group by Type
).
Then finally, override the Type column with a concatenation of the Org and Type.
Valid types
Booleans values are supplied as:
true, false, yes, no
When coercing a String or Number value into a boolean, a
0, 1
will also be accepted
Numbers are supplied as whole or with a decimal point:
5, 2.5
Strings are within double quotes or single quotes:
"name", 'day'
Date values are supplied in a string:
'01-01-1900'
Timespan values are supplied variably:
01:10
is 1 hour, 10 minutes01:10:22
is 1 hour, 10 minutes, 22 seconds01:10:22:55
is 1 day 10 hours, 22 minutes, 55 seconds01:10:22:55.110
is is 1 day 10 hours, 22 minutes, 55 seconds, 110 milliseconds
Valid Operators
+ for addition
- for subtraction
* For multiplication
/ for division
^ for power
& for concatenation
% for modulus
== or = for equals
!= for not equals
All < > >= <= comparison symbols for greater/lesser than.
Default Functions
All of the functions listed below are included in the FxDefaultFunctions
class.
Number Functions
Absolute Value (abs
)
abs
)Ensures the output value is always positive.
Maximum (max
)
max
)Returns the larger of two numbers.
Minimum (min
)
min
)Returns the smaller of two numbers.
Sine (sin
)
sin
)Calculates the sine of a given angle (in radians).
Cosine (cos
)
cos
)Calculates the cosine of a given angle (in radians).
Tangent (tan
)
tan
)Calculates the tangent of a given angle (in radians).
Log Base 10 (log10
)
log10
)Calculates the base-10 logarithm of a number.
Square Root (sqrt
)
sqrt
)Calculates the square root of a number.
Round (round
)
round
)Rounds a number to a specified number of decimal places.
Places (places
)
places
)Rounds a number to a specified number of decimal places without exceeding the original value.
Ceiling (ceil
)
ceil
)Rounds a number up to the nearest integer.
Floor (floor
)
floor
)Rounds a number down to the nearest integer.
Null Handling
Null If (NullIf
)
NullIf
)Returns null
if the first value equals the fallback value; otherwise, returns the first value.
Is Null (IsNull
)
IsNull
)Returns the fallback value if the first value is null
; otherwise, returns the first value.
Conditionals
If (If
)
If
)Returns ifTrue
if the condition is true; otherwise, returns ifFalse
.
Coalesce (Coalesce
)
Coalesce
)Returns the first non-null value from the provided conditions.
Logical Operations
And (and
)
and
)Returns true
if all conditions are true; otherwise, returns false
.
Or (or
)
or
)Returns true
if any condition is true; otherwise, returns false
.
Not (not
)
not
)Inverts the boolean value of the condition.
If Error (iferror
)
iferror
)Returns the fallback value if the first value is an error; otherwise, returns the first value.
Is Error (iserror
)
iserror
)Checks if the value is an error.
Is Number (isnumber
)
isnumber
)Checks if the value is a number.
Is String (isstr
)
isstr
)Checks if the value is a string.
Is Blank (isblank
)
isblank
)Checks if the value is null
or an empty string.
Is Boolean (isbool
)
isbool
)Checks if the value is a boolean.
Financial Functions
Payment (pmt
)
pmt
)Calculates the payment for a loan based on constant payments and a constant interest rate.
Principal Payment (ppmt
)
ppmt
)Calculates the principal part of a payment for a specific period.
Interest Payment (ipmt
)
ipmt
)Calculates the interest part of a payment for a specific period.
Date Functions
Today (today
)
today
)Returns the current date.
Now (now
)
now
)Returns the current date and time.
Date (date
)
date
)Creates a date from year, month, and day.
Format (Format
)
Format
)Formats a date or time span according to the specified format.
Date Add (DateAdd
)
DateAdd
)Adds a specified number of intervals to a date.
Date Difference (DateDiff
)
DateDiff
)Calculates the difference between two dates based on the specified date part.
String Functions
Repeat (repeat
)
repeat
)Repeats a sequence of strings a specified number of times.
Concatenate (concat
)
concat
)Concatenates multiple strings into one.
Substring (substr
)
substr
)Extracts a substring from a string starting at a specified index with a specified length.
Left (left
)
left
)Returns the leftmost specified number of characters from a string.
Right (right
)
right
)Returns the rightmost specified number of characters from a string.
Trim (trim
)
trim
)Removes leading and trailing whitespace from a string.
Trim with Characters (trim
)
trim
)Removes specified characters from the start and end of a string.
Replace (replace
)
replace
)Replaces all occurrences of a specified substring with another substring.
Upper (upper
)
upper
)Converts a string to uppercase.
Lower (lower
)
lower
)Converts a string to lowercase.
Length (len
)
len
)Returns the length of a string.
Aggregate Functions
Sum Values (sumvals
)
sumvals
)Calculates the sum of multiple numerical values.
Average Values (avgvals
)
avgvals
)Calculates the average of multiple numerical values.
Last updated