Lambda Expression

You use a lambda expression to create an anonymous function. A lambda expression can be of any of the following two forms:

  • Expression lambda that has an expression as its body:
(input-parameters) => expression
  • Statement lambda that has a statement block as its body:
(input-parameters) => { <sequence-of-statements> }

Example:

const arr = [3, 2, 1, 4, 5];
arr.sort((a: i32, b: i32): i32 => a - b);

Map arrays

The map() method creates a new array populated with the results of calling a provided lambda on every element in the calling array.

const array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map((x: i32): i32 => x * 2);
print(map1);

Filter arrays

The filter() method creates a new array with all elements that pass the test implemented by the provided lambda.

const arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
const result = arr.filter((item: i32): boolean => item % 2 == 0);
print(result);