Functions - lang.array

enumerate

Returns a new array comprising of position and member pairs.

filter

Returns a new array constructed from those elements of 'arr' for which func returns true.

forEach

Apply function func to each member of array arr.

fromBase16

Returns the byte array that str represents in Base16. str must consist of the characters 0..9, A..F, a..f and whitespace as allowed by a Ballerina Base16Literal.

fromBase64

Returns the byte array that str represents in Base64 encoding. str must consist of the characters A..Z, a..z, 0..9, +, /, = and whitespace as allowed by a Ballerina Base64Literal.

indexOf

Returns the index of first member of arr that is equal to val if there is one. Returns () if not found Equality is tested using ==

iterator

Returns an iterator over the members of arr

length

Returns the number of members contained in arr.

map

Returns a new array applying function func to each member of array arr.

pop

Remove and return the last member of the arr.

push

Add vals to end of the arr array.

reduce

Reduce operate on each member of arr using combining function func to produce a new value combining all members of arr.

remove

Removes the member of arr and index i and returns it. Panics if i is out of range.

removeAll

Removes all members of arr. Panics if any member cannot be removed.

reverse

Reverse the order of the members of arr. Returns arr.

setLength

Increase or decrease the length. setLength(arr, 0) is equivalent to removeAll(arr).

shift

Remove and return first element of the array arr.

slice

Returns a sub array starting from startIndex (inclusive) to endIndex (exclusive).

sort

Sort arr using func to order members. Returns arr.

toBase16

Returns the string representing arr using Base16. 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.

toBase64

Returns the string representing arr using Base64 encoding. 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.

unshift

Add vals to beginig of the array arr.

enumerate

(Type[] arr)

returns [int, Type]

Returns a new array comprising of position and member pairs.

Parameters

  • arr Type[]
  • the array

  • Return Type

    ([int, Type])
  • array of position, member pair

filter

(Type[] arr, function(Type) returns (boolean) func)

returns Type[]

Returns a new array constructed from those elements of 'arr' for which func returns true.

Parameters

  • arr Type[]
  • the array

  • func function(Type) returns (boolean)
  • a predicate to apply to each element to determine if it should be included

  • Return Type

    (Type[])
  • new array only containig members which evaluate function 'func' to true

forEach

Apply function func to each member of array arr.

Parameters

  • arr Type[]
  • the array

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

fromBase16

(string str)

returns byte[] | error

Returns the byte array that str 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

(string str)

returns byte[] | error

Returns the byte array that str represents in Base64 encoding. 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

(PureType[] arr, PureType val, int startIndex)

returns int?

Returns the index of first member of arr that is equal to val if there is one. Returns () if not found Equality is tested using ==

Parameters

  • startIndex int - 0
  • index to start the search from

  • Return Type

    (int?)
  • index of the member if found, else ()

iterator

(Type[] arr)

returns $anonType$2

Returns an iterator over the members of arr

Parameters

  • arr Type[]
  • the array

length

(any | error arr)

returns int

Returns the number of members contained in arr.

Parameters

  • Return Type

    (int)
  • number of members in the array

map

(Type[] arr, function(Type) returns (Type1) func)

returns Type1[]

Returns a new array applying function func to each member of array arr.

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 function func to each member

pop

(Type[] arr)

returns Type

Remove and return the last member of the arr.

Parameters

  • arr Type[]
  • the array

  • Return Type

    (Type)
  • removed member

push

Add vals to end of the arr array.

Parameters

  • arr Type[]
  • the array

  • vals Type[]
  • values to add to the end of the array

reduce

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

returns Type1

Reduce operate on each member of arr using combining function func to produce a new value combining all members of arr.

Parameters

  • arr Type[]
  • the array

  • initial Type1
  • initial value to first evaluation of combining function func

  • Return Type

    (Type1)
  • result of applying combining function to each member of the array

    Example: Emulating sum function.

     var ar = [1, 2, 3];
     var a = ar.reduce(function (int i, int j) returns int { return i + j; }, 0);
    

    Example: Emulating map behavior.

     var ar = [1, 2, 3];
     int[] newArr = [];
     int[] a = ar.reduce(function (int[] a, int j) returns int[] { a.push(j*2); return a; }, newArr);
    

remove

(Type[] arr, int i)

returns Type

Removes the member of arr and index i and returns it. Panics if i is out of range.

Parameters

  • arr Type[]
  • the array

  • i int
  • index of member to be removed

  • Return Type

    (Type)
  • removed member

removeAll

Removes all members of arr. Panics if any member cannot be removed.

Parameters

reverse

(Type[] arr)

returns Type[]

Reverse the order of the members of arr. Returns arr.

Parameters

  • arr Type[]
  • the array to be reversed

  • Return Type

    (Type[])
  • reversed arr

setLength

Increase or decrease the length. setLength(arr, 0) is equivalent to removeAll(arr).

Parameters

  • i int
  • new length

shift

(Type[] arr)

returns Type

Remove and return first element of the array arr.

Parameters

  • arr Type[]
  • the array

  • Return Type

    (Type)
  • removed member

slice

(Type[] arr, int startIndex, int endIndex)

returns Type[]

Returns a sub array starting from startIndex (inclusive) to endIndex (exclusive).

Parameters

  • arr Type[]
  • the array

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

  • endIndex int
  • index of first member not to include in the slice

  • Return Type

    (Type[])
  • array slice within specified range

sort

(Type[] arr, function(Type, Type) returns (int) func)

returns Type[]

Sort arr using func to order members. Returns arr.

Parameters

  • arr Type[]
  • the array

  • func function(Type, Type) returns (int)
  • comparator function

  • Return Type

    (Type[])
  • sorted arr

toBase16

(byte[] arr)

returns string

Returns the string representing arr using Base16. 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

(byte[] arr)

returns string

Returns the string representing arr using Base64 encoding. 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

unshift

Add vals to beginig of the array arr.

Parameters

  • arr Type[]
  • the array

  • vals Type[]
  • values to add to the start of the array