Syntax
This is the structure of the language you'll be using to code nearly all steps and decisions in your pricing model, writing rules, filtering data, looking up variables and applying transforms.
Last updated
This is the structure of the language you'll be using to code nearly all steps and decisions in your pricing model, writing rules, filtering data, looking up variables and applying transforms.
Last updated
The syntax for basic mathematical operations in Swallow is the same as Excel:
+ - * / ^ > < = => <=
When entering inputs, our bot will always auto-suggest keys that already exist in your model, as well as operations or Swallow function methods that you can use to reformat the data. To use them as you're typing, just select them and press the return/enter key.
You can choose from a list of functions within Swallow which you can use in your expressions and calculations, some of them are basic mathematical operators, and others are more nuanced to pricing or insurance.
Key | Syntax | Type | Description | Example 1 | Example 2 |
---|---|---|---|---|---|
mean | mean() | method | Returns the mean value of number items | collection.map(column).mean() | [1,2,3].mean() |
count | count() | method | Returns the count of number items | collection.map(column).count() | [1,2,3].count() |
max | max() | method | Returns the max value of the number items | collection.map(column).max() | [1,2,3].max() |
range | range() | method | Returns the range in values all number items | collection.map(column).range() | [1,2,3].range() |
sum | sum() | method | Returns the sum of number items | collection.map(column).sum() | [1,2,3].sum() |
min | min() | method | Returns the min of number items | collection.map(column).min() | [1,2,3].min() |
map | map() | method | Returns a specific attribute of a data set | collection.map(column) | collection.map(column).first() |
filter | filter() | method | Returns the filtered data set | collection.filter(column=value) | collection.filter(column=input).first() |
unique | unique() | method | Returns only unique values in a data set | collection.filter(column=value).unique().count() | [1,2,3,3].unique().count() |
first | first() | method | Returns the first data item for a set | collection.map(column).first() | [1,2,3].first() |
last | last() | method | Returns the last data item for a set | collection.map(column).last() | [1,2,3].last() |
exists | exists() | method | Returns a boolean if the data item exists | collection.filter(column=value).exists() | collection.filter(column=input).exists() |
default | default() | method | Sets a default value if no data item exists | collection.filter(column=value).default(1) | collection.filter(column=input).default(bob) |
age | age() | method | Returns an age from a date of birth | date.age(YY) | date.age(YY, inception_date) |
regex | regex() | method | Returns custom regex to transform a string | string.regex(^[A-Za-z]{2}) | string.regex(^[A-Za-z]{1}) |
round | round() | method | Returns a rounded number to specified dp | round(data.map(age).sum(), 2) | round(number, 4) |
and | && | operator | This an AND operator | count = 4 && sum < 10 | count = 4 AND sum < 10 |
or | || | operator | This an OR operator | count = 4 || sum < 10 | count = 4 OR sum < 10 |
equals | == | operator | This an EQUALS operator | count == 4 | count EQUALS 4 |
not equals | != | operator | This an NOT EQUALS operator | count != 4 | count NOT EQUALS 4 |
less than | < | operator | This an LESS THAN operator | count < 4 | count LESS THAN 4 |
less than equals | < | operator | This an LESS THAN EQUALS operator | count <= 4 | count LESS THAN EQUALS 4 |
greater than | > | operator | This an GREATER THAN operator | count > 4 | count GREATER THAN 4 |
greater than equals | >= | operator | This an GREATER THAN EQUALS operator | count >= 4 | count GREATER THAN EQUALS 4 |
You'll use the language from the Syntax Glossaryin expressions throughout the Build phase, whether you're trying to store data, create rules, reformat and transform data or query other datasets you've uploaded.
In the most simple terms, your expression will normally be formatted as INPUT, then FUNCTION, then the desired TRANSFORMATION.
Here's an example of using Swallow's 'Replace' function to reformat a car's registration plate, removing spaces and forcing the whole input to be capitalised:
Output key of the INPUT you want to transform formatted in brackets e.g. {{vehicle_reg}}
Transformation you want to apply. You can use standard operators or choose from a range of pre-existing Swallow functions e.g. .replace
Desired format of the transformed data output e.g. (/\s/g, '').toUpperCase()
Structuring expressions for querying Collections must always start with the prefix command collection.
, to ensure that you're referencing the data set in your collection.
After that, you can use any of the functions from the glossary as you would normally.
Here's an example of using Swallow's 'Filter' function, combined with 'Map' and 'First' functions, to query a collection, returning a specific attribute of a data set, and formatting it to only include the first part of the datapoint.
Breaking this down:
The collection
prefix is used to ensure you're referencing the collection data set
The .filter
function allows us to select the INPUT we're using to query the collection, in this case we're using the postcode
along with which column in the Collection's table you want to filter against - in this case also {{postcode}}
The .map
function dictates which column from your collection you want to return, by referencing the column title, which in this example is ad-area
The .first
function then dictates which part of the part of the datapoint we want to return (in this case, just the first part)
To check if syntax you've written is parsing correctly, simply click off the field, or press the 'Test' button.
If the 'View' icon is green, with a tick, you know your expression is written and is parsing correctly.
If the 'View' icon is red, with a cross, you know there is an issue with your expression and you'll need to amend it.
Once expressions are correctly parsing, you'll see them surrounded by a label, representing each input or function you're using.
To debug any statement you've written, click any green 'View' icon to expand the code, allowing you to see how your test data would react to it.
Test data will automatically be generated from the 'Default' inputs you've set up in your INPUTS step. When you're editing a step, you can always see the test data or manually update it at any time in the bottom left of your sidebar.