Swan Lake Beta2 is here.
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 from clause and end with 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]
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 from clause and end with 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]

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand