Filters
Filters in Filtrera are a core feature that allows for transforming and processing data in a functional and expressive way. This reference guide provides a detailed specification of filters, their syntax, and usage for users already familiar with the concept.
Filters in Filtrera are defined using the |>
notation, which indicates the filter body during definition. Filters can take tuples as input and produce tuples as output, enabling seamless chaining and data transformation.
Syntax
Filters are defined using the following syntax:
let <filter_name> (<parameters>) |> <expression>
Example
let increment (x) |> x + 1
In this example, increment
is a filter that takes a single parameter x
and returns x + 1
.
Using Filters
When using filters, values are passed into the next expression automatically. Filters can be chained together to perform complex data transformations.
Example
let increment (x) |> x + 1let double (x) |> x * 2
let result = 1 increment double
from result
In this example, the value 1
is first passed through the increment
filter, resulting in 2
, and then through the double
filter, resulting in 4
.
Filters with Tuples
Filters 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 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
takes a tuple (x, y)
and returns a tuple (sum, product)
. The incrementAndDouble
filter then takes this output tuple and processes it further.
Parameter Deconstruction
When a filter takes a tuple as input, the components of the tuple can be deconstructed and accessed by their names or indices within the filter.
Example
let processPerson (name, age) |> let message = $'Name: {name}, Age: {age}' from message
let person = (name: 'Alice', age: 30)let result = person processPerson
from result
In this example, the processPerson
filter deconstructs the person
tuple into name
and age
, which are then used to create a message.
Using the when
Keyword
Filters can use the when
keyword to include conditions. If a filter input matches the type pattern but not the condition, the filter is still allowed but will return nothing
.
Example
let incrementIfPositive (x: number when x > 0) |> x + 1
let result1 = 5 incrementIfPositivelet result2 = -3 incrementIfPositive
from result1from result2
In this example, incrementIfPositive
increments the input only if it is a positive number. For result1
, the filter returns 6
, but for result2
, the filter returns nothing
.
Practical Usage
Example: Filtering a Collection
let isEven (x) |> x mod 2 == 0let evenNumbers (numbers) |> numbers filter isEven
let result = [1, 2, 3, 4, 5, 6] evenNumbers
from result
In this example, the isEven
filter checks if a number is even, and the evenNumbers
filter extracts even numbers from a list.
Example: Complex Filter Chain
let increment (x) |> x + 1let double (x) |> x * 2let toText (x) |> '{x}'
let result = [1, 2, 3] select v => v increment select v => v double select v => v toText
from result
In this example, a list of numbers is incremented, doubled, and then converted to text using a chain of filters.
Summary
Filters in Filtrera provide a powerful mechanism for transforming and processing data. By understanding their syntax and usage, including the |>
notation, parameter deconstruction, tuple handling, and the use of the when
keyword, you can create complex data processing pipelines with ease. The ability to chain filters together further enhances the expressive and functional capabilities of Filtrera, enabling you to build robust and flexible programs.