import ballerina/io;

// Anonymous function syntax
var isOdd = function(int n) returns boolean {
    return n % 2 != 0;
};

// Function type syntax
type IntFilter function (int n) returns boolean;

// Module level function defintion
function isEven(int n) returns boolean {
    return n % 2 == 0;
}

public function main() {
    // Function `isEven` refered as a value
    IntFilter f = isEven;

    int[] nums = [1, 2, 3];

    // Arrays provide the usual functional methods: 
    // filter, map, forEach, reduce
    int[] evenNums = nums.filter(f);
    io:println(evenNums);

    // Shorthand syntax for when type is inferred and body is an expression
    int[] oddNums = nums.filter(n => n % 2 != 0);
    io:println(oddNums);
}

Function Values

Functions are values and work as closures Function type is a Separate basic type Anonymous function and type syntax look like function definition without the name.

import ballerina/io;
var isOdd = function(int n) returns boolean {
    return n % 2 != 0;
};

Anonymous function syntax

type IntFilter function (int n) returns boolean;

Function type syntax

function isEven(int n) returns boolean {
    return n % 2 == 0;
}

Module level function defintion

public function main() {
    IntFilter f = isEven;

Function isEven refered as a value

    int[] nums = [1, 2, 3];
    int[] evenNums = nums.filter(f);
    io:println(evenNums);

Arrays provide the usual functional methods: filter, map, forEach, reduce

    int[] oddNums = nums.filter(n => n % 2 != 0);
    io:println(oddNums);
}

Shorthand syntax for when type is inferred and body is an expression

bal run function_values.bal
[2]
[1,3]