# The fixes from the slides, as one program that runs: sigils and names# (1, 2), the habits (3), comparisons instead of truthiness (5), a guarded# range (7), raw strings for braces (8) and a map key tested first (bonus).use io;
use maps;
defconstMAX_RETRIESasintinit3;
def x asintinit5;
$x = $x + 1; # 3: no +=, no ++io.printf("%d of %d\n", $x, MAX_RETRIES); # 1: $x, constants barefuncrange(lo asint, hi asint) { return$hi - $lo; } # 2: lo / hidef items aslistofintinit [];
def myVar asintinitrange(2, 7);
io.printf("%d %d\n", len($items), $myVar);
# 3: and / or / not, # comments, elseifdef a asboolinittrue;
def n asintinit2;
if ($aandnotfalse) {
if ($n > 2) { io.printf("big\n"); } elseif ($n > 1) { io.printf("two\n"); }
}
# 5: say what you meandef name asstringinit"ada";
def m asmapofstringtointinit {"key": 1};
if ($n != 0) { io.printf("n is not zero\n"); }
if (len($name) > 0) { io.printf("name is set\n"); }
if (maps.has($m, "key")) { io.printf("key is there\n"); }
# 7: guard computed bounds before the loopdef s asstringinit"ab";
for (def start in [1, 3]) {
if ($start < len($s)) {
for (def i in$start..len($s)) { io.printf("start %d: %d\n", $start, $i); }
}
}
# 8: raw strings have no escapes and no slotsdef body asstringinit'{"a": 1}';
def keys aslistofstringinit ['{', '}'];
io.printf("%s, %d keys\n", $body, len($keys));
# bonus: test the key firstdef counts asmapofstringtointinit {"a": 1};
if (maps.has($counts, "b")) { io.printf("%d\n", $counts["b"]); }
io.printf("no b\n");