import ballerina/io;

public function main() {
    int[] nums = [1, 2, 3, 4];

    // The `from` clause works similar to a `foreach` statement.
    int[] numsTimes10 = from var i in nums
                        // The `select` clause is evaluated for each iteration.
                        select i * 10;

                        
    io:println(numsTimes10);

    // This is based on
    //
    // `{ 10 × i | i ∈ nums }`
    //
    // `{ i | i mod 2 = 0, i ∈ nums }`
    //
    // set builder notation.
    int[] evenNums = from var i in nums
                     where i % 2 == 0
                     select i;

    io:println(evenNums);
}

Query expressions

Query-like expressions start with a from clause and end with a select clause. It is a list comprehension, based on mathematical “set builder” notation.

import ballerina/io;
public function main() {
    int[] nums = [1, 2, 3, 4];
    int[] numsTimes10 = from var i in nums

The from clause works similar to a foreach statement.

                        select i * 10;

The select clause is evaluated for each iteration.

                        
    io:println(numsTimes10);
    int[] evenNums = from var i in nums
                     where i % 2 == 0
                     select i;

This is based on

{ 10 × i | i ∈ nums }

{ i | i mod 2 = 0, i ∈ nums }

set builder notation.

    io:println(evenNums);
}
bal run query_expressions.bal
[10,20,30,40]
[2,4]