lang.table
ballerina/lang.table Ballerina library
Module overview
The lang.table module corresponds to the table basic type.
Functions
'map
function 'map(table<MapType> t, function(MapType<any|error>) returns (MapType1<any|error>) func) returns table<MapType1> key<never> Applies a function to each member of a table and returns a table of the result.
table<record {|int id; int math; int physics;|}> students = table [ {id: 1, math: 78, physics: 70}, {id: 2, math: 83, physics: 80} ]; students.map(student => {id: student.id, avg: (student.math + student.physics) / 2}) ⇒ [{"id":1,"avg":74},{"id":2,"avg":81}]
Parameters
- t table<MapType> - the table
Return Type
- table<MapType1> key<never> - new table containing result of applying parameter
functo each member
add
function add(table<MapType> t, MapType<any|error> val)Adds a member to a table.
It will be added as the last member.
It panics if parameter val has the same key as an existing member of parameter t,
or if parameter val is inconsistent with the inherent type of t.
table<record {|int id; string name;|}> employees = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; employees.add({id: 1, name: "Pat"}); employees ⇒ [{"id":1,"name":"Jo"},{"id":2,"name":"Sam"},{"id":1,"name":"Pat"}] table<record {|readonly int id; string name;|}> key(id) students = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; students.add({id: 3, name: "Pat"}); students ⇒ [{"id":1,"name":"Jo"},{"id":2,"name":"Sam"},{"id":3,"name":"Pat"}] students.add({id: 1, name: "James"}) ⇒ panic table<record {readonly int id;}> key(id) studentIds = students; studentIds.add({id: 7}) ⇒ panic
filter
function filter(table<MapType> key<KeyType> t, function(MapType<any|error>) returns (boolean) func) returns table<MapType> key<KeyType> Selects the members from a table for which a function returns true.
The resulting table will have the same keys as the argument table.
table<record {|int id; int salary;|}> employees = table [ {id: 1, salary: 1200}, {id: 2, salary: 1100}, {id: 3, salary: 800} ]; employees.filter(emp => emp.salary < 1000) ⇒ [{"id":3,"salary":800}]
Parameters
- t table<MapType> key<KeyType> - the table
Return Type
- table<MapType> key<KeyType> - new table containing members for which parameter
funcevaluates to true
forEach
function forEach(table<MapType> t, function(MapType<any|error>) returns (() ) func) returns () Applies a function to each member of a table.
The parameter func is applied to each member of parameter t.
table<record {|string name; int salary;|}> employees = table [ {name: "Jo", salary: 1200}, {name: "Emma", salary: 800} ]; int total = 0; employees.forEach(function(record {|string name; int salary;|} emp) { total += emp.salary; }); total ⇒ 2000
Parameters
- t table<MapType> - the table
- func
function(MapType<any|error>) returns (() )- a function to apply to each member
get
function get(table<MapType> key<KeyType> t, KeyType k) returns MapType<any|error>Returns the member of an table with a particular key.
This for use in a case where it is known that the table has a specific key,
and accordingly panics if parameter t does not have a member with key parameter k.
table<record {|readonly string index; string name;|}> key(index) students = table [ {index: "220001CS", name: "Jo"}, {index: "220002CS", name: "Sam"} ]; students.get("220002CS") ⇒ {"index":"220002CS","name":"Sam"} students.get("110002CS") ⇒ panic
Parameters
- t table<MapType> key<KeyType> - the table
- k KeyType - the key
Return Type
- MapType<any|error> - member with key parameter
k
hasKey
function hasKey(table<MapType> key<KeyType> t, KeyType k) returns booleanTests whether a table has a member with a particular key.
table<record {|readonly int id; string name;|}> key(id) students = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; students.hasKey(1) ⇒ true students.hasKey(5) ⇒ false
Parameters
- t table<MapType> key<KeyType> - the table
- k KeyType - the key
Return Type
- boolean - true if parameter
thas a member with key parameterk
iterator
function iterator(table<MapType> t) returns object {
public isolated function next() returns record {|
MapType value;
|}?;
} Returns an iterator over a table.
The iterator will iterate over the members of the table not the keys.
The entries function can be used to iterate over the keys and members together.
The keys function can be used to iterator over just the keys.
object { public isolated function next() returns record {|record {|string name;|} value;|}?; } iterator = table [ {name: "Jo"}, {name: "Smith"} ].iterator(); iterator.next() ⇒ {"value":{"name":"Jo"}}
Parameters
- t table<MapType> - the table
Return Type
- object {
public isolated function next() returns record {|
MapType value;
|}?;
} - a new iterator object that will iterate over the members of parameter
t
keys
function keys(table<map<any|error>> key<KeyType> t) returns KeyType[]Returns a list of all the keys of a table.
table<record {|readonly string code; string name;|}> key(code) countries = table [ {code: "CAN", name: "Canada"}, {code: "DNK", name: "Denmark"}, {code: "NPL", name: "Nepal"} ]; countries.keys() ⇒ ["CAN","DNK","NPL"]
Parameters
- t table<map<any|error>> key<KeyType> - the table
Return Type
- KeyType[] - a new list of all keys
length
function length(table<map<any|error>> t) returns intReturns number of members of a table.
table<record {|string name;|}> students = table [ {name: "Jo"}, {name: "Smith"} ]; students.length() ⇒ 2
Parameters
- t table<map<any|error>> - the table
Return Type
- int - number of members in parameter
t
nextKey
function nextKey(table<MapType> key<int> t) returns intReturns the next available integer key.
This is maximum used key value + 1, or 0 if no key used XXX should it be 0, if the maximum used key value is < 0? Provides similar functionality to auto-increment
table<record {|readonly int id; string name;|}> key(id) users = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; users.nextKey() ⇒ 3
Parameters
- t table<MapType> key<int> - the table with a key of type int
Return Type
- int - an integer not yet used as a key
put
function put(table<MapType> t, MapType<any|error> val)Adds a member to a table value, replacing any member with the same key value.
If parameter val replaces an existing member, it will have the same position
in the order of the members as the existing member;
otherwise, it will be added as the last member.
It panics if parameter val is inconsistent with the inherent type of parameter t.
table<record {|int id; string name;|}> employees = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; employees.put({id: 1, name: "Pat"}); employees ⇒ [{"id":1,"name":"Jo"},{"id":2,"name":"Sam"},{"id":1,"name":"Pat"}] table<record {|readonly int id; string name;|}> key(id) students = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; students.put({id: 3, name: "Pat"}); students ⇒ [{"id":1,"name":"Jo"},{"id":2,"name":"Sam"},{"id":3,"name":"Pat"}] students.put({id: 1, name: "Kane"}); students ⇒ [{"id":1,"name":"Kane"},{"id":2,"name":"Sam"},{"id":3,"name":"Pat"}] table<record {readonly int id;}> key(id) studentIds = students; studentIds.put({id: 7}) ⇒ panic
reduce
function reduce(table<MapType> t, function(any|error, MapType<any|error>) returns (any|error) func, any|error initial) returns any|errorCombines the members of a table using a combining function.
The combining function takes the combined value so far and a member of the table, and returns a new combined value.
table<record {int id; int salary;}> employees = table [ {id: 1, salary: 1200}, {id: 2, salary: 1100}, {id: 3, salary: 800} ]; employees.reduce(isolated function (int total, record {int id; int salary;} next) returns int => total + next.salary, 0) ⇒ 3100
Parameters
- t table<MapType> - the table
- initial any|error - initial value for the first argument of combining parameter
func
Return Type
- any|error - result of combining the members of parameter
tusing parameterfunc
remove
function remove(table<MapType> key<KeyType> t, KeyType k) returns MapType<any|error>Removes a member of a table.
This removed the member of parameter t with key parameter k and returns it.
It panics if there is no such member.
table<record {|readonly int id; string name;|}> key(id) students = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; students.remove(1) ⇒ {"id":1,"name":"Jo"} students ⇒ [{"id":2,"name":"Sam"}] students.remove(5) ⇒ panic
Parameters
- t table<MapType> key<KeyType> - the table
- k KeyType - the key
Return Type
- MapType<any|error> - the member of parameter
tthat had key parameterk
removeAll
function removeAll(table<map<any|error>> t) returns () Removes all members of a table.
This panics if any member cannot be removed.
table<record {|string name;|}> students = table [ {name: "Jo"}, {name: "Sam"} ]; students.removeAll() is () ⇒ true students ⇒ [] table<record {|int score;|}> scores = <readonly> table [ {score: 30}, {score: 40} ]; scores.removeAll() ⇒ panic
Parameters
- t table<map<any|error>> - the table
removeIfHasKey
function removeIfHasKey(table<MapType> key<KeyType> t, KeyType k) returns MapType<any|error>?Removes a member of a table with a given key, if the table has member with the key.
If parameter t has a member with key parameter k, it removes and returns it;
otherwise it returns ().
table<record {|readonly int id; string name;|}> key(id) students = table [ {id: 1, name: "Jo"}, {id: 2, name: "Sam"} ]; students.removeIfHasKey(1) ⇒ {"id":1,"name":"Jo"} students ⇒ [{"id":2,"name":"Sam"}] students.removeIfHasKey(3) is () ⇒ true
Parameters
- t table<MapType> key<KeyType> - the table
- k KeyType - the key
Return Type
- MapType<any|error>? - the member of parameter
tthat had key parameterk, or()if parametertdoes not have a key parameterk
toArray
function toArray(table<MapType> t) returns MapType<any|error>[]Returns a list of all the members of a table.
table [ {code: "CAN", name: "Canada"}, {code: "DNK", name: "Denmark"}, {code: "NPL", name: "Nepal"} ].toArray() ⇒ [{"code":"CAN","name":"Canada"},{"code":"DNK","name":"Denmark"},{"code":"NPL","name":"Nepal"}]
Parameters
- t table<MapType> - the table
Return Type
- MapType<any|error>[] - an array whose members are the members of parameter
t
Union types
lang.table: Type
Type
A type parameter that is a subtype of any|error.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.
Anydata types
lang.table: KeyType
KeyType
A type parameter that is a subtype of anydata.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.