Blocks
Blocks are a fundamental concept in Filtrera, providing a way to group statements and manage scope. This article will help you understand how blocks work, how they can be nested, and how scope is managed within them.
What is a Block?
In Filtrera, a block is defined by any number of statements that share the same level of indentation. Indentation can be created using either spaces or tabs. The grouping of statements within a block allows for organized and readable code.
Example of a Simple Block
statement1 statement2 statement3In the above example, statement1, statement2, and statement3 all share the same indentation, forming a single block.
Nesting Blocks
Blocks in Filtrera can be nested. This means you can have a block within another block, each with its own level of indentation.
Example of Nested Blocks
statement1 statement2 nested_statement1 nested_statement2 statement3Here, nested_statement1 and nested_statement2 form a nested block within the outer block that contains statement1, statement2, and statement3.
Scope Within Blocks
Each block in Filtrera has its own scope. This means that symbols (constants, functions, filters, etc.) defined within a block are accessible by statements within the same block, by subsequent statements, and by nested blocks.
Example of Scope
let x = 10 let y = 20
statement1 // Can access x and y statement2 // Can access x and y statement3 // Can access x and y statement4 // Can access x and yIn the above example, x and y are defined at the beginning of the block. All subsequent statements, including those within nested blocks, can access x and y.
Returning Values from Blocks
Each block in Filtrera returns values specified by its from statements. A block can have multiple from statements, each returning a single value. This allows you to capture the output of a block and use it in other parts of your code.
Example of Returning Values
let a = 5 let b = 10 from a from bThis block defines a and b, and then has two from statements that return a and b respectively.
Nested Block with Return Values
let a = 5 let b = 10
let c = a + b from c
from a from bIn this example, the nested block calculates c as the sum of a and b, and returns c. The outer block then has two from statements returning a and b.
Practical Usage
Example: Calculating Totals
Let’s say we want to calculate the total sales and total tax from a list of transactions. We can use Filtrera’s functional approach to achieve this.
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 'Total sales: {totalSales}'from 'Total tax: {totalTax}'In this example:
- We define
transactionsas a list of transaction dictionaries. totalSalesis calculated by selecting theamountfrom each transaction and summing them up.totalTaxis calculated by selecting thetaxfrom each transaction and summing them up.- The final
fromstatements return the total sales and total tax as formatted strings.
By understanding and using blocks in Filtrera, you can structure your code more effectively, manage scope with ease, and capture the results of grouped statements systematically. This makes your scripts clearer, more maintainable, and better organized.