1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
use core:debug;
use lang:bs:macro;
void intMapAdd(Int->Int to, Int[] keys, Int[] values) {
if (keys.count == values.count) {
for (Nat i = 0; i < keys.count; i++) {
to.put(keys[i], values[i]);
}
}
}
Int->Int intMapTest(Int[] keys, Int[] values) {
Int->Int result;
intMapAdd(result, keys, values);
result;
}
void strMapAdd(Str->Str to, Str[] keys, Str[] values) {
if (keys.count == values.count) {
for (Nat i = 0; i < keys.count; i++) {
to.put(keys[i], values[i]);
}
}
}
Str->Str strMapTest(Str[] keys, Str[] values) {
Str->Str result;
strMapAdd(result, keys, values);
result;
}
Int readIntMap(Int->Int src, Int k) {
if (src.has(k)) {
src.get(k);
} else {
0;
}
}
Str readStrMap(Str->Str src, Str k) {
if (src.has(k)) {
src.get(k);
} else {
"";
}
}
Str readStrLiteral(Str->Str src) {
if (src.has("A")) {
src.get("A");
} else {
"";
}
}
Str strA() {
"A";
}
Str readStrFn(Str->Str src) {
if (src.has(strA())) {
src.get(strA());
} else {
"";
}
}
Int addMap() {
Int->Int m;
m[10] = 20;
m[10];
}
Int addMapStr() {
Int->Str m;
m[10] = "25";
m[10].toInt;
}
Int iterateMap(Int->Int data) {
Int r = 0;
for (k, v in data) {
r += k*v;
}
r;
}
Int defaultMapInt() {
Int->Int m;
m[1];
}
Str defaultMapStr() {
Int->Str m;
m[1];
}
Str[] defaultMapArray() {
Map<Int, Str[]> m;
m[1];
}
Str mapAtStr(Str->Str map) {
if (v = map.at("a")) {
v;
} else {
"nothing";
}
}
Int mapAtInt(Int->Int map) {
if (v = map.at(1)) {
v;
} else {
-1;
}
}
|