Skip to content

Tuple

The tuple data type in Filtrera is one of the cornerstones of the language. It provides a way to group multiple values into a single compound value. This reference guide provides a detailed specification of the tuple data type, its syntax, and usage for users already familiar with the concept.

In Filtrera, function and filter outputs are always tuples, and parameters are tuples too. This design allows for seamless chaining of functions and filters, making Filtrera highly expressive and functional.

Tuple Definition

A tuple is defined by enclosing multiple values in parentheses. Each value in a tuple is called a component.

Examples

let person = ('Alice', 30)
from person

In this example, person is a tuple containing two components: 'Alice' and 30.

Type Notation for Tuples

The type notation for tuples is ([<label>]:<type>). Labels can be specified when defining a tuple value, allowing named access using the period (.) operator.

Example

let myTuple: (name: text, age: number) = (name = 'Alice', age = 30)

In this example, myTuple is defined as a tuple with named components name and age.

Accessing Tuple Components

Named Access

If the components of a tuple are named, they can be accessed using the period (.) operator.

Example

let myTuple = (name = 'Alice', age = 30)
let personName = myTuple.name
from personName

In this example, personName is 'Alice' because it accesses the name component of myTuple.

Index Access

If the components are not named, they can always be accessed using the component index (1-based).

Example

let myTuple = ('Alice', 30)
let firstComponent = myTuple.1
let secondComponent = myTuple.2
from firstComponent
from secondComponent

In this example, firstComponent is 'Alice' and secondComponent is 30 because they access the first and second components of myTuple, respectively.

Function and Filter Output

Function and filter outputs are always tuples. This means that any function or filter in Filtrera will return its results as a tuple.

Example

let addAndMultiply = (x, y) =>
let `sum` = x + y
let product = x * y
from `sum`
from product
let result = addAndMultiply(2, 3)
from result

In this example, addAndMultiply returns a tuple (5, 6), containing the sum and product of x and y.

Parameter Deconstruction

Parameters in Filtrera are tuples that automatically deconstruct based on the function or filter parameter type. If the parameter is a tuple with named components, each component can be accessed as a symbol within the function or filter scope.

Example

let processPerson = (name, age) =>
let message = $'Name: {name}, Age: {age}'
from message
let person = (name = 'Alice', age = 30)
let result = processPerson person
from result

In this example, processPerson takes a tuple parameter and deconstructs it into name and age symbols, which are then used to create a message.

Chaining Filters and Functions

The design of having input and output be tuples allows for chaining filters and functions with ease. The |> notation is used to define a filter, indicating the filter body during definition. When calling filters, the values are passed into the next expression automatically.

Example

let calculateSumAndProduct (x, y) |>
let sum = x + y
let product = x * y
from sum
from product
let incrementAndDouble (sum, product) |>
let incrementedSum = sum + 1
let doubledProduct = product * 2
from incrementedSum
from doubledProduct
let result = (2, 3) calculateSumAndProduct incrementAndDouble
from result

In this example, calculateSumAndProduct returns a tuple (5, 6) and incrementAndDouble processes this tuple to return (6, 12), demonstrating how functions can be chained together using tuples.

Practical Usage

Example: Tuple as Function Output

let calculateStatistics = (numbers) =>
let `sum` = numbers sum
let `count` = numbers count
let average = `sum` / `count`
from `sum`
from `count`
from average
let stats = calculateStatistics([1, 2, 3, 4, 5])
from stats

In this example, calculateStatistics returns a tuple containing the sum, count, and average of a list of numbers.

Example: Deconstructing Tuple Parameters

let displayStats(`sum`, count, average) =>
let message = $'Sum: {`sum`}, Count: {count}, Average: {average}'
from message
let stats = (`sum` = 15, count = 5, average = 3)
let result = displayStats stats
from result

In this example, displayStats takes a tuple parameter and deconstructs it into sum, count, and average symbols to create a message.

Example: Inferring Tuple Types

let myTuple = (first = 'Alice', age = 30)
from myTuple.first

In this example, myTuple is inferred to be a tuple with named components first and age, and first can be accessed using myTuple.first.

Summary

The tuple data type in Filtrera is a fundamental feature that enables the grouping of multiple values into a single compound value. By understanding its syntax and usage, you can leverage tuples to create expressive and functional programs. The automatic deconstruction of tuple parameters and the consistent use of tuples for function and filter outputs facilitate seamless chaining of operations, making Filtrera a powerful tool for complex data processing tasks. The type notation for tuples, named access, and the ability to infer types further enhance the flexibility and robustness of your Filtrera programs.