Move all elements from another map into this map, leaving the other map empty.
#[test]
fn append() {
let a = box(map([("a", "A")]));
let b = map([("b", "B")]);
b.append(a); // unboxed a will copy and a.len() will be 1 still, boxed passes by ref
assertEq(a.len(), 0);
assertEq(b.len(), 2);
assertEq(b.first(), ("a", "A"));
assertEq(b.last(), ("b", "B"));
}
Map.clear(map: map):void
Clear this map, removing all elements.
#[test]
fn clear() {
let a = map([("a", "A"), (10, 100)]);
a.clear();
assertEq(a.len(), 0);
assert(a.empty());
}
Map.contains(map: map, key: unknown):bool
Does this map contain this key?
#[test]
fn contains() {
let a = map(("a", "A"), (42, "meaning of life"));
assert(a.contains("a"));
assert(a.contains(42));
assertNot(a.contains("b"));
}
Map.first(map: map):(unknown, unknown)
Returns the first key-value pair in this ordered map.
#[test]
fn first() {
let a = map([(10, 10), (9, 9), (8, 8), (7, 7)]);
assertEq(a.first(), (7, 7));
}
Map.last(map: map):(unknown, unknown)
Returns the last key-value pair in this ordered map.
#[test]
fn last() {
let a = map([(10, 10), (9, 9), (8, 8), (7, 7)]);
assertEq(a.last(), (10, 10));
}
Map.get(map: map, key: unknown):unknown
Get a value from this map, or null if the map does not contain this key.
Returns the number of items contained within this map.
#[test]
fn len() {
let a = map();
assertEq(a.len(), 0);
a.insert("a", "A");
assertEq(a.len(), 1);
}
Map.at(map: map, index: int | unknown):(unknown, unknown)
Return an item within this map at a specific index. If the index is numerical, it will be interpreted as a position within the ordered map. If the index is any other value, the behavior mimics that of the "get" function but returns a key-value pair if the item exists (null otherwise).
#[test]
fn at() {
// recommended that you use 'get', but 'at' works for non-numerical keys also
let a = map((3, 6), (1, 4), (2, 5));
assertEq(a[0], (1, 4));
assertEq(a.at(1), (2, 5));
assertEq(a[2], (3, 6));
let count = 0;
for (item in a) {
count += 1;
if (first) assertEq(item, (1, 4));
else if (last) assertEq(item, (3, 6));
else assertEq(item, (2, 5));
}
assertEq(count, 3);
}
#[test]
fn at_non_index() {
let a = map([(3, 6), (1, 4), ("hi", 5)]);
assertEq(a["hi"], ("hi", 5));
}
Remove the item in this map with the given key and return it's value, or null if the item does not exist.
#[test]
fn remove() {
let a = map([(3, 6), (1, 4), (2, 5), ("hi", 100)]);
let val: int = a.remove("hi");
assertEq(val, 100);
val = a.remove("hi");
assertNull(val);
val = a.remove(3);
assertEq(val, 6);
assertEq(a.len(), 2);
assertEq(a.first(), (1, 4));
assertEq(a.last(), (2, 5));
}
Map.retain(map: map, predicate: fn):void
Retain only the elements in this map specified by the predicate. The predicate function takes two parameters (key and value) and returns a boolean, indicating whether to keep or remove the key-value pair from the map.
#[test]
fn union() {
let a = map([('a', 'A')]);
let b = map([('b', 'B')]);
let c = a + b; // union operation
assertEq(c.toString(), '{String("a"): String("A"), String("b"): String("B")}');
}
Difference
#[test]
fn diff_map() {
let a = map([('a', 'A'), ('b', 'B')]);
let b = map([('b', 'B')]);
let c = a - b;
assertEq(c.toString(), '{String("a"): String("A")}');
}
#[test]
fn diff_vec() {
let a = map([('a', 'A'), ('b', 'B')]);
let b = ['b'];
let c = a - b;
assertEq(c.toString(), '{String("a"): String("A")}');
}
Intersection
#[test]
fn intersect_map() {
let a = map([('a', 'A'), ('b', 'B'), ('c', 'C')]);
let b = map([('c', 'C'), ('d', 'D'), ('e', 'E')]);
let c = a * b;
assertEq(c.toString(), '{String("c"): String("C")}');
}
#[test]
fn intersect_vec() {
let a = map([('a', 'A'), ('b', 'B'), ('c', 'C')]);
let b = vec('c', 'd', 'e');
let c = a * b;
assertEq(c.toString(), '{String("c"): String("C")}');
}
Symmetric Difference
#[test]
fn symmetric_difference() {
let a = map([('a', 'A'), ('b', 'B'), ('c', 'C')]);
let b = map([('c', 'C'), ('d', 'D'), ('e', 'E')]);
let c = a % b;
assertEq(c.toString(), '{String("a"): String("A"), String("b"): String("B"), String("d"): String("D"), String("e"): String("E")}');
}