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<any|error>> t, function(MapType<any|error>) returns (MapType1<any|error>)
func) returns table<MapType1<any|error>>
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
add
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<any|error>> t, function(MapType<any|error>) returns (boolean)
func) returns table<MapType<any|error>>
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
forEach
function forEach(table<MapType<any|error>> 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
get
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
Return Type
- MapType<any|error> - member with key parameter
k
hasKey
Tests 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
Return Type
- boolean - true if parameter
t
has a member with key parameterk
iterator
function iterator(table<MapType<any|error>> 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"}}
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
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"]
Return Type
- KeyType[] - a new list of all keys
length
Returns number of members of a table.
table<record {|string name;|}> students = table [ {name: "Jo"}, {name: "Smith"} ]; students.length() ⇒ 2
Return Type
- int - number of members in parameter
t
nextKey
Returns 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
Return Type
- int - an integer not yet used as a key
put
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<any|error>> t, function(any|error, MapType<any|error>) returns (any|error)
func, any|error initial) returns any|error
Combines 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
- initial any|error - initial value for the first argument of combining parameter
func
Return Type
- any|error - result of combining the members of parameter
t
using parameterfunc
remove
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
Return Type
- MapType<any|error> - the member of parameter
t
that had key parameterk
removeAll
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
removeIfHasKey
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
Return Type
- MapType<any|error>? - the member of parameter
t
that had key parameterk
, or()
if parametert
does not have a key parameterk
toArray
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"}]
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.
Map types
lang.table: MapType
MapType
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.
lang.table: MapType1
MapType1
A type parameter that is a subtype of map<any|error>
.
Has the special semantic that when used in a declaration
all uses in the declaration must refer to same type.