A Tour of Morfa

Other operators

To be able to build symbolic expressions using symbols we are overloading the well known arithmetic operators and providing some conversions, very similar to the matrix DSL.

public func *(e1: expr, e2: expr): expr
{
    return multiply(e1, e2);
}
public func convert(s: symbol): expr
{
    return toExpr(s);
}

To that, we add some convenience operators to ease evaluation and substitution:

// evaluation
public func $()(e: expr, data: SomeDataUtility): float
{
    return e.evaluate(data);
}

// substitution
public func ${}(e: expr, subs_with: KeyValuePair<symbol, expr>[]...): expr
{
    return e.substitute(subs_with);
}

// setting-up data
public func $[]=(provider: SomeDataUtility, rhs: float, lhs: symbol): void
{
    provider.assign(lhs, rhs);
}

to be able to then use like this:

var an_expression = 'x * 'y;
var data = new SomeDataUtility;
data['x] = 5.5;
data['y] = 0.5;
var evaluation = an_expression(data);
var substitution = an_expression{'x -> 5.5, 'y -> 'z * 'u};