Functions
Functions in Filtrera are essential for defining reusable logic and encapsulating behavior. This reference guide provides a detailed specification of functions, their syntax, and usage for users already familiar with the concept.
Functions in Filtrera are defined using the => notation. Functions can take tuples as input and produce tuples as output, facilitating functional programming practices and allowing for seamless integration with filters.
Syntax
Functions are defined using the following syntax:
let <function_name> (<parameters>) => <expression>Example
let add (x, y) => x + yIn this example, add is a function that takes two parameters, x and y, and returns their sum.
Type Notation for Functions
The type notation for functions is <parameter type> => <return type>. This notation is used when explicitly setting the type of a parameter.
Example
```filtrera let applyFunction (fn: number => number, value: number) => fn(value) ```
In this example, applyFunction takes a function fn with a type of number => number and a value of type number.
Using Functions
Functions are called by passing arguments that match the parameters defined in the function. Functions can be used to perform various operations and return results.
Example
let add (x, y) => x + ylet result = add(2, 3)
from resultIn this example, the add function is called with arguments 2 and 3, returning the result 5.
Functions with Tuples
Functions can take tuples as input and produce tuples as output. This allows for more complex data transformations and processing.
Example
let calculateSumAndProduct (x, y) => let sum = x + y let product = x * y from sum from product
let result = calculateSumAndProduct(2, 3)from resultIn this example, calculateSumAndProduct takes a tuple (x, y) and returns a tuple (sum, product).
Parameter Deconstruction
When a function takes a tuple as input, the components of the tuple can be deconstructed and accessed by their names or indices within the function.
Example
let processPerson (name, age) => let message = $'Name: {name}, Age: {age}' from message
let person = (name: 'Alice', age: 30)let result = processPerson(person)
from resultIn this example, the processPerson function deconstructs the person tuple into name and age, which are then used to create a message.
Chaining Functions
The design of having input and output be tuples allows for chaining functions with ease. This is similar to how filters work.
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 resultIn 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.
Functions as Arguments
Functions can be passed to other functions as arguments, but they must be literal functions defined in-line. Functions cannot be passed by reference.
Example
let applyFunction (fn, value) => fn(value)
let result = applyFunction((x) => x * 2, 5)
from resultIn this example, the applyFunction function takes another function fn and a value value as arguments. It applies fn to value, resulting in 10.
Practical Usage
Example: Function Composition
let increment (x) => x + 1let double (x) => x * 2let toText (x) => $'{x}'
let processNumber (x) => let incremented = increment(x) let doubled = double(incremented) let text = toText(doubled) from text
let result = processNumber(2)
from resultIn this example, the processNumber function composes multiple functions to process the input 2, resulting in the text '6'.
Example: Handling Collections
let isEven (x) => x mod 2 == 0let evenNumbers (numbers) => numbers where (x => isEven(x))
let result = evenNumbers([1, 2, 3, 4, 5, 6])
from resultIn this example, the isEven function checks if a number is even, and the evenNumbers function extracts even numbers from a list using the where filter.
Summary
Functions in Filtrera provide a powerful mechanism for defining reusable logic and encapsulating behavior. By understanding their syntax and usage, including the => notation, parameter deconstruction, tuple handling, and passing literal functions as arguments, you can create complex data processing pipelines with ease. The ability to chain functions and filters together further enhances the expressive and functional capabilities of Filtrera, enabling you to build robust and flexible programs.