abacus/abacus-ui/README.org

2.9 KiB

Scripting

Everything is an expression

Everything can be evaulated as an experession. This means variables can be set to if blocks, loops, or anything else;

let x = if y == "test" { 1 } else { 2 };

Returning output

Output is rendered based on the returned value of a block. The final value of the block is automatically returned. The return keyword is only needed if an early return is preferable.

if x == 0 {
	return 0;
}

if x > 10 {
	// note the lack of a semicolon here
	x / 5
} else {
	// note the lack of a semicolon here
	x / 10
}

Variables

Variables can be assigned using the let keyword. Variables are dynamically typed and globally mutable:

let pulse_rate = 0.8;
let time = 3.2;

time / pulse_rate

Functions

Functions can be defined using the fn keyword. Return types and arguments are dynamic and do not require typing.

fn add_one(x) {
	x + 1;
}

add_one(2)

Object maps

Dictionary style data maps are possible and can be index or accessed by a property notation. Dictionaries are defined using a special syntax wrapper: #{}.

let account = #{ first_name: "Alice", last_name: "Allison", balance: 150.32 };
// Access via property notation
account.balance += 10;
// Access via indexing
account["last_name"] = "Test";
account

Series

A series provides a powerful way to perform calculations on an array of data. Operations performedon the series are performed against all of its member values. Serieses are constructed using the series fucntion and passing an array of values.

let s = series([1,2,3,4]);
// Return an array of values calculated from the series
[ s + 10, s * s ]

Dataframes

Dataframes provide an object for working with tabular data made up of several series. Dataframes are initialzied using the dataframe constructor function and passing in an object map container name/array pairs.

Dataframe series can be accessed via indexing or property notation just like object maps.

let df = dataframe(#{
	name: ["Alice", "Bob", "Charles"],
	rate: [18,20,20],
	hours: [22,40,55]
});
df.balance = df.rate * df.hours;
df

Filtering

Dataframes are more powerful than object maps and can be filtered. Filtering requires two parts:

  1. Selecting the columns to be included
  2. providing a predicate to filter a column's value The filtering syntax is FROM <dataframe> <column array> : <array of predicates>

Predicates consist of a quoted name for the series/column an operator and a value. Valid operators are: gt, gte, lt, lte.

Filtering returns a new dataframe with the extracted values.

let df = dataframe(#{
	name: ["Alice", "Bob", "Charles"],
	rate: [18,20,20],
	hours: [22,40,55]
});
let high_rates = from df ["name", "rate", "hours"] : ["rate" gte 20];
high_rates.balance = high_rates.rate * high_rates.hours;
high_rates