Functions

'map
I

Applies a function to each member of an array and returns an array of the results.

enumerate
I

Returns a new array consisting of index and member pairs.

filter
I

Selects the members from an array for which a function returns true.

forEach
I

Applies a function to each member of an array.

fromBase16
I

Returns the byte array that a string represents in Base16.

fromBase64
I

Returns the byte array that a string represents in Base64.

indexOf
I

Returns the index of first member of an array that is equal to a given value if there is one.

iterator
I

Returns an iterator over an array.

lastIndexOf
I

Returns the index of last member of an array that is equal to a given value if there is one.

length
I

Returns the number of members of an array.

pop
I

Removes and returns the last member of an array.

push
I

Adds values to the end of an array.

reduce
I

Combines the members of an array using a combining function.

remove
I

Removes a member of an array.

removeAll
I

Removes all members of an array.

reverse
I

Reverses the order of the members of an array.

setLength
I

Changes the length of an array.

shift
I

Removes and returns first member of an array.

slice
I

Returns a subarray using a start index (inclusive) and an end index (exclusive).

sort
I

Sorts an array.

toBase16
I

Returns the string that is the Base16 representation of an array of bytes.

toBase64
I

Returns the string that is the Base64 representation of an array of bytes.

toStream
I

Returns a stream from the given array.

unshift
I

Adds values to the start of an array.

'map

function 'map(Type[ ] arr,  function(Type) returns (Type1) func) returns Type1[ ]
Isolated Function

Applies a function to each member of an array and returns an array of the results.

Parameters

  • arr Type[ ]
  • the array

  • func function(Type) returns (Type1)
  • a function to apply to each member

Return Type

(Type1[ ])

new array containing result of applying parameter func to each member of parameter arr in order

enumerate

function enumerate(Type[ ] arr) returns [int, Type][ ]
Isolated Function

Returns a new array consisting of index and member pairs.

Parameters

  • arr Type[ ]
  • the array

Return Type

([int, Type][ ])

array of index, member pairs

filter

function filter(Type[ ] arr,  function(Type) returns (boolean) func) returns Type[ ]
Isolated Function

Selects the members from an array for which a function returns true.

Parameters

  • arr Type[ ]
  • the array

  • func function(Type) returns (boolean)
  • a predicate to apply to each member to test whether it should be selected

Return Type

(Type[ ])

new array only containing members of parameter arr for which parameter func evaluates to true

forEach

function forEach(Type[ ] arr,  function(Type) returns (() ) func) returns () 
Isolated Function

Applies a function to each member of an array.

The parameter func is applied to each member of parameter arr in order.

Parameters

  • arr Type[ ]
  • the array

  • func function(Type) returns (() )
  • a function to apply to each member

Return Type

(() )

fromBase16

function fromBase16(string str) returns byte[ ] | error
Isolated Function

Returns the byte array that a string represents in Base16.

str must consist of the characters 0..9, A..F, a..f and whitespace as allowed by a Ballerina Base16Literal.

Parameters

  • str string
  • Base16 string representation

Return Type

(byte[ ] | error)

the byte array or error

fromBase64

function fromBase64(string str) returns byte[ ] | error
Isolated Function

Returns the byte array that a string represents in Base64.

parameter str must consist of the characters A..Z, a..z, 0..9, +, /, = and whitespace as allowed by a Ballerina Base64Literal.

Parameters

  • str string
  • Base64 string representation

Return Type

(byte[ ] | error)

the byte array or error

indexOf

function indexOf(AnydataType[ ] arr, AnydataType val, int startIndex) returns int?
Isolated Function

Returns the index of first member of an array that is equal to a given value if there is one. Returns () if not found. Equality is tested using ==.

Parameters

  • startIndex int (default 0)
  • index to start the search from

Return Type

(int?)

index of the member if found, else ()

iterator

function iterator(Type[ ] arr) returns object {
    public isolated function next() returns record {|
        Type value;
    |}?;
} 
Isolated Function

Returns an iterator over an array.

Parameters

  • arr Type[ ]
  • the array

Return Type

(object { public isolated function next() returns record {| Type value; |}?; } )

a new iterator object that will iterate over the members of parameter arr.

lastIndexOf

function lastIndexOf(AnydataType[ ] arr, AnydataType val, int startIndex) returns int?
Isolated Function

Returns the index of last member of an array that is equal to a given value if there is one. Returns () if not found. Equality is tested using ==.

Parameters

  • startIndex int (default arr.length() - 1)
  • index to start searching backwards from

Return Type

(int?)

index of the member if found, else ()

length

function length((any | error)[ ] arr) returns int
Isolated Function

Returns the number of members of an array.

Parameters

  • arr (any | error)[ ]
  • the array

Return Type

(int)

number of members in parameter arr

pop

function pop(Type[ ] arr) returns Type
Isolated Function

Removes and returns the last member of an array.

The array must not be empty.

Parameters

  • arr Type[ ]
  • the array

Return Type

(Type)

removed member

push

function push(Type[ ] arr, Type... vals) returns () 
Isolated Function

Adds values to the end of an array.

Parameters

  • arr Type[ ]
  • the array

  • vals Type...
  • values to add to the end of the array

Return Type

(() )

reduce

function reduce(Type[ ] arr,  function(Type1, Type) returns (Type1) func, Type1 initial) returns Type1
Isolated Function

Combines the members of an array using a combining function.

The combining function takes the combined value so far and a member of the array, and returns a new combined value.

For example

1reduce([1, 2, 3], function (int total, int n) returns int { return total + n; }, 0)

is the same as sum(1, 2, 3).

Parameters

  • arr Type[ ]
  • the array

  • initial Type1
  • initial value for the first argument of combining parameter func

Return Type

(Type1)

result of combining the members of parameter arr using parameter func

remove

function remove(Type[ ] arr, int index) returns Type
Isolated Function

Removes a member of an array.

This removes the member of parameter arr with index parameter index and returns it. It panics if there is no such member.

Parameters

  • arr Type[ ]
  • the array

  • index int
  • index of member to be removed from parameter arr

Return Type

(Type)

the member of parameter arr that was at parameter index

removeAll

function removeAll((any | error)[ ] arr) returns () 
Isolated Function

Removes all members of an array.

Panics if any member cannot be removed.

Parameters

  • arr (any | error)[ ]
  • the array

Return Type

(() )

reverse

function reverse(Type[ ] arr) returns Type[ ]
Isolated Function

Reverses the order of the members of an array.

Parameters

  • arr Type[ ]
  • the array to be reversed

Return Type

(Type[ ])

parameter arr with its members in reverse order

setLength

function setLength((any | error)[ ] arr, int length) returns () 
Isolated Function

Changes the length of an array.

setLength(arr, 0) is equivalent to removeAll(arr).

Parameters

  • arr (any | error)[ ]
  • the array of which to change the length

  • length int
  • new length

Return Type

(() )

shift

function shift(Type[ ] arr) returns Type
Isolated Function

Removes and returns first member of an array.

The array must not be empty.

Parameters

  • arr Type[ ]
  • the array

Return Type

(Type)

the value that was the first member of the array

slice

function slice(Type[ ] arr, int startIndex, int endIndex) returns Type[ ]
Isolated Function

Returns a subarray using a start index (inclusive) and an end index (exclusive).

Parameters

  • arr Type[ ]
  • the array

  • startIndex int
  • index of first member to include in the slice

  • endIndex int (default arr.length())
  • index of first member not to include in the slice

Return Type

(Type[ ])

array slice within specified range

sort

function sort(Type[ ] arr, SortDirection direction, ( function(Type) returns (OrderedType))? key) returns Type[ ]
Isolated Function

Sorts an array.

If the member type of the array is not ordered, then the parameter key function must be specified. Sorting works the same as with the parameter sort clause of query expressions.

Parameters

  • arr Type[ ]
  • the array to be sorted;

  • direction SortDirection (default ASCENDING)
  • direction in which to sort

  • key ( function(Type) returns (OrderedType))? (default ())
  • function that returns a key to use to sort the members

Return Type

(Type[ ])

a new array consisting of the members of parameter arr in sorted order

toBase16

function toBase16(byte[ ] arr) returns string
Isolated Function

Returns the string that is the Base16 representation of an array of bytes.

The representation is the same as used by a Ballerina Base16Literal. The result will contain only characters 0..9, a..f. There will be no whitespace in the returned string.

Parameters

  • arr byte[ ]
  • the array

Return Type

(string)

Base16 string representation

toBase64

function toBase64(byte[ ] arr) returns string
Isolated Function

Returns the string that is the Base64 representation of an array of bytes.

The representation is the same as used by a Ballerina Base64Literal. The result will contain only characters A..Z, a..z, 0..9, +, / and =. There will be no whitespace in the returned string.

Parameters

  • arr byte[ ]
  • the array

Return Type

(string)

Base64 string representation

toStream

function toStream(Type[ ] arr) returns stream<Type>
Isolated Function

Returns a stream from the given array.

Parameters

  • arr Type[ ]
  • The array from which the stream is created

Return Type

(stream<Type>)

The stream representation of the array arr

unshift

function unshift(Type[ ] arr, Type... vals) returns () 
Isolated Function

Adds values to the start of an array.

The values newly added to the array will be in the same order as they are in parameter vals.

Parameters

  • arr Type[ ]
  • the array

  • vals Type...
  • values to add to the start of the array

Return Type

(() )