Skip to content

select

Module Export

Projects each item of an iterator into a new form.

The select function transforms an iterator by applying a function to each of its items, producing a new iterator with the results. It is one of the most fundamental operations for working with collections of data.

This operation is often called map in other functional languages.

Signature

source_iterator |> select (item) => expression

Parameters

  • source_iterator: The input iterator whose items will be transformed.
  • transform_function: A function that is called one time for each item in the source_iterator.
    • item: The argument to the function, representing the current item being processed.
    • expression: The body of the function, which returns the new value for the item.

Returns

A new iterator (Iterator<B>) containing the transformed items. The new iterator will have the same number of items as the source iterator. The original source_iterator is not modified.

Examples

1. Basic Value Transformation

select can be used to perform a mathematical or logical operation on each item in a simple list.

import 'iterators'
from [1, 2, 3, 4]
|> select (x) => x * 2

Result: A new iterator is produced where each number has been doubled.

[2, 4, 6, 8]

2. Changing the Type of Items

The transform function does not need to return the same type as the input. This makes select perfect for converting or formatting data.

import 'iterators'
let user_ids = [101, 204, 315]
from
user_ids
select (id) => $'user_id:{id}'

Result: An iterator of numbers is transformed into an iterator of text values.

[
'user_id:101',
'user_id:204',
'user_id:315'
]

3. Projecting Properties from Records

A primary use case for select is to extract a specific piece of information (a property) from an iterator of complex objects or records.

import 'iterators'
let orders = [
{ id = 'ORD-001', amount = 199.99, currency = 'USD' },
{ id = 'ORD-002', amount = 49.50, currency = 'USD' },
{ id = 'ORD-003', amount = 82.00, currency = 'EUR' }
]
// Select only the 'id' from each order
from
orders
select (order) => order.id

Result: A new, simpler iterator containing only the order IDs.

[
'ORD-001',
'ORD-002',
'ORD-003'
]

4. Creating New Records

You can also use select to transform records into a completely new shape, perhaps to simplify them for a specific purpose or to combine fields.

import 'iterators'
// Using the same 'orders' list from the previous example
from
orders
select (o) => {
order_id: o.id,
display_price: $'{o.amount} {o.currency}'
}

Result: An iterator of new records with a different structure.

[
{ order_id = 'ORD-001', display_price: '199.99 USD' },
{ order_id = 'ORD-002', display_price: '49.50 USD' },
{ order_id = 'ORD-003', display_price: '82.00 EUR' }
]