lang.map
ballerina/lang.map Ballerina library
Module overview
The lang.map
module corresponds to the mapping
basic type.
Functions
'map
function 'map(map<any|error> m, function(any|error) returns (any|error)
func) returns map<any|error>
Applies a function each member of a map and returns a map of the result.
The resulting map will have the same keys as the argument map.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.map(m => m > 50) ⇒ {"Carl":true,"Bob":false,"Max":true}
Parameters
entries
Returns a map containing [key, member] pair as the value for each key.
{"Carl": 85, "Bob": 50}.entries() ⇒ {"Carl":["Carl",85],"Bob":["Bob",50]}
filter
function filter(map<any|error> m, function(any|error) returns (boolean)
func) returns map<any|error>
Selects the members from a map for which a function returns true.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.filter(m => m >= 60) ⇒ {"Carl":85,"Max":60}
Parameters
forEach
Applies a function to each member of a map.
The parameter func
is applied to each member of parameter m
.
int total = 0; {"Carl": 85, "Bob": 50, "Max": 60}.forEach(function (int m) { total += m; }); total ⇒ 195
get
Returns the member of a map with given key.
This for use in a case where it is known that the map has a specific key,
and accordingly panics if parameter m
does not have a member with parameter k
key.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.get("Carl") ⇒ 85 marks.get("John") ⇒ panic
Return Type
- any|error - member with parameter
k
key
hasKey
Tests whether a map value has a member with a given key.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.hasKey("Carl") ⇒ true marks.hasKey("John") ⇒ false
Return Type
- boolean - true if parameter
m
has a member with key parameterk
iterator
function iterator(map<any|error> m) returns object {
public isolated function next() returns record {|
Type value;
|}?;
}
Returns an iterator over a map.
The iterator will iterate over the members of the map not the keys.
The function entries
can be used to iterate over the keys and members together.
The function keys
can be used to iterator over just the keys.
object { public isolated function next() returns record {|int value;|}?; } iterator = {"Carl": 85, "Bob": 50, "Max": 60}.iterator(); iterator.next() ⇒ {"value":85}
Return Type
- object {
public isolated function next() returns record {|
Type value;
|}?;
} - a new iterator object that will iterate over the members of parameter
m
keys
Returns a list of all the keys of a map.
{"Carl": 85, "Bob": 50, "Max": 60}.keys() ⇒ ["Carl","Bob","Max"]
Return Type
- string[] - a new list of all keys
length
Returns number of members of a map.
{"Carl": 85, "Bob": 50, "Max": 60}.length() ⇒ 3
Return Type
- int - number of members in parameter
m
reduce
function reduce(map<any|error> m, function(any|error, any|error) returns (any|error)
func, any|error initial) returns any|error
Combines the members of a map using a combining function.
The combining function takes the combined value so far and a member of the map, and returns a new combined value.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.reduce(isolated function (int total, int next) returns int => total + next, 0) ⇒ 195
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
m
using parameterfunc
remove
Removes a member of a map.
This removes the member of parameter m
with key parameter k
and returns it.
It panics if there is no such member.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.remove("Carl") ⇒ 85 marks ⇒ {"Bob":50,"Max":60} marks.remove("John") ⇒ panic
Return Type
- any|error - the member of parameter
m
that had key parameterk
removeAll
Removes all members of a map.
This panics if any member cannot be removed.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.removeAll(); marks ⇒ {} map<int> values = <record {|int x; int y;|}> {x: 10, y: 20}; values.removeAll() ⇒ panic;
removeIfHasKey
Removes a member of a map with a given key, if the map has member with the key.
If parameter m
has a member with key parameter k
, it removes and returns it;
otherwise it returns ()
.
map<int> marks = {"Carl": 85, "Bob": 50, "Max": 60}; marks.removeIfHasKey("Carl") ⇒ 85 marks ⇒ {"Bob":50,"Max":60} marks.removeIfHasKey("John") is () ⇒ true
Return Type
- any|error? - the member of parameter
m
that had key parameterk
, or()
if parameterm
does not have a key parameterk
toArray
Returns a list of all the members of a map.
{"Carl": 85, "Bob": 50, "Max": 60}.toArray() ⇒ [85,50,60]
Return Type
- any|error[] - an array whose members are the members of parameter
m
Union types
lang.map: 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.
lang.map: Type1
Type1
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.