Skip to content

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,
    }
end
  • The input is Map<Int, List<String>> and the output Map<String, Int>, the canonical shapes with the score as an Int rather than the JSON’s string key.
  • One pipeline: Iter.flat_map over the map’s (score, letters) entries, then Iter.to_map. A Map iterates as key-value tuples, so the lambda destructures the entry in its parameter.
  • Letters stay Strings, not Codepoints, 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.