ETL
Turn a Scrabble score table keyed by score into one keyed by lower-case letter.
Adapted from Exercism’s problem specification (MIT). Listed under Collections & Algorithms.
// FILE: etl.nomi
//# Turns the legacy Scrabble score table, letters by score, into a score for
//# each letter.
/// Turns the legacy table, a score for each list of upper-case letters, into
/// a score for each lower-case letter.
pub fn transform(legacy: Map<Int, List<String>>) -> Map<String, Int>
legacy
|> Iter.flat_map(fn (score, letters) ->
Iter.map(letters, fn letter -> (String.lowercase(letter), score))
end)
|> Iter.to_map()
end
// FILE: etl_test.nomi
import etl
test "single letter"
assert etl.transform({1 => ["A"]}) == {"a" => 1}
end
test "single score with multiple letters"
legacy = {1 => ["A", "E", "I", "O", "U"]}
assert etl.transform(legacy) == {"a" => 1, "e" => 1, "i" => 1, "o" => 1, "u" => 1}
end
test "multiple scores with multiple letters"
legacy = {1 => ["A", "E"], 2 => ["D", "G"]}
assert etl.transform(legacy) == {"a" => 1, "d" => 2, "e" => 1, "g" => 2}
end
test "multiple scores with differing numbers of letters"
legacy = {
1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2 => ["D", "G"],
3 => ["B", "C", "M", "P"],
4 => ["F", "H", "V", "W", "Y"],
5 => ["K"],
8 => ["J", "X"],
10 => ["Q", "Z"],
}
assert etl.transform(legacy) == {
"a" => 1,
"b" => 3,
"c" => 3,
"d" => 2,
"e" => 1,
"f" => 4,
"g" => 2,
"h" => 4,
"i" => 1,
"j" => 8,
"k" => 5,
"l" => 1,
"m" => 3,
"n" => 1,
"o" => 1,
"p" => 3,
"q" => 10,
"r" => 1,
"s" => 1,
"t" => 1,
"u" => 1,
"v" => 4,
"w" => 4,
"x" => 8,
"y" => 4,
"z" => 10,
}
endok single letter
ok single score with multiple letters
ok multiple scores with multiple letters
ok multiple scores with differing numbers of letters
test result: ok. 4 passed, 0 failed
How it’s written
Section titled “How it’s written”- The input is
Map<Int, List<String>>and the outputMap<String, Int>, the canonical shapes with the score as anIntrather than the JSON’s string key. - One pipeline:
Iter.flat_mapover the map’s(score, letters)entries, thenIter.to_map. AMapiterates as key-value tuples, so the lambda destructures the entry in its parameter. - Letters stay
Strings, notCodepoints, since they are text in the table. - Four plain tests, since the cases differ mostly in the size of their maps and read better as separate literals.