from Statement
The from
statement in Filtrera is used to specify the values that a block should return. Each block can contain multiple from
statements, and the output of the block is a tuple with one component for each from
statement, in the order of their appearance. This reference guide provides a detailed specification of the from
statement, its syntax, and usage for users already familiar with the concept.
Syntax
The basic syntax for the from
statement is:
from <expression>
Parameters
<expression>
: The value or expression to be returned by thefrom
statement. This can be a constant, a calculation, a function result, or any valid Filtrera expression.
Examples
Single from
Statement
let x = 10from x
This block returns a single-component tuple (10)
.
Multiple from
Statements
let x = 10let y = 20
from xfrom y
This block returns a tuple (10, 20)
, with x
and y
as its components.
Tuple Output
When a block contains multiple from
statements, the output is a tuple with one component for each from
statement, in the order of appearance in the file. If there is only one from
statement, the output is still a tuple, known as a singleton tuple.
Example
let width = 5let height = 10let area = width * height
from widthfrom heightfrom area
In this example, the block returns a tuple (5, 10, 50)
corresponding to width
, height
, and area
.
Singleton Tuple
Even with a single from
statement, the block’s output is a tuple:
let total = 100
from total
This block returns a singleton tuple (100)
.
Nesting and Scope
The from
statements respect the scope of their containing block. Values returned by nested blocks are part of the outer block’s tuple only if the from
statement is within the scope of the outer block and the nested block is correctly preceded by an expression or statement that expects the block’s output.
Example
let result = let a = 1 let b = 2
let sum = let c = a + b from c
from a from b from sum
This example returns the tuple (1, 2, 3)
, where c
is calculated and returned within the nested block, and a
, b
, and sum
are returned in the outer block.
Practical Usage
Example: Calculating and Returning Multiple Values
let transactions = [ { amount = 100, tax = 5 }, { amount = 200, tax = 10 }, { amount = 300, tax = 15 }]
let totalSales = transactions select t => t.amount sum
let totalTax = transactions select t => t.tax sum
from totalSalesfrom totalTax
In this example:
transactions
is defined as a list of transaction dictionaries.totalSales
is calculated by summing theamount
from each transaction.totalTax
is calculated by summing thetax
from each transaction.- The block returns a tuple with
totalSales
andtotalTax
.
Using Tuples with Functions and Filters
One powerful feature of returning tuples from blocks is that functions and filters can take a tuple as input and automatically destructure it into their parameters. This means you do not have to explicitly destructure and construct function/filter arguments.
Example
let processData = (sales, tax) => let total = sales + tax from total
let transactions = [ { amount = 100, tax = 5 }, { amount = 200, tax = 10 }]
let results = transactions select t => (t.amount, t.tax) select t => processData t
from results
In this example, processData
is a function that takes a tuple (sales, tax)
, calculates the total, and returns it. The transactions
list is processed to create tuples of amount
and tax
, which are then passed to processData
without explicit destructuring.
Best Practices
- Order of
from
Statements: Ensurefrom
statements are in the desired order to produce the correct tuple output. - Descriptive Expressions: Use clear and meaningful expressions in
from
statements to enhance readability and maintainability. - Consistent Indentation: Maintain consistent indentation for readability and to correctly define block scopes.
Summary
The from
statement is a fundamental feature in Filtrera for specifying block return values. By understanding its syntax and usage, you can control the output of your blocks effectively. Leveraging multiple from
statements allows you to return tuples, providing a structured and organized way to manage block outputs. Understanding the scope and order of from
statements is key to producing the desired results in your Filtrera programs. The ability to automatically destructure tuples in functions and filters further enhances the flexibility and power of your code.