Skip to main content

module Map

rascal-0.40.16

Library functions for maps.

Usage

import Map;

Description

The following library functions are defined for maps:

function delete

Delete a key from a map.

map[&K,&V] delete(map[&K,&V] m, &K k)

Returns the map m minus the key k.

Examples

rascal>import Map;
ok
rascal>delete(("apple":1,"pear":2), "apple");
map[str, int]: ("pear":2)

function domain

Determine the domain (set of keys) of a map.

set[&K] domain(map[&K, &V] M)

Returns the domain (set of keys) of map M.

Examples

rascal>import Map;
ok
rascal>domain(("apple": 1, "pear": 2));
set[str]: {"pear","apple"}

function domainR

Map restricted to certain keys.

map[&K, &V] domainR(map[&K, &V] M, set[&K] S)

Return the map M restricted to pairs with key in S.

Examples

rascal>import Map;
ok
rascal>domainR(("apple": 1, "pear": 2, "orange": 3), {"apple", "pear"});
map[str, int]: ("pear":2,"apple":1)

function domainX

Map with certain keys excluded.

map[&K, &V] domainX(map[&K, &V] M, set[&K] S)

Return the map M restricted to pairs with key not in S.

Examples

rascal>import Map;
ok
rascal>domainX(("apple": 1, "pear": 2, "orange": 3), {"apple", "pear"});
map[str, int]: ("orange":3)

function getOneFrom

Get a n arbitrary key from a map.

&K getOneFrom(map[&K, &V] M)

Returns an arbitrary key of map M.

Examples

rascal>import Map;
ok
rascal>getOneFrom(("apple": 1, "pear": 2, "pineapple": 3));
str: "apple"
---
apple
---
rascal>getOneFrom(("apple": 1, "pear": 2, "pineapple": 3));
str: "apple"
---
apple
---
rascal>getOneFrom(("apple": 1, "pear": 2, "pineapple": 3));
str: "pear"
---
pear
---

function invert

Invert the (key,value) pairs in a map.

map[&V, set[&K]] invert(map[&K, &V] M)

Returns inverted map in which each value in the old map M is associated with a set of key values from the old map. Also see Invert Unique.

Examples

rascal>import Map;
ok
rascal>invert(("apple": 1, "pear": 2, "orange": 1));
map[int, set[str]]: (
1:{"orange","apple"},
2:{"pear"}
)

function invertUnique

Invert the (key,value) pairs in a map.

map[&V, &K] invertUnique(map[&K, &V] M)

Returns a map with key and value inverted; the result should be a map. If the initial map contains duplicate values, the MultipleKey exception is raised since an attempt is made to create a map where more than one value would be associated with the same key.

Also see Invert and Exception.

Examples

rascal>import Map;
ok
rascal>invertUnique(("apple": 1, "pear": 2, "orange": 3));
map[int, str]: (1:"apple",3:"orange",2:"pear")

Here is an examples that generates an exception:

rascal>invertUnique(("apple": 1, "pear": 2, "orange": 1));
|file:///home/runner/actions-runner/_work/rascal/rascal/src/org/rascalmpl/library/Map.rsc|(2659,708,<109,0>,<130,54>): MultipleKey(1,"apple","orange")
at *** somewhere ***(|file:///home/runner/actions-runner/_work/rascal/rascal/src/org/rascalmpl/library/Map.rsc|(2659,708,<109,0>,<130,54>))
at invertUnique(|prompt:///|(47,1,<1,47>,<1,48>))
ok

function isEmpty

Test whether a map is empty.

bool isEmpty(map[&K, &V] M)

Returns true if map M is empty, and false otherwise.

Examples

rascal>import Map;
ok
rascal>isEmpty(());
bool: true
rascal>isEmpty(("apple": 1, "pear": 2, "orange": 3));
bool: false

function mapper

Apply a function to all (key, value) pairs in a map.

map[&L, &W] mapper(map[&K, &V] M, &L (&K) F, &W (&V) G)

Apply the functions F and G to each key/value pair in a map and return the transformed map.

Examples

rascal>import Map;
ok
rascal>str prefix(str s) { return "X" + s; }
str (str): function(|prompt:///|(0,37,<1,0>,<1,37>))
rascal>int incr(int x) { return x + 1; }
int (int): function(|prompt:///|(0,33,<1,0>,<1,33>))
rascal>mapper(("apple": 1, "pear": 2, "orange": 3), prefix, incr);
map[str, int]: ("Xapple":2,"Xorange":4,"Xpear":3)

function range

The range (set of values that correspond to its keys) of a map.

set[&V] range(map[&K, &V] M)

Returns the range (set of values) of map M.

Examples

rascal>import Map;
ok
rascal>range(("apple": 1, "pear": 2));
set[int]: {1,2}

function rangeR

Map restricted to certain values in (key,values) pairs.

map[&K, &V] rangeR(map[&K, &V] M, set[&V] S)

Returns the map restricted to pairs with values in S.

Examples

rascal>import Map;
ok
rascal>rangeR(("apple": 1, "pear": 2, "orange": 3), {2, 3});
map[str, int]: ("pear":2,"orange":3)

function rangeX

Map with certain values in (key,value) pairs excluded.

map[&K, &V] rangeX(map[&K, &V] M, set[&V] S)

Returns the map restricted to pairs with values not in S.

Examples

rascal>import Map;
ok
rascal>rangeX(("apple": 1, "pear": 2, "orange": 3), {2, 3});
map[str, int]: ("apple":1)

function size

Number of (key, value) pairs in a map.

int size(map[&K, &V] M)

Returns the number of pairs in map M.

Examples

rascal>import Map;
ok
rascal>size(("apple": 1, "pear": 2, "orange": 3));
int: 3

function toList

Convert a map to a list of tuples.

list[tuple[&K, &V]] toList(map[&K, &V] M)

Examples

rascal>import Map;
ok
rascal>toList(("apple": 1, "pear": 2, "orange": 3));
lrel[str,int]: [
<"apple",1>,
<"orange",3>,
<"pear",2>
]

function toRel

Convert a map to a relation.

rel[&K,&V] toRel(map[&K, set[&V]] M)

rel[&K,&V] toRel(map[&K, list[&V]] M)

default java rel[&K, &V] toRel(map[&K, &V] M)

Examples

rascal>import Map;
ok
rascal>toRel(("apple": 1, "pear": 2, "orange": 3));
rel[str,int]: {
<"pear",2>,
<"orange",3>,
<"apple",1>
}

function toString

Convert a map to a string.

str toString(map[&K, &V] M)

Examples

rascal>import Map;
ok
rascal>toString(("apple": 1, "pear": 2, "orange": 3));
str: "(\"pear\":2,\"orange\":3,\"apple\":1)"
---
("pear":2,"orange":3,"apple":1)
---

function itoString

Convert a map to a indented string.

str itoString(map[&K, &V] M)

Examples

rascal>import Map;
ok
rascal>itoString(("apple": 1, "pear": 2, "orange": 3));
str: "(\"pear\":2,\"orange\":3,\"apple\":1)"
---
("pear":2,"orange":3,"apple":1)
---