From 351e5c35686bae43efd466c938aaf901a960acd6 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Tue, 16 Jan 2024 15:00:08 +0000 Subject: [PATCH] Update exercises --- .tool-versions | 1 - .../src/exercise_generator.gleam | 58 ++-- .../high-score-board/.meta/example.gleam | 16 +- .../src/high_score_board.gleam | 4 +- .../test/high_score_board_test.gleam | 22 +- .../accumulate/test/accumulate_test.gleam | 14 +- .../practice/alphametics/.meta/example.gleam | 36 +-- .../alphametics/src/alphametics.gleam | 2 +- .../alphametics/test/alphametics_test.gleam | 132 ++++---- .../practice/anagram/test/anagram_test.gleam | 7 +- .../atbash-cipher/.meta/example.gleam | 8 +- .../practice/bowling/test/bowling_test.gleam | 33 +- exercises/practice/change/.meta/example.gleam | 16 +- .../practice/change/test/change_test.gleam | 6 +- .../practice/clock/test/clock_test.gleam | 6 +- .../practice/connect/.meta/example.gleam | 42 +-- .../practice/custom-set/.meta/example.gleam | 26 +- exercises/practice/etl/.meta/example.gleam | 10 +- exercises/practice/etl/src/etl.gleam | 4 +- exercises/practice/etl/test/etl_test.gleam | 100 +++---- exercises/practice/forth/.meta/example.gleam | 34 +-- .../practice/grade-school/.meta/example.gleam | 12 +- exercises/practice/house/src/house.gleam | 5 +- .../practice/minesweeper/.meta/example.gleam | 26 +- .../nucleotide-count/.meta/example.gleam | 10 +- .../src/nucleotide_count.gleam | 4 +- .../test/nucleotide_count_test.gleam | 10 +- exercises/practice/pov/test/pov_test.gleam | 281 ++++++++---------- .../practice/rectangles/.meta/example.gleam | 24 +- .../rotational-cipher/.meta/example.gleam | 12 +- .../satellite/test/satellite_test.gleam | 39 ++- .../scrabble-score/.meta/example.gleam | 6 +- .../practice/series/test/series_test.gleam | 6 +- .../practice/tournament/.meta/example.gleam | 14 +- .../practice/word-count/.meta/example.gleam | 10 +- .../practice/word-count/src/word_count.gleam | 4 +- .../word-count/test/word_count_test.gleam | 30 +- exercises/practice/yacht/.meta/example.gleam | 6 +- 38 files changed, 531 insertions(+), 545 deletions(-) delete mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index 5a0198418..000000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -gleam 0.30.5 diff --git a/exercise_generator/src/exercise_generator.gleam b/exercise_generator/src/exercise_generator.gleam index 13e57f791..9f08e8bbb 100644 --- a/exercise_generator/src/exercise_generator.gleam +++ b/exercise_generator/src/exercise_generator.gleam @@ -1,5 +1,5 @@ import gleam/io -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/function import gleam/list import gleam/int @@ -28,7 +28,7 @@ type TestData { reimplements: Result(String, Nil), description: String, function: String, - input: Map(String, JsonData), + input: Dict(String, JsonData), expected: JsonData, ) } @@ -54,7 +54,7 @@ type JsonData { JsonFloat(Float) JsonString(String) JsonList(List(JsonData)) - JsonObject(Map(String, JsonData)) + JsonObject(Dict(String, JsonData)) } fn json_data_to_gleam_type(data: JsonData) -> String { @@ -83,7 +83,7 @@ fn json_data_to_gleam_value(data: JsonData) -> String { JsonObject(map) -> { let args = map - |> map.to_list + |> dict.to_list |> list.map(fn(arg) { clean_variable(arg.0) <> ": " <> json_data_to_gleam_value(arg.1) }) @@ -105,11 +105,11 @@ fn have_same_type(a: JsonData, b: JsonData) -> Bool { JsonObject(x), JsonObject(y) -> { let x: List(#(String, JsonData)) = x - |> map.to_list + |> dict.to_list |> list.sort(fn(a, b) { string.compare(a.0, b.0) }) let y: List(#(String, JsonData)) = y - |> map.to_list + |> dict.to_list |> list.sort(fn(a, b) { string.compare(a.0, b.0) }) let comparaison = list.strict_zip(x, y) @@ -181,7 +181,7 @@ fn write_solution_files( let content = functions - |> map.to_list + |> dict.to_list |> list.sort(by: fn(a, b) { int.compare({ a.1 }.order, { b.1 }.order) }) |> list.map(fn(item) { let #(name, Function(arguments, return_type, can_error, need_labels, _)) = @@ -215,7 +215,13 @@ fn write_solution_files( }) |> string.join(", ") - "pub fn " <> clean_variable(name) <> "(" <> args <> ") -> " <> return <> " {\n todo \n}" + "pub fn " + <> clean_variable(name) + <> "(" + <> args + <> ") -> " + <> return + <> " {\n todo \n}" }) |> string.join("\n") @@ -237,14 +243,14 @@ fn write_solution_files( let assert Ok(Nil) = file.write(content, example_path) } -fn functions_to_implement(test_cases: List(TestCase)) -> Map(String, Function) { - list.fold(over: test_cases, from: map.new(), with: check_test_case) +fn functions_to_implement(test_cases: List(TestCase)) -> Dict(String, Function) { + list.fold(over: test_cases, from: dict.new(), with: check_test_case) } fn check_test_case( - functions: Map(String, Function), + functions: Dict(String, Function), test_case: TestCase, -) -> Map(String, Function) { +) -> Dict(String, Function) { case test_case { TestGroup(cases: cases, ..) -> list.fold(over: cases, from: functions, with: check_test_case) @@ -255,13 +261,13 @@ fn check_test_case( .., )) -> { let can_error = case expected { - JsonObject(object) -> map.has_key(object, "error") + JsonObject(object) -> dict.has_key(object, "error") _ -> False } let args = input - |> map.to_list + |> dict.to_list |> list.map(fn(arg) { Argument(arg.0, arg.1) }) let need_labels = case args { @@ -272,7 +278,7 @@ fn check_test_case( _ -> True } - let current_function = case map.get(functions, function) { + let current_function = case dict.get(functions, function) { Ok(func) -> { let func = Function(..func, need_labels: func.need_labels || need_labels) @@ -284,10 +290,10 @@ fn check_test_case( } Error(Nil) -> - Function(args, expected, can_error, need_labels, map.size(functions)) + Function(args, expected, can_error, need_labels, dict.size(functions)) } - map.insert(functions, function, current_function) + dict.insert(functions, function, current_function) } } } @@ -334,7 +340,7 @@ fn print_comments(comments: List(String)) -> String { fn print_tests( slug: String, prefix: String, - functions: Map(String, Function), + functions: Dict(String, Function), tests: List(TestCase), ) -> String { tests @@ -345,7 +351,7 @@ fn print_tests( fn print_test( slug: String, prefix: String, - functions: Map(String, Function), + functions: Dict(String, Function), test: TestCase, ) -> String { case test { @@ -368,7 +374,9 @@ fn print_test( [ "This test reimplements the test with uuid " <> uuid, "Please identify that test and remove it. Link:", - "https://github.com/exercism/problem-specifications/blob/main/exercises/" <> slug <> "/canonical-data.json", + "https://github.com/exercism/problem-specifications/blob/main/exercises/" + <> slug + <> "/canonical-data.json", ] |> list.append(comments) |> print_comments() @@ -376,10 +384,10 @@ fn print_test( } let test_name = flatten_description(prefix <> description) let assert Ok(Function(need_labels: need_labels, ..)) = - map.get(functions, function) + dict.get(functions, function) let input = input - |> map.to_list + |> dict.to_list |> list.map(fn(item) { case need_labels { True -> @@ -424,14 +432,14 @@ fn flatten_description(description: String) -> String { fn get_expected_value( function: String, - functions: Map(String, Function), + functions: Dict(String, Function), expected: JsonData, ) { - case map.get(functions, function) { + case dict.get(functions, function) { Ok(Function(can_error: True, ..)) -> case expected { JsonObject(map) -> - case map.get(map, "error") { + case dict.get(map, "error") { Ok(value) -> "Error(" <> json_data_to_gleam_value(value) <> ")" Error(Nil) -> "Ok(" <> json_data_to_gleam_value(expected) <> ")" } diff --git a/exercises/concept/high-score-board/.meta/example.gleam b/exercises/concept/high-score-board/.meta/example.gleam index 67e95f42c..b9490a34d 100644 --- a/exercises/concept/high-score-board/.meta/example.gleam +++ b/exercises/concept/high-score-board/.meta/example.gleam @@ -1,10 +1,10 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} pub type ScoreBoard = - Map(String, Int) + Dict(String, Int) pub fn create_score_board() -> ScoreBoard { - map.from_list([#("The Best Ever", 1_000_000)]) + dict.from_list([#("The Best Ever", 1_000_000)]) } pub fn add_player( @@ -12,11 +12,11 @@ pub fn add_player( player: String, score: Int, ) -> ScoreBoard { - map.insert(score_board, player, score) + dict.insert(score_board, player, score) } pub fn remove_player(score_board: ScoreBoard, player: String) -> ScoreBoard { - map.delete(score_board, player) + dict.delete(score_board, player) } pub fn update_score( @@ -24,12 +24,12 @@ pub fn update_score( player: String, points: Int, ) -> ScoreBoard { - case map.get(score_board, player) { - Ok(score) -> map.insert(score_board, player, score + points) + case dict.get(score_board, player) { + Ok(score) -> dict.insert(score_board, player, score + points) Error(Nil) -> score_board } } pub fn apply_monday_bonus(score_board: ScoreBoard) -> ScoreBoard { - map.map_values(score_board, fn(_, score) { score + 100 }) + dict.map_values(score_board, fn(_, score) { score + 100 }) } diff --git a/exercises/concept/high-score-board/src/high_score_board.gleam b/exercises/concept/high-score-board/src/high_score_board.gleam index 88a74377f..9f1aa1ef2 100644 --- a/exercises/concept/high-score-board/src/high_score_board.gleam +++ b/exercises/concept/high-score-board/src/high_score_board.gleam @@ -1,7 +1,7 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} pub type ScoreBoard = - Map(String, Int) + Dict(String, Int) pub fn create_score_board() -> ScoreBoard { todo diff --git a/exercises/concept/high-score-board/test/high_score_board_test.gleam b/exercises/concept/high-score-board/test/high_score_board_test.gleam index f1a0319e6..e695e789e 100644 --- a/exercises/concept/high-score-board/test/high_score_board_test.gleam +++ b/exercises/concept/high-score-board/test/high_score_board_test.gleam @@ -1,7 +1,7 @@ import high_score_board import exercism/test_runner import exercism/should -import gleam/map +import gleam/dict pub fn main() { test_runner.main() @@ -9,14 +9,14 @@ pub fn main() { pub fn create_score_board_test() { high_score_board.create_score_board() - |> should.equal(map.from_list([#("The Best Ever", 1_000_000)])) + |> should.equal(dict.from_list([#("The Best Ever", 1_000_000)])) } pub fn add_player_board_test() { [#("Amil Pastorius", 99_373), #("Min-seo Shin", 0)] - |> map.from_list + |> dict.from_list |> high_score_board.add_player("Jessie Johnson", 1337) - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("Amil Pastorius", 99_373), #("Min-seo Shin", 0), #("Jessie Johnson", 1337), @@ -25,9 +25,9 @@ pub fn add_player_board_test() { pub fn remove_player_test() { [#("Amil Pastorius", 99_373), #("Min-seo Shin", 0), #("Jesse Johnson", 1337)] - |> map.from_list + |> dict.from_list |> high_score_board.remove_player("Jesse Johnson") - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("Amil Pastorius", 99_373), #("Min-seo Shin", 0), ])) @@ -35,7 +35,7 @@ pub fn remove_player_test() { pub fn remove_player_unknown_test() { let board = - map.from_list([ + dict.from_list([ #("Amil Pastorius", 99_373), #("Min-seo Shin", 0), #("Jesse Johnson", 1337), @@ -48,11 +48,11 @@ pub fn remove_player_unknown_test() { pub fn update_score_test() { [#("Amil Pastorius", 99_373), #("Min-seo Shin", 0), #("Jesse Johnson", 1337)] - |> map.from_list + |> dict.from_list |> high_score_board.update_score("Min-seo Shin", 1999) |> high_score_board.update_score("Jesse Johnson", 1337) |> high_score_board.update_score("Unknown player", 1) - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("Amil Pastorius", 99_373), #("Min-seo Shin", 1999), #("Jesse Johnson", 2674), @@ -61,9 +61,9 @@ pub fn update_score_test() { pub fn apply_monday_bonus_test() { [#("Amil Pastorius", 345), #("Min-seo Shin", 19), #("Jesse Johnson", 122)] - |> map.from_list + |> dict.from_list |> high_score_board.apply_monday_bonus - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("Amil Pastorius", 445), #("Min-seo Shin", 119), #("Jesse Johnson", 222), diff --git a/exercises/practice/accumulate/test/accumulate_test.gleam b/exercises/practice/accumulate/test/accumulate_test.gleam index dadf73d33..944eccbcc 100644 --- a/exercises/practice/accumulate/test/accumulate_test.gleam +++ b/exercises/practice/accumulate/test/accumulate_test.gleam @@ -28,14 +28,10 @@ pub fn accumulate_reversed_strings_test() { } pub fn accumulate_recursively_test() { - accumulate.accumulate( - ["a", "b", "c"], - fn(x) -> List(String) { - accumulate.accumulate( - ["1", "2", "3"], - fn(y) -> String { string.append(x, y) }, - ) - }, - ) + accumulate.accumulate(["a", "b", "c"], fn(x) -> List(String) { + accumulate.accumulate(["1", "2", "3"], fn(y) -> String { + string.append(x, y) + }) + }) |> should.equal([["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]) } diff --git a/exercises/practice/alphametics/.meta/example.gleam b/exercises/practice/alphametics/.meta/example.gleam index c0ea1d166..dc7393a59 100644 --- a/exercises/practice/alphametics/.meta/example.gleam +++ b/exercises/practice/alphametics/.meta/example.gleam @@ -1,28 +1,28 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/string import gleam/list import gleam/int import gleam/option import gleam/set.{type Set} -pub fn solve(puzzle: String) -> Result(Map(String, Int), Nil) { +pub fn solve(puzzle: String) -> Result(Dict(String, Int), Nil) { let #(coefficients, leading_digits) = parse_puzzle(puzzle) - let letters = map.keys(coefficients) + let letters = dict.keys(coefficients) let values = list.range(0, 9) |> set.from_list - let guess = map.new() + let guess = dict.new() solve_puzzle(letters, values, guess, coefficients, leading_digits) } -fn parse_puzzle(puzzle: String) -> #(Map(String, Int), Set(String)) { +fn parse_puzzle(puzzle: String) -> #(Dict(String, Int), Set(String)) { let assert [left, right] = string.split(puzzle, " == ") let terms = string.split(left, " + ") let coefficients = terms |> list.fold( - from: map.new(), + from: dict.new(), with: fn(coefficients, term) { add_term(coefficients, term, int.add) }, ) |> add_term(right, int.subtract) @@ -36,17 +36,17 @@ fn parse_puzzle(puzzle: String) -> #(Map(String, Int), Set(String)) { } fn add_term( - coefficients: Map(String, Int), + coefficients: Dict(String, Int), term: String, operator: fn(Int, Int) -> Int, -) -> Map(String, Int) { +) -> Dict(String, Int) { term |> string.to_graphemes |> list.reverse |> list.index_fold( from: coefficients, with: fn(coefficients, letter, index) { - map.update( + dict.update( coefficients, letter, fn(maybe_coeff) { @@ -67,10 +67,10 @@ fn pow(base: Int, exp: Int) -> Int { fn solve_puzzle( letters_to_assign: List(String), possible_values: Set(Int), - current_guess: Map(String, Int), - coefficients: Map(String, Int), + current_guess: Dict(String, Int), + coefficients: Dict(String, Int), leading_digits: Set(String), -) -> Result(Map(String, Int), Nil) { +) -> Result(Dict(String, Int), Nil) { case letters_to_assign { [] -> is_solution(current_guess, coefficients) [letter, ..rest] -> @@ -81,7 +81,7 @@ fn solve_puzzle( from: Error(Nil), with: fn(_, value) { let possible_values = set.delete(possible_values, value) - let current_guess = map.insert(current_guess, letter, value) + let current_guess = dict.insert(current_guess, letter, value) case solve_puzzle( rest, @@ -111,15 +111,15 @@ fn maybe_remove_zero( } fn is_solution( - current_guess: Map(String, Int), - coefficients: Map(String, Int), -) -> Result(Map(String, Int), Nil) { + current_guess: Dict(String, Int), + coefficients: Dict(String, Int), +) -> Result(Dict(String, Int), Nil) { let sum = - map.fold( + dict.fold( over: current_guess, from: 0, with: fn(sum, letter, value) { - let assert Ok(coeff) = map.get(coefficients, letter) + let assert Ok(coeff) = dict.get(coefficients, letter) sum + coeff * value }, ) diff --git a/exercises/practice/alphametics/src/alphametics.gleam b/exercises/practice/alphametics/src/alphametics.gleam index 4eff602a8..37515a1c7 100644 --- a/exercises/practice/alphametics/src/alphametics.gleam +++ b/exercises/practice/alphametics/src/alphametics.gleam @@ -1,3 +1,3 @@ -pub fn solve(puzzle: String) -> Result(Map(String, Int), Nil) { +pub fn solve(puzzle: String) -> Result(Dict(String, Int), Nil) { todo } diff --git a/exercises/practice/alphametics/test/alphametics_test.gleam b/exercises/practice/alphametics/test/alphametics_test.gleam index 381337b65..537c60268 100644 --- a/exercises/practice/alphametics/test/alphametics_test.gleam +++ b/exercises/practice/alphametics/test/alphametics_test.gleam @@ -1,7 +1,7 @@ import exercism/test_runner import exercism/should import alphametics -import gleam/map +import gleam/dict pub fn main() { test_runner.main() @@ -9,7 +9,7 @@ pub fn main() { pub fn puzzle_with_three_letters_test() { alphametics.solve("I + BB == ILL") - |> should.equal(Ok(map.from_list([#("B", 9), #("I", 1), #("L", 0)]))) + |> should.equal(Ok(dict.from_list([#("B", 9), #("I", 1), #("L", 0)]))) } pub fn solution_must_have_unique_value_for_each_letter_test() { @@ -24,83 +24,105 @@ pub fn leading_zero_solution_is_invalid_test() { pub fn puzzle_with_two_digits_final_carry_test() { alphametics.solve("A + A + A + A + A + A + A + A + A + A + A + B == BCC") - |> should.equal(Ok(map.from_list([#("A", 9), #("B", 1), #("C", 0)]))) + |> should.equal(Ok(dict.from_list([#("A", 9), #("B", 1), #("C", 0)]))) } pub fn puzzle_with_four_letters_test() { alphametics.solve("AS + A == MOM") - |> should.equal(Ok(map.from_list([#("A", 9), #("M", 1), #("O", 0), #("S", 2)]))) + |> should.equal( + Ok(dict.from_list([#("A", 9), #("M", 1), #("O", 0), #("S", 2)])), + ) } pub fn puzzle_with_six_letters_test() { alphametics.solve("NO + NO + TOO == LATE") - |> should.equal(Ok(map.from_list([ - #("A", 0), - #("E", 2), - #("L", 1), - #("N", 7), - #("O", 4), - #("T", 9), - ]))) + |> should.equal( + Ok( + dict.from_list([ + #("A", 0), + #("E", 2), + #("L", 1), + #("N", 7), + #("O", 4), + #("T", 9), + ]), + ), + ) } pub fn puzzle_with_seven_letters_test() { alphametics.solve("HE + SEES + THE == LIGHT") - |> should.equal(Ok(map.from_list([ - #("E", 4), - #("G", 2), - #("H", 5), - #("I", 0), - #("L", 1), - #("S", 9), - #("T", 7), - ]))) + |> should.equal( + Ok( + dict.from_list([ + #("E", 4), + #("G", 2), + #("H", 5), + #("I", 0), + #("L", 1), + #("S", 9), + #("T", 7), + ]), + ), + ) } pub fn puzzle_with_eight_letters_test() { alphametics.solve("SEND + MORE == MONEY") - |> should.equal(Ok(map.from_list([ - #("D", 7), - #("E", 5), - #("M", 1), - #("N", 6), - #("O", 0), - #("R", 8), - #("S", 9), - #("Y", 2), - ]))) + |> should.equal( + Ok( + dict.from_list([ + #("D", 7), + #("E", 5), + #("M", 1), + #("N", 6), + #("O", 0), + #("R", 8), + #("S", 9), + #("Y", 2), + ]), + ), + ) } pub fn puzzle_with_ten_letters_test() { alphametics.solve("AND + A + STRONG + OFFENSE + AS + A + GOOD == DEFENSE") - |> should.equal(Ok(map.from_list([ - #("A", 5), - #("D", 3), - #("E", 4), - #("F", 7), - #("G", 8), - #("N", 0), - #("O", 2), - #("R", 1), - #("S", 6), - #("T", 9), - ]))) + |> should.equal( + Ok( + dict.from_list([ + #("A", 5), + #("D", 3), + #("E", 4), + #("F", 7), + #("G", 8), + #("N", 0), + #("O", 2), + #("R", 1), + #("S", 6), + #("T", 9), + ]), + ), + ) } pub fn puzzle_with_ten_letters_and_199_addends_test() { alphametics.solve( "THIS + A + FIRE + THEREFORE + FOR + ALL + HISTORIES + I + TELL + A + TALE + THAT + FALSIFIES + ITS + TITLE + TIS + A + LIE + THE + TALE + OF + THE + LAST + FIRE + HORSES + LATE + AFTER + THE + FIRST + FATHERS + FORESEE + THE + HORRORS + THE + LAST + FREE + TROLL + TERRIFIES + THE + HORSES + OF + FIRE + THE + TROLL + RESTS + AT + THE + HOLE + OF + LOSSES + IT + IS + THERE + THAT + SHE + STORES + ROLES + OF + LEATHERS + AFTER + SHE + SATISFIES + HER + HATE + OFF + THOSE + FEARS + A + TASTE + RISES + AS + SHE + HEARS + THE + LEAST + FAR + HORSE + THOSE + FAST + HORSES + THAT + FIRST + HEAR + THE + TROLL + FLEE + OFF + TO + THE + FOREST + THE + HORSES + THAT + ALERTS + RAISE + THE + STARES + OF + THE + OTHERS + AS + THE + TROLL + ASSAILS + AT + THE + TOTAL + SHIFT + HER + TEETH + TEAR + HOOF + OFF + TORSO + AS + THE + LAST + HORSE + FORFEITS + ITS + LIFE + THE + FIRST + FATHERS + HEAR + OF + THE + HORRORS + THEIR + FEARS + THAT + THE + FIRES + FOR + THEIR + FEASTS + ARREST + AS + THE + FIRST + FATHERS + RESETTLE + THE + LAST + OF + THE + FIRE + HORSES + THE + LAST + TROLL + HARASSES + THE + FOREST + HEART + FREE + AT + LAST + OF + THE + LAST + TROLL + ALL + OFFER + THEIR + FIRE + HEAT + TO + THE + ASSISTERS + FAR + OFF + THE + TROLL + FASTS + ITS + LIFE + SHORTER + AS + STARS + RISE + THE + HORSES + REST + SAFE + AFTER + ALL + SHARE + HOT + FISH + AS + THEIR + AFFILIATES + TAILOR + A + ROOFS + FOR + THEIR + SAFE == FORTRESSES", ) - |> should.equal(Ok(map.from_list([ - #("A", 1), - #("E", 0), - #("F", 5), - #("H", 8), - #("I", 7), - #("L", 2), - #("O", 6), - #("R", 3), - #("S", 4), - #("T", 9), - ]))) + |> should.equal( + Ok( + dict.from_list([ + #("A", 1), + #("E", 0), + #("F", 5), + #("H", 8), + #("I", 7), + #("L", 2), + #("O", 6), + #("R", 3), + #("S", 4), + #("T", 9), + ]), + ), + ) } diff --git a/exercises/practice/anagram/test/anagram_test.gleam b/exercises/practice/anagram/test/anagram_test.gleam index 131f6cec8..03709b9bb 100644 --- a/exercises/practice/anagram/test/anagram_test.gleam +++ b/exercises/practice/anagram/test/anagram_test.gleam @@ -27,10 +27,9 @@ pub fn detects_anagram_test() { } pub fn detects_three_anagrams_test() { - find_anagrams( - "allergy", - ["gallery", "ballerina", "regally", "clergy", "largely", "leading"], - ) + find_anagrams("allergy", [ + "gallery", "ballerina", "regally", "clergy", "largely", "leading", + ]) |> should.equal(["gallery", "regally", "largely"]) } diff --git a/exercises/practice/atbash-cipher/.meta/example.gleam b/exercises/practice/atbash-cipher/.meta/example.gleam index 2c177ce3d..065a7e4e4 100644 --- a/exercises/practice/atbash-cipher/.meta/example.gleam +++ b/exercises/practice/atbash-cipher/.meta/example.gleam @@ -1,7 +1,7 @@ import gleam/result import gleam/string import gleam/list -import gleam/map.{type Map} +import gleam/dict.{type Dict} pub fn encode(phrase: String) -> String { let encode_map = encoding_map() @@ -9,7 +9,7 @@ pub fn encode(phrase: String) -> String { phrase |> string.lowercase() |> string.to_graphemes() - |> list.map(fn(char) { map.get(encode_map, char) }) + |> list.map(fn(char) { dict.get(encode_map, char) }) |> result.values() |> list.sized_chunk(into: 5) |> list.map(string.concat) @@ -22,11 +22,11 @@ pub fn decode(phrase: String) -> String { |> string.replace(each: " ", with: "") } -fn encoding_map() -> Map(String, String) { +fn encoding_map() -> Dict(String, String) { let letters = string.to_graphemes("abcdefghijklmnopqrstuvwxyz") let digits = string.to_graphemes("0123456789") list.zip(letters, list.reverse(letters)) |> list.append(list.zip(digits, digits)) - |> map.from_list() + |> dict.from_list() } diff --git a/exercises/practice/bowling/test/bowling_test.gleam b/exercises/practice/bowling/test/bowling_test.gleam index 5e141009f..a1fc0df8b 100644 --- a/exercises/practice/bowling/test/bowling_test.gleam +++ b/exercises/practice/bowling/test/bowling_test.gleam @@ -217,39 +217,30 @@ pub fn last_two_strikes_followed_by_only_last_bonus_non_strike_points_test() { fn roll_and_check_score(rolls: List(Int), correct_score: Int) { rolls - |> list.fold( - Game([]), - fn(game, pins) { - let assert Ok(new_game) = roll(game, pins) - new_game - }, - ) + |> list.fold(Game([]), fn(game, pins) { + let assert Ok(new_game) = roll(game, pins) + new_game + }) |> score() |> should.equal(Ok(correct_score)) } fn roll_and_last_roll_be_error(rolls: List(Int), last_roll: Int, error: Error) { rolls - |> list.fold( - Game([]), - fn(game, pins) { - let assert Ok(new_game) = roll(game, pins) - new_game - }, - ) + |> list.fold(Game([]), fn(game, pins) { + let assert Ok(new_game) = roll(game, pins) + new_game + }) |> roll(last_roll) |> should.equal(Error(error)) } fn roll_and_score_be_error(rolls: List(Int)) { rolls - |> list.fold( - Game([]), - fn(game, pins) { - let assert Ok(new_game) = roll(game, pins) - new_game - }, - ) + |> list.fold(Game([]), fn(game, pins) { + let assert Ok(new_game) = roll(game, pins) + new_game + }) |> score() |> should.equal(Error(GameNotComplete)) } diff --git a/exercises/practice/change/.meta/example.gleam b/exercises/practice/change/.meta/example.gleam index 7c760beea..ddac167f5 100644 --- a/exercises/practice/change/.meta/example.gleam +++ b/exercises/practice/change/.meta/example.gleam @@ -1,7 +1,7 @@ import gleam/result import gleam/list import gleam/int -import gleam/map.{type Map} +import gleam/dict.{type Dict} pub type Error { ImpossibleTarget @@ -13,24 +13,24 @@ pub fn find_fewest_coins( ) -> Result(List(Int), Error) { list.range(1, target) |> list.fold( - from: map.from_list([#(0, [])]), + from: dict.from_list([#(0, [])]), with: fn(fewest_coins_map, coin) { update_fewest_coins_map(coins, fewest_coins_map, coin) }, ) - |> map.get(target) + |> dict.get(target) |> result.replace_error(ImpossibleTarget) } fn calculate_fewest_coins( coins: List(Int), - fewest_coins_map: Map(Int, List(Int)), + fewest_coins_map: Dict(Int, List(Int)), target: Int, ) -> Result(List(Int), Nil) { coins |> list.filter(fn(coin) { coin <= target }) |> list.filter_map(fn(coin) { - map.get(fewest_coins_map, target - coin) + dict.get(fewest_coins_map, target - coin) |> result.map(fn(change) { [coin, ..change] }) }) |> list.sort(fn(a, b) { int.compare(list.length(a), list.length(b)) }) @@ -39,10 +39,10 @@ fn calculate_fewest_coins( fn update_fewest_coins_map( coins: List(Int), - fewest_coins_map: Map(Int, List(Int)), + fewest_coins_map: Dict(Int, List(Int)), amount: Int, -) -> Map(Int, List(Int)) { +) -> Dict(Int, List(Int)) { calculate_fewest_coins(coins, fewest_coins_map, amount) - |> result.map(map.insert(fewest_coins_map, amount, _)) + |> result.map(dict.insert(fewest_coins_map, amount, _)) |> result.unwrap(fewest_coins_map) } diff --git a/exercises/practice/change/test/change_test.gleam b/exercises/practice/change/test/change_test.gleam index 49927609d..687df2c54 100644 --- a/exercises/practice/change/test/change_test.gleam +++ b/exercises/practice/change/test/change_test.gleam @@ -33,9 +33,9 @@ pub fn change_with_lower_elbonia_coins_test() { pub fn large_target_values_test() { change.find_fewest_coins([1, 2, 5, 10, 20, 50, 100], 999) - |> should.equal(Ok([ - 2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100, - ])) + |> should.equal( + Ok([2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100]), + ) } pub fn possible_change_without_unit_coins_available_test() { diff --git a/exercises/practice/clock/test/clock_test.gleam b/exercises/practice/clock/test/clock_test.gleam index 02bbb2cce..f0ed9640f 100644 --- a/exercises/practice/clock/test/clock_test.gleam +++ b/exercises/practice/clock/test/clock_test.gleam @@ -310,10 +310,8 @@ pub fn compare_two_clocks_for_equality_clocks_with_negative_hours_and_minutes_te pub fn compare_two_clocks_for_equality_clocks_with_negative_hours_and_minutes_that_wrap_test() { let assert True = - clock.create(hour: 18, minute: 7) == clock.create( - hour: -54, - minute: -11_513, - ) + clock.create(hour: 18, minute: 7) + == clock.create(hour: -54, minute: -11_513) } pub fn compare_two_clocks_for_equality_full_clock_and_zeroed_clock_test() { diff --git a/exercises/practice/connect/.meta/example.gleam b/exercises/practice/connect/.meta/example.gleam index e5ac7cf09..e4ea59b47 100644 --- a/exercises/practice/connect/.meta/example.gleam +++ b/exercises/practice/connect/.meta/example.gleam @@ -1,5 +1,5 @@ import gleam/list -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/string import gleam/result @@ -35,7 +35,7 @@ pub fn winner(board: String) -> Result(Player, Nil) { } } -fn parse(rows: List(String)) -> Map(Position, Player) { +fn parse(rows: List(String)) -> Dict(Position, Player) { rows |> list.index_map(fn(row, line) { line @@ -50,25 +50,25 @@ fn parse(rows: List(String)) -> Map(Position, Player) { |> list.flatten }) |> list.flatten - |> map.from_list + |> dict.from_list } -fn o_wins(board: Map(Position, Player), size: Size) -> Result(Player, Nil) { +fn o_wins(board: Dict(Position, Player), size: Size) -> Result(Player, Nil) { let start_positions = - map.filter(board, fn(pos, player) { player == O && pos.row == 0 }) + dict.filter(board, fn(pos, player) { player == O && pos.row == 0 }) let end_positions = - map.filter( + dict.filter( board, fn(pos, player) { player == O && pos.row == size.rows - 1 }, ) - case map.size(start_positions) > 0 && map.size(end_positions) > 0 { + case dict.size(start_positions) > 0 && dict.size(end_positions) > 0 { False -> Error(Nil) True -> traverse_board( board, - map.keys(start_positions), + dict.keys(start_positions), start_positions, end_positions, O, @@ -76,24 +76,24 @@ fn o_wins(board: Map(Position, Player), size: Size) -> Result(Player, Nil) { } } -fn x_wins(board: Map(Position, Player), size: Size) -> Result(Player, Nil) { +fn x_wins(board: Dict(Position, Player), size: Size) -> Result(Player, Nil) { let start_positions = - map.filter(board, fn(pos, player) { player == X && pos.row == pos.column }) + dict.filter(board, fn(pos, player) { player == X && pos.row == pos.column }) let end_positions = - map.filter( + dict.filter( board, fn(pos, player) { player == X && pos.column - pos.row == 2 * size.columns - 2 }, ) - case map.size(start_positions) > 0 && map.size(end_positions) > 0 { + case dict.size(start_positions) > 0 && dict.size(end_positions) > 0 { False -> Error(Nil) True -> traverse_board( board, - map.keys(start_positions), + dict.keys(start_positions), start_positions, end_positions, X, @@ -102,28 +102,28 @@ fn x_wins(board: Map(Position, Player), size: Size) -> Result(Player, Nil) { } fn traverse_board( - board: Map(Position, Player), + board: Dict(Position, Player), current: List(Position), - explored: Map(Position, Player), - target: Map(Position, Player), + explored: Dict(Position, Player), + target: Dict(Position, Player), player: Player, ) -> Result(Player, Nil) { case current { [] -> Error(Nil) [position, ..rest] -> - case map.has_key(target, position) { + case dict.has_key(target, position) { True -> Ok(player) False -> { let next_positions = board |> get_next_positions(position, player) - |> list.filter(fn(pos) { !map.has_key(explored, pos) }) + |> list.filter(fn(pos) { !dict.has_key(explored, pos) }) let explored = list.fold( over: next_positions, from: explored, - with: fn(explored, pos) { map.insert(explored, pos, player) }, + with: fn(explored, pos) { dict.insert(explored, pos, player) }, ) traverse_board( board, @@ -138,7 +138,7 @@ fn traverse_board( } fn get_next_positions( - board: Map(Position, Player), + board: Dict(Position, Player), position: Position, player: Player, ) -> List(Position) { @@ -152,5 +152,5 @@ fn get_next_positions( Position(row - 1, col - 1), ] - list.filter(neighbors, fn(pos) { map.get(board, pos) == Ok(player) }) + list.filter(neighbors, fn(pos) { dict.get(board, pos) == Ok(player) }) } diff --git a/exercises/practice/custom-set/.meta/example.gleam b/exercises/practice/custom-set/.meta/example.gleam index 86c7f06db..30f2a53b0 100644 --- a/exercises/practice/custom-set/.meta/example.gleam +++ b/exercises/practice/custom-set/.meta/example.gleam @@ -1,38 +1,38 @@ import gleam/bool import gleam/list -import gleam/map.{type Map} +import gleam/dict.{type Dict} pub opaque type Set(t) { - Set(map: Map(t, Bool)) + Set(map: Dict(t, Bool)) } pub fn new(members: List(t)) -> Set(t) { let map = members |> list.map(fn(member) { #(member, True) }) - |> map.from_list() + |> dict.from_list() Set(map) } pub fn is_empty(set: Set(t)) -> Bool { - map.size(set.map) == 0 + dict.size(set.map) == 0 } pub fn contains(in set: Set(t), this member: t) -> Bool { - map.has_key(set.map, member) + dict.has_key(set.map, member) } pub fn is_subset(first: Set(t), of second: Set(t)) -> Bool { first.map - |> map.keys() - |> list.all(fn(member) { map.has_key(second.map, member) }) + |> dict.keys() + |> list.all(fn(member) { dict.has_key(second.map, member) }) } pub fn disjoint(first: Set(t), second: Set(t)) -> Bool { first.map - |> map.keys() - |> list.any(fn(member) { map.has_key(second.map, member) }) + |> dict.keys() + |> list.any(fn(member) { dict.has_key(second.map, member) }) |> bool.negate() } @@ -41,17 +41,17 @@ pub fn is_equal(first: Set(t), to second: Set(t)) -> Bool { } pub fn add(to set: Set(t), this member: t) -> Set(t) { - Set(map: map.insert(set.map, member, True)) + Set(map: dict.insert(set.map, member, True)) } pub fn intersection(of first: Set(t), and second: Set(t)) -> Set(t) { - Set(map: map.take(from: first.map, keeping: map.keys(second.map))) + Set(map: dict.take(from: first.map, keeping: dict.keys(second.map))) } pub fn difference(between first: Set(t), and second: Set(t)) -> Set(t) { - Set(map: map.drop(from: first.map, drop: map.keys(second.map))) + Set(map: dict.drop(from: first.map, drop: dict.keys(second.map))) } pub fn union(of first: Set(t), and second: Set(t)) -> Set(t) { - Set(map: map.merge(into: first.map, from: second.map)) + Set(map: dict.merge(into: first.map, from: second.map)) } diff --git a/exercises/practice/etl/.meta/example.gleam b/exercises/practice/etl/.meta/example.gleam index fd32c6e75..564af922b 100644 --- a/exercises/practice/etl/.meta/example.gleam +++ b/exercises/practice/etl/.meta/example.gleam @@ -1,17 +1,17 @@ import gleam/string -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/list -pub fn transform(legacy: Map(Int, List(String))) -> Map(String, Int) { - map.fold( +pub fn transform(legacy: Dict(Int, List(String))) -> Dict(String, Int) { + dict.fold( over: legacy, - from: map.new(), + from: dict.new(), with: fn(transformed, score, letters) { list.fold( over: letters, from: transformed, with: fn(transformed, letter) { - map.insert(transformed, string.lowercase(letter), score) + dict.insert(transformed, string.lowercase(letter), score) }, ) }, diff --git a/exercises/practice/etl/src/etl.gleam b/exercises/practice/etl/src/etl.gleam index d56723582..6187f6ae3 100644 --- a/exercises/practice/etl/src/etl.gleam +++ b/exercises/practice/etl/src/etl.gleam @@ -1,5 +1,5 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} -pub fn transform(legacy: Map(Int, List(String))) -> Map(String, Int) { +pub fn transform(legacy: Dict(Int, List(String))) -> Dict(String, Int) { todo } diff --git a/exercises/practice/etl/test/etl_test.gleam b/exercises/practice/etl/test/etl_test.gleam index d7dd17c4d..c09897212 100644 --- a/exercises/practice/etl/test/etl_test.gleam +++ b/exercises/practice/etl/test/etl_test.gleam @@ -1,4 +1,4 @@ -import gleam/map +import gleam/dict import exercism/test_runner import exercism/should import etl @@ -8,62 +8,62 @@ pub fn main() { } pub fn single_letter_test() { - etl.transform(map.from_list([#(1, ["A"])])) - |> should.equal(map.from_list([#("a", 1)])) + etl.transform(dict.from_list([#(1, ["A"])])) + |> should.equal(dict.from_list([#("a", 1)])) } pub fn single_score_with_multiple_letters_test() { - etl.transform(map.from_list([#(1, ["A", "E", "I", "O", "U"])])) - |> should.equal(map.from_list([ - #("a", 1), - #("e", 1), - #("i", 1), - #("o", 1), - #("u", 1), - ])) + etl.transform(dict.from_list([#(1, ["A", "E", "I", "O", "U"])])) + |> should.equal( + dict.from_list([#("a", 1), #("e", 1), #("i", 1), #("o", 1), #("u", 1)]), + ) } pub fn multiple_scores_with_multiple_letters_test() { - etl.transform(map.from_list([#(1, ["A", "E"]), #(2, ["D", "G"])])) - |> should.equal(map.from_list([#("a", 1), #("d", 2), #("e", 1), #("g", 2)])) + etl.transform(dict.from_list([#(1, ["A", "E"]), #(2, ["D", "G"])])) + |> should.equal(dict.from_list([#("a", 1), #("d", 2), #("e", 1), #("g", 2)])) } pub fn multiple_scores_with_differing_numbers_of_letters_test() { - etl.transform(map.from_list([ - #(1, ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"]), - #(10, ["Q", "Z"]), - #(2, ["D", "G"]), - #(3, ["B", "C", "M", "P"]), - #(4, ["F", "H", "V", "W", "Y"]), - #(5, ["K"]), - #(8, ["J", "X"]), - ])) - |> should.equal(map.from_list([ - #("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), - ])) + etl.transform( + dict.from_list([ + #(1, ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"]), + #(10, ["Q", "Z"]), + #(2, ["D", "G"]), + #(3, ["B", "C", "M", "P"]), + #(4, ["F", "H", "V", "W", "Y"]), + #(5, ["K"]), + #(8, ["J", "X"]), + ]), + ) + |> should.equal( + dict.from_list([ + #("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), + ]), + ) } diff --git a/exercises/practice/forth/.meta/example.gleam b/exercises/practice/forth/.meta/example.gleam index 15c14d667..440cd0cf6 100644 --- a/exercises/practice/forth/.meta/example.gleam +++ b/exercises/practice/forth/.meta/example.gleam @@ -1,6 +1,6 @@ import gleam/result import gleam/int -import gleam/map +import gleam/dict import gleam/order import gleam/list import gleam/string @@ -18,19 +18,19 @@ pub type ForthTok { } pub type Forth { - Forth(stack: List(Int), env: map.Map(String, ForthWordDef)) + Forth(stack: List(Int), env: dict.Dict(String, ForthWordDef)) } -pub fn stdlib() -> map.Map(String, ForthWordDef) { - map.new() - |> map.insert("+", BuiltIn("+")) - |> map.insert("-", BuiltIn("-")) - |> map.insert("/", BuiltIn("/")) - |> map.insert("*", BuiltIn("*")) - |> map.insert("DUP", BuiltIn("DUP")) - |> map.insert("DROP", BuiltIn("DROP")) - |> map.insert("SWAP", BuiltIn("SWAP")) - |> map.insert("OVER", BuiltIn("OVER")) +pub fn stdlib() -> dict.Dict(String, ForthWordDef) { + dict.new() + |> dict.insert("+", BuiltIn("+")) + |> dict.insert("-", BuiltIn("-")) + |> dict.insert("/", BuiltIn("/")) + |> dict.insert("*", BuiltIn("*")) + |> dict.insert("DUP", BuiltIn("DUP")) + |> dict.insert("DROP", BuiltIn("DROP")) + |> dict.insert("SWAP", BuiltIn("SWAP")) + |> dict.insert("OVER", BuiltIn("OVER")) } pub type ForthError { @@ -174,7 +174,7 @@ fn eval_token(forth: Forth, token: ForthTok) -> Result(Forth, ForthError) { Value(v) -> Ok(Forth(..forth, stack: stack_push(forth.stack, v))) Word(w) -> // load word and evaluate with stack - case map.get(forth.env, w) { + case dict.get(forth.env, w) { // You're on your own when you redefine a built in. :) Ok(UserDef(body)) -> execute(forth, body) Ok(BuiltIn(bin)) -> execute_builtin(forth, bin) @@ -188,7 +188,7 @@ fn eval_token(forth: Forth, token: ForthTok) -> Result(Forth, ForthError) { Error(_) -> { // All the words in the definition need to be looked up in the environment use updated_def <- result.then(resolve_def(def, forth.env)) - Ok(Forth(..forth, env: map.insert(forth.env, w, updated_def))) + Ok(Forth(..forth, env: dict.insert(forth.env, w, updated_def))) } } } @@ -196,7 +196,7 @@ fn eval_token(forth: Forth, token: ForthTok) -> Result(Forth, ForthError) { fn resolve_def( definition_body: ForthWordDef, - env: map.Map(String, ForthWordDef), + env: dict.Dict(String, ForthWordDef), ) -> Result(ForthWordDef, ForthError) { case definition_body { BuiltIn(_) -> Ok(definition_body) @@ -211,11 +211,11 @@ fn resolve_def( fn lookup_token( token: ForthTok, - env: map.Map(String, ForthWordDef), + env: dict.Dict(String, ForthWordDef), ) -> Result(List(ForthTok), ForthError) { case token { Word(w) -> - case map.get(env, w) { + case dict.get(env, w) { Ok(UserDef(tokens)) -> Ok(tokens) Ok(BuiltIn(body)) -> Ok([Word(body)]) Error(_) -> Error(UnknownWord) diff --git a/exercises/practice/grade-school/.meta/example.gleam b/exercises/practice/grade-school/.meta/example.gleam index d69cb6228..82d70b3eb 100644 --- a/exercises/practice/grade-school/.meta/example.gleam +++ b/exercises/practice/grade-school/.meta/example.gleam @@ -1,4 +1,4 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/option.{None, Some} import gleam/result import gleam/string @@ -7,16 +7,16 @@ import gleam/pair import gleam/int pub type School { - School(grades: Map(Int, List(String))) + School(grades: Dict(Int, List(String))) } pub fn create() -> School { - School(map.new()) + School(dict.new()) } pub fn roster(school: School) -> List(String) { school.grades - |> map.to_list() + |> dict.to_list() |> list.sort(fn(a, b) { int.compare(a.0, b.0) }) |> list.map(pair.second) |> list.map(list.sort(_, string.compare)) @@ -31,7 +31,7 @@ pub fn add( case list.contains(roster(school), student) { True -> Error(Nil) False -> - map.update( + dict.update( in: school.grades, update: grade, with: fn(existing_students) { @@ -47,7 +47,7 @@ pub fn add( } pub fn grade(school: School, desired_grade: Int) -> List(String) { - map.get(school.grades, desired_grade) + dict.get(school.grades, desired_grade) |> result.unwrap([]) |> list.sort(string.compare) } diff --git a/exercises/practice/house/src/house.gleam b/exercises/practice/house/src/house.gleam index 954837cc9..04ec52640 100644 --- a/exercises/practice/house/src/house.gleam +++ b/exercises/practice/house/src/house.gleam @@ -1,3 +1,6 @@ -pub fn recite(start_verse start_verse: Int, end_verse end_verse: Int) -> String { +pub fn recite( + start_verse start_verse: Int, + end_verse end_verse: Int, +) -> String { todo } diff --git a/exercises/practice/minesweeper/.meta/example.gleam b/exercises/practice/minesweeper/.meta/example.gleam index 97b7bd16b..00c580b00 100644 --- a/exercises/practice/minesweeper/.meta/example.gleam +++ b/exercises/practice/minesweeper/.meta/example.gleam @@ -2,7 +2,7 @@ import gleam/string import gleam/int import gleam/list import gleam/pair -import gleam/map.{type Map} +import gleam/dict.{type Dict} type Position { Position(Int, Int) @@ -21,7 +21,7 @@ pub fn annotate(minefield: String) -> String { |> print } -fn parse(minefield: String) -> Map(Position, Cell) { +fn parse(minefield: String) -> Dict(Position, Cell) { minefield |> string.split("\n") |> list.index_map(fn(row, line) { @@ -35,18 +35,18 @@ fn parse(minefield: String) -> Map(Position, Cell) { }) }) |> list.flatten - |> map.from_list + |> dict.from_list } -fn do_annotate(cells: Map(Position, Cell)) -> Map(Position, Cell) { - map.fold(over: cells, from: cells, with: count_neighbors) +fn do_annotate(cells: Dict(Position, Cell)) -> Dict(Position, Cell) { + dict.fold(over: cells, from: cells, with: count_neighbors) } fn count_neighbors( - cells: Map(Position, Cell), + cells: Dict(Position, Cell), pos: Position, cell: Cell, -) -> Map(Position, Cell) { +) -> Dict(Position, Cell) { case cell { Empty -> { let Position(row, col) = pos @@ -61,23 +61,23 @@ fn count_neighbors( Position(row, col - 1), Position(row, col + 1), ] - |> list.filter(fn(pos) { map.get(cells, pos) == Ok(Bomb) }) + |> list.filter(fn(pos) { dict.get(cells, pos) == Ok(Bomb) }) |> list.length - map.insert(cells, pos, Neighbors(neighbors)) + dict.insert(cells, pos, Neighbors(neighbors)) } _ -> cells } } -fn print(cells: Map(Position, Cell)) -> String { +fn print(cells: Dict(Position, Cell)) -> String { cells - |> map.to_list + |> dict.to_list |> list.group(fn(pair) { let #(Position(row, _), _) = pair row }) - |> map.map_values(fn(_row, cells) { + |> dict.map_values(fn(_row, cells) { cells |> list.sort(fn(a, b) { let #(Position(_, col_a), _) = a @@ -88,7 +88,7 @@ fn print(cells: Map(Position, Cell)) -> String { |> list.map(print_cell) |> string.concat }) - |> map.to_list + |> dict.to_list |> list.sort(fn(a, b) { int.compare(pair.first(a), pair.first(b)) }) |> list.map(pair.second) |> string.join("\n") diff --git a/exercises/practice/nucleotide-count/.meta/example.gleam b/exercises/practice/nucleotide-count/.meta/example.gleam index 94a1a8003..0e0f85786 100644 --- a/exercises/practice/nucleotide-count/.meta/example.gleam +++ b/exercises/practice/nucleotide-count/.meta/example.gleam @@ -1,16 +1,16 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/string import gleam/list import gleam/result -pub fn nucleotide_count(dna: String) -> Result(Map(String, Int), Nil) { +pub fn nucleotide_count(dna: String) -> Result(Dict(String, Int), Nil) { dna |> string.to_graphemes() |> list.try_fold( - from: map.from_list([#("A", 0), #("C", 0), #("G", 0), #("T", 0)]), + from: dict.from_list([#("A", 0), #("C", 0), #("G", 0), #("T", 0)]), with: fn(counts, letter) { - map.get(counts, letter) - |> result.map(fn(count) { map.insert(counts, letter, count + 1) }) + dict.get(counts, letter) + |> result.map(fn(count) { dict.insert(counts, letter, count + 1) }) }, ) } diff --git a/exercises/practice/nucleotide-count/src/nucleotide_count.gleam b/exercises/practice/nucleotide-count/src/nucleotide_count.gleam index 34e5e09dd..f3f105e22 100644 --- a/exercises/practice/nucleotide-count/src/nucleotide_count.gleam +++ b/exercises/practice/nucleotide-count/src/nucleotide_count.gleam @@ -1,5 +1,5 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} -pub fn nucleotide_count(dna: String) -> Result(Map(String, Int), Nil) { +pub fn nucleotide_count(dna: String) -> Result(Dict(String, Int), Nil) { todo } diff --git a/exercises/practice/nucleotide-count/test/nucleotide_count_test.gleam b/exercises/practice/nucleotide-count/test/nucleotide_count_test.gleam index 3235bec81..d8edc8e89 100644 --- a/exercises/practice/nucleotide-count/test/nucleotide_count_test.gleam +++ b/exercises/practice/nucleotide-count/test/nucleotide_count_test.gleam @@ -1,4 +1,4 @@ -import gleam/map +import gleam/dict import exercism/test_runner import exercism/should import nucleotide_count @@ -11,7 +11,7 @@ pub fn empty_strand_test() { let strand = "" let expected = [#("A", 0), #("C", 0), #("G", 0), #("T", 0)] - |> map.from_list + |> dict.from_list |> Ok nucleotide_count.nucleotide_count(strand) |> should.equal(expected) @@ -21,7 +21,7 @@ pub fn can_count_one_nucleotide_in_single_character_input_test() { let strand = "G" let expected = [#("A", 0), #("C", 0), #("G", 1), #("T", 0)] - |> map.from_list + |> dict.from_list |> Ok nucleotide_count.nucleotide_count(strand) |> should.equal(expected) @@ -31,7 +31,7 @@ pub fn strand_with_repeated_nucleotide_test() { let strand = "GGGGGGG" let expected = [#("A", 0), #("C", 0), #("G", 7), #("T", 0)] - |> map.from_list + |> dict.from_list |> Ok nucleotide_count.nucleotide_count(strand) |> should.equal(expected) @@ -42,7 +42,7 @@ pub fn strand_with_multiple_nucleotides_test() { "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC" let expected = [#("A", 20), #("C", 12), #("G", 17), #("T", 21)] - |> map.from_list + |> dict.from_list |> Ok nucleotide_count.nucleotide_count(strand) |> should.equal(expected) diff --git a/exercises/practice/pov/test/pov_test.gleam b/exercises/practice/pov/test/pov_test.gleam index 58c48bd11..add8374a2 100644 --- a/exercises/practice/pov/test/pov_test.gleam +++ b/exercises/practice/pov/test/pov_test.gleam @@ -20,129 +20,117 @@ pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_can_reroot_a_tree_wi "x", ) |> result.map(normalize) - |> should.equal(Ok(Tree( - label: "x", - children: [Tree(label: "parent", children: [Tree("sibling", [])])], - ))) + |> should.equal( + Ok( + Tree(label: "x", children: [ + Tree(label: "parent", children: [Tree("sibling", [])]), + ]), + ), + ) } pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_can_reroot_a_tree_with_a_parent_and_many_siblings_test() { pov.from_pov( - Tree( - label: "parent", - children: [Tree("a", []), Tree("b", []), Tree("c", []), Tree("x", [])], - ), + Tree(label: "parent", children: [ + Tree("a", []), + Tree("b", []), + Tree("c", []), + Tree("x", []), + ]), "x", ) |> result.map(normalize) - |> should.equal(Ok(Tree( - label: "x", - children: [ - Tree( - label: "parent", - children: [Tree("a", []), Tree("b", []), Tree("c", [])], - ), - ], - ))) + |> should.equal( + Ok( + Tree(label: "x", children: [ + Tree(label: "parent", children: [ + Tree("a", []), + Tree("b", []), + Tree("c", []), + ]), + ]), + ), + ) } pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_can_reroot_a_tree_with_new_root_deeply_nested_in_tree_test() { pov.from_pov( - Tree( - label: "level-0", - children: [ - Tree( - label: "level-1", - children: [ - Tree( - label: "level-2", - children: [Tree(label: "level-3", children: [Tree("x", [])])], - ), - ], - ), - ], - ), + Tree(label: "level-0", children: [ + Tree(label: "level-1", children: [ + Tree(label: "level-2", children: [ + Tree(label: "level-3", children: [Tree("x", [])]), + ]), + ]), + ]), "x", ) |> result.map(normalize) - |> should.equal(Ok(Tree( - label: "x", - children: [ - Tree( - label: "level-3", - children: [ - Tree( - label: "level-2", - children: [Tree(label: "level-1", children: [Tree("level-0", [])])], - ), - ], - ), - ], - ))) + |> should.equal( + Ok( + Tree(label: "x", children: [ + Tree(label: "level-3", children: [ + Tree(label: "level-2", children: [ + Tree(label: "level-1", children: [Tree("level-0", [])]), + ]), + ]), + ]), + ), + ) } pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_moves_children_of_the_new_root_to_same_level_as_former_parent_test() { pov.from_pov( - Tree( - label: "parent", - children: [ - Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), - ], - ), + Tree(label: "parent", children: [ + Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), + ]), "x", ) |> result.map(normalize) - |> should.equal(Ok(Tree( - label: "x", - children: [Tree("kid-0", []), Tree("kid-1", []), Tree("parent", [])], - ))) + |> should.equal( + Ok( + Tree(label: "x", children: [ + Tree("kid-0", []), + Tree("kid-1", []), + Tree("parent", []), + ]), + ), + ) } pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_can_reroot_a_complex_tree_with_cousins_test() { pov.from_pov( - Tree( - label: "grandparent", - children: [ - Tree( - label: "parent", - children: [ - Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), - Tree("sibling-0", []), - Tree("sibling-1", []), - ], - ), - Tree( - label: "uncle", - children: [Tree("cousin-0", []), Tree("cousin-1", [])], - ), - ], - ), + Tree(label: "grandparent", children: [ + Tree(label: "parent", children: [ + Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), + Tree("sibling-0", []), + Tree("sibling-1", []), + ]), + Tree(label: "uncle", children: [ + Tree("cousin-0", []), + Tree("cousin-1", []), + ]), + ]), "x", ) |> result.map(normalize) - |> should.equal(Ok(Tree( - label: "x", - children: [ - Tree("kid-0", []), - Tree("kid-1", []), - Tree( - label: "parent", - children: [ - Tree( - label: "grandparent", - children: [ - Tree( - label: "uncle", - children: [Tree("cousin-0", []), Tree("cousin-1", [])], - ), - ], - ), + |> should.equal( + Ok( + Tree(label: "x", children: [ + Tree("kid-0", []), + Tree("kid-1", []), + Tree(label: "parent", children: [ + Tree(label: "grandparent", children: [ + Tree(label: "uncle", children: [ + Tree("cousin-0", []), + Tree("cousin-1", []), + ]), + ]), Tree("sibling-0", []), Tree("sibling-1", []), - ], - ), - ], - ))) + ]), + ]), + ), + ) } pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_errors_if_target_does_not_exist_in_a_singleton_tree_test() { @@ -152,14 +140,11 @@ pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_errors_if_target_doe pub fn reroot_a_tree_so_that_its_root_is_the_specified_node_errors_if_target_does_not_exist_in_a_large_tree_test() { pov.from_pov( - Tree( - label: "parent", - children: [ - Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), - Tree("sibling-0", []), - Tree("sibling-1", []), - ], - ), + Tree(label: "parent", children: [ + Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), + Tree("sibling-0", []), + Tree("sibling-1", []), + ]), "nonexistent", ) |> should.equal(Error(Nil)) @@ -176,10 +161,12 @@ pub fn given_two_nodes_find_the_path_between_them_can_find_path_to_parent_test() pub fn given_two_nodes_find_the_path_between_them_can_find_path_to_sibling_test() { pov.path_to( - tree: Tree( - label: "parent", - children: [Tree("a", []), Tree("x", []), Tree("b", []), Tree("c", [])], - ), + tree: Tree(label: "parent", children: [ + Tree("a", []), + Tree("x", []), + Tree("b", []), + Tree("c", []), + ]), from: "x", to: "b", ) @@ -188,23 +175,17 @@ pub fn given_two_nodes_find_the_path_between_them_can_find_path_to_sibling_test( pub fn given_two_nodes_find_the_path_between_them_can_find_path_to_cousin_test() { pov.path_to( - tree: Tree( - label: "grandparent", - children: [ - Tree( - label: "parent", - children: [ - Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), - Tree("sibling-0", []), - Tree("sibling-1", []), - ], - ), - Tree( - label: "uncle", - children: [Tree("cousin-0", []), Tree("cousin-1", [])], - ), - ], - ), + tree: Tree(label: "grandparent", children: [ + Tree(label: "parent", children: [ + Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), + Tree("sibling-0", []), + Tree("sibling-1", []), + ]), + Tree(label: "uncle", children: [ + Tree("cousin-0", []), + Tree("cousin-1", []), + ]), + ]), from: "x", to: "cousin-1", ) @@ -213,19 +194,13 @@ pub fn given_two_nodes_find_the_path_between_them_can_find_path_to_cousin_test() pub fn given_two_nodes_find_the_path_between_them_can_find_path_not_involving_root_test() { pov.path_to( - tree: Tree( - label: "grandparent", - children: [ - Tree( - label: "parent", - children: [ - Tree("x", []), - Tree("sibling-0", []), - Tree("sibling-1", []), - ], - ), - ], - ), + tree: Tree(label: "grandparent", children: [ + Tree(label: "parent", children: [ + Tree("x", []), + Tree("sibling-0", []), + Tree("sibling-1", []), + ]), + ]), from: "x", to: "sibling-1", ) @@ -234,10 +209,12 @@ pub fn given_two_nodes_find_the_path_between_them_can_find_path_not_involving_ro pub fn given_two_nodes_find_the_path_between_them_can_find_path_from_nodes_other_than_x_test() { pov.path_to( - tree: Tree( - label: "parent", - children: [Tree("a", []), Tree("x", []), Tree("b", []), Tree("c", [])], - ), + tree: Tree(label: "parent", children: [ + Tree("a", []), + Tree("x", []), + Tree("b", []), + Tree("c", []), + ]), from: "a", to: "c", ) @@ -246,14 +223,11 @@ pub fn given_two_nodes_find_the_path_between_them_can_find_path_from_nodes_other pub fn given_two_nodes_find_the_path_between_them_errors_if_destination_does_not_exist_test() { pov.path_to( - tree: Tree( - label: "parent", - children: [ - Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), - Tree("sibling-0", []), - Tree("sibling-1", []), - ], - ), + tree: Tree(label: "parent", children: [ + Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), + Tree("sibling-0", []), + Tree("sibling-1", []), + ]), from: "x", to: "nonexistent", ) @@ -262,14 +236,11 @@ pub fn given_two_nodes_find_the_path_between_them_errors_if_destination_does_not pub fn given_two_nodes_find_the_path_between_them_errors_if_source_does_not_exist_test() { pov.path_to( - tree: Tree( - label: "parent", - children: [ - Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), - Tree("sibling-0", []), - Tree("sibling-1", []), - ], - ), + tree: Tree(label: "parent", children: [ + Tree(label: "x", children: [Tree("kid-0", []), Tree("kid-1", [])]), + Tree("sibling-0", []), + Tree("sibling-1", []), + ]), from: "nonexistent", to: "x", ) diff --git a/exercises/practice/rectangles/.meta/example.gleam b/exercises/practice/rectangles/.meta/example.gleam index 0aaf737a2..4cd811001 100644 --- a/exercises/practice/rectangles/.meta/example.gleam +++ b/exercises/practice/rectangles/.meta/example.gleam @@ -1,6 +1,6 @@ import gleam/list import gleam/string -import gleam/map.{type Map} +import gleam/dict.{type Dict} type Wall { Corner @@ -14,7 +14,7 @@ pub fn rectangles(input: String) -> Int { |> find_rectangles } -fn parse_input(input: String) -> Map(#(Int, Int), Wall) { +fn parse_input(input: String) -> Dict(#(Int, Int), Wall) { input |> string.split("\n") |> list.index_map(fn(row, line) { @@ -31,19 +31,19 @@ fn parse_input(input: String) -> Map(#(Int, Int), Wall) { }) |> list.flatten |> list.flatten - |> map.from_list + |> dict.from_list } -fn find_rectangles(map: Map(#(Int, Int), Wall)) -> Int { - let corners = map.filter(map, fn(_pos, wall) { wall == Corner }) +fn find_rectangles(map: Dict(#(Int, Int), Wall)) -> Int { + let corners = dict.filter(map, fn(_pos, wall) { wall == Corner }) - map.fold( + dict.fold( from: 0, over: corners, with: fn(rectangles, position1, _corner) { let #(x1, y1) = position1 - map.fold( + dict.fold( from: rectangles, over: corners, with: fn(rectangles, position2, _corner2) { @@ -51,7 +51,7 @@ fn find_rectangles(map: Map(#(Int, Int), Wall)) -> Int { let position3 = #(x1, y2) let position4 = #(x2, y1) let is_rectangle = - x1 < x2 && y1 < y2 && map.has_key(corners, position3) && map.has_key( + x1 < x2 && y1 < y2 && dict.has_key(corners, position3) && dict.has_key( corners, position4, ) && has_vertical_walls_between(map, position1, position4) && has_vertical_walls_between( @@ -75,7 +75,7 @@ fn find_rectangles(map: Map(#(Int, Int), Wall)) -> Int { } fn has_vertical_walls_between( - map: Map(#(Int, Int), Wall), + map: Dict(#(Int, Int), Wall), position1: #(Int, Int), position2: #(Int, Int), ) -> Bool { @@ -87,7 +87,7 @@ fn has_vertical_walls_between( list.range(x1, x2) |> list.map(fn(x) { #(x, y1) }) |> list.all(fn(pos) { - case map.get(map, pos) { + case dict.get(map, pos) { Ok(Corner) -> True Ok(Vertical) -> True _ -> False @@ -96,7 +96,7 @@ fn has_vertical_walls_between( } fn has_horizontal_walls_between( - map: Map(#(Int, Int), Wall), + map: Dict(#(Int, Int), Wall), position1: #(Int, Int), position2: #(Int, Int), ) -> Bool { @@ -108,7 +108,7 @@ fn has_horizontal_walls_between( list.range(y1, y2) |> list.map(fn(y) { #(x1, y) }) |> list.all(fn(pos) { - case map.get(map, pos) { + case dict.get(map, pos) { Ok(Corner) -> True Ok(Horizontal) -> True _ -> False diff --git a/exercises/practice/rotational-cipher/.meta/example.gleam b/exercises/practice/rotational-cipher/.meta/example.gleam index a2b6dca1b..c6ccfcb9f 100644 --- a/exercises/practice/rotational-cipher/.meta/example.gleam +++ b/exercises/practice/rotational-cipher/.meta/example.gleam @@ -1,9 +1,9 @@ import gleam/string import gleam/list -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/result -fn shift_map(shift_key: Int, text: String) -> Map(String, String) { +fn shift_map(shift_key: Int, text: String) -> Dict(String, String) { let graphemes = string.to_graphemes(text) let shifted = list.append( @@ -11,11 +11,11 @@ fn shift_map(shift_key: Int, text: String) -> Map(String, String) { list.take(graphemes, shift_key), ) list.zip(graphemes, shifted) - |> map.from_list + |> dict.from_list } -fn shift_letters(shift_key: Int) -> Map(String, String) { - map.merge( +fn shift_letters(shift_key: Int) -> Dict(String, String) { + dict.merge( shift_map(shift_key, "abcdefghijklmnopqrstuvwxyz"), shift_map(shift_key, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"), ) @@ -27,7 +27,7 @@ pub fn rotate(shift_key: Int, text: String) -> String { text |> string.to_graphemes() |> list.map(fn(letter) { - map.get(letter_map, letter) + dict.get(letter_map, letter) |> result.unwrap(letter) }) |> string.concat diff --git a/exercises/practice/satellite/test/satellite_test.gleam b/exercises/practice/satellite/test/satellite_test.gleam index 24e71ee7b..c3ad06e65 100644 --- a/exercises/practice/satellite/test/satellite_test.gleam +++ b/exercises/practice/satellite/test/satellite_test.gleam @@ -17,19 +17,20 @@ pub fn tree_with_one_item_test() { } pub fn tree_with_many_items_test() { - satellite.tree_from_traversals( - inorder: ["i", "a", "f", "x", "r"], - preorder: ["a", "i", "x", "f", "r"], + satellite.tree_from_traversals(inorder: ["i", "a", "f", "x", "r"], preorder: [ + "a", "i", "x", "f", "r", + ]) + |> should.equal( + Ok(Node( + value: "a", + left: Node(value: "i", left: Nil, right: Nil), + right: Node( + value: "x", + left: Node(value: "f", left: Nil, right: Nil), + right: Node(value: "r", left: Nil, right: Nil), + ), + )), ) - |> should.equal(Ok(Node( - value: "a", - left: Node(value: "i", left: Nil, right: Nil), - right: Node( - value: "x", - left: Node(value: "f", left: Nil, right: Nil), - right: Node(value: "r", left: Nil, right: Nil), - ), - ))) } pub fn reject_traversals_of_different_length_test() { @@ -38,17 +39,15 @@ pub fn reject_traversals_of_different_length_test() { } pub fn reject_inconsistent_traversals_of_same_length_test() { - satellite.tree_from_traversals( - inorder: ["a", "b", "c"], - preorder: ["x", "y", "z"], - ) + satellite.tree_from_traversals(inorder: ["a", "b", "c"], preorder: [ + "x", "y", "z", + ]) |> should.equal(Error(DifferentItems)) } pub fn reject_traversals_with_repeated_items_test() { - satellite.tree_from_traversals( - inorder: ["b", "a", "a"], - preorder: ["a", "b", "a"], - ) + satellite.tree_from_traversals(inorder: ["b", "a", "a"], preorder: [ + "a", "b", "a", + ]) |> should.equal(Error(NonUniqueItems)) } diff --git a/exercises/practice/scrabble-score/.meta/example.gleam b/exercises/practice/scrabble-score/.meta/example.gleam index e42e2ada0..130b1cd54 100644 --- a/exercises/practice/scrabble-score/.meta/example.gleam +++ b/exercises/practice/scrabble-score/.meta/example.gleam @@ -1,10 +1,10 @@ import gleam/string -import gleam/map +import gleam/dict import gleam/list pub fn score(word word: String) -> Int { let word_values = - map.from_list([ + dict.from_list([ #("a", 1), #("b", 3), #("c", 3), @@ -39,7 +39,7 @@ pub fn score(word word: String) -> Int { |> list.fold( 0, fn(acc, x) { - case map.get(word_values, x) { + case dict.get(word_values, x) { Ok(value) -> acc + value _ -> acc } diff --git a/exercises/practice/series/test/series_test.gleam b/exercises/practice/series/test/series_test.gleam index 53dbc65e3..f4cf0861d 100644 --- a/exercises/practice/series/test/series_test.gleam +++ b/exercises/practice/series/test/series_test.gleam @@ -39,9 +39,9 @@ pub fn slices_can_include_duplicates_test() { pub fn slices_of_a_long_series_test() { "918493904243" |> series.slices(5) - |> should.equal(Ok([ - "91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243", - ])) + |> should.equal( + Ok(["91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243"]), + ) } pub fn slice_length_is_too_large_test() { diff --git a/exercises/practice/tournament/.meta/example.gleam b/exercises/practice/tournament/.meta/example.gleam index 5f07304c0..e27b6e19a 100644 --- a/exercises/practice/tournament/.meta/example.gleam +++ b/exercises/practice/tournament/.meta/example.gleam @@ -1,7 +1,7 @@ import gleam/string import gleam/list import gleam/int -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/option.{type Option, None, Some} import gleam/order.{type Order, Eq, Gt, Lt} @@ -27,18 +27,18 @@ type Score { const header = "Team | MP | W | D | L | P" -fn get_team_scores(input: String) -> Map(String, Score) { +fn get_team_scores(input: String) -> Dict(String, Score) { input |> string.split("\n") |> list.fold( - from: map.new(), + from: dict.new(), with: fn(scores, match) { let assert [team_a, team_b, result] = string.split(match, ";") let #(result_a, result_b) = parse_results(result) scores - |> map.update(team_a, increase(_, result_a)) - |> map.update(team_b, increase(_, result_b)) + |> dict.update(team_a, increase(_, result_a)) + |> dict.update(team_b, increase(_, result_b)) }, ) } @@ -62,10 +62,10 @@ fn increase(score: Option(Score), result: Result) -> Score { } } -fn print_table(scores: Map(String, Score)) -> String { +fn print_table(scores: Dict(String, Score)) -> String { let rows = scores - |> map.to_list + |> dict.to_list |> list.sort(by: points_then_names) |> list.map(format_row) diff --git a/exercises/practice/word-count/.meta/example.gleam b/exercises/practice/word-count/.meta/example.gleam index 5aa0eb1e9..d7c4d53d3 100644 --- a/exercises/practice/word-count/.meta/example.gleam +++ b/exercises/practice/word-count/.meta/example.gleam @@ -1,18 +1,18 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/list import gleam/regex import gleam/string import gleam/option -pub fn count_words(input: String) -> Map(String, Int) { +pub fn count_words(input: String) -> Dict(String, Int) { let assert Ok(re) = regex.from_string("[a-z0-9]+('[a-z0-9]+)?") input |> string.lowercase |> regex.scan(re, _) |> list.map(fn(m) { m.content }) - |> list.fold(map.new(), increment) + |> list.fold(dict.new(), increment) } -fn increment(counts: Map(String, Int), word: String) -> Map(String, Int) { - map.update(counts, word, fn(previous) { option.unwrap(previous, 0) + 1 }) +fn increment(counts: Dict(String, Int), word: String) -> Dict(String, Int) { + dict.update(counts, word, fn(previous) { option.unwrap(previous, 0) + 1 }) } diff --git a/exercises/practice/word-count/src/word_count.gleam b/exercises/practice/word-count/src/word_count.gleam index e2c84a78e..15fedc395 100644 --- a/exercises/practice/word-count/src/word_count.gleam +++ b/exercises/practice/word-count/src/word_count.gleam @@ -1,5 +1,5 @@ -import gleam/map.{type Map} +import gleam/dict.{type Dict} -pub fn count_words(input: String) -> Map(String, Int) { +pub fn count_words(input: String) -> Dict(String, Int) { todo } diff --git a/exercises/practice/word-count/test/word_count_test.gleam b/exercises/practice/word-count/test/word_count_test.gleam index c4f254052..babc4fcd6 100644 --- a/exercises/practice/word-count/test/word_count_test.gleam +++ b/exercises/practice/word-count/test/word_count_test.gleam @@ -1,5 +1,5 @@ import word_count -import gleam/map +import gleam/dict import exercism/test_runner import exercism/should @@ -10,19 +10,19 @@ pub fn main() { pub fn count_one_word_test() { "word" |> word_count.count_words - |> should.equal(map.from_list([#("word", 1)])) + |> should.equal(dict.from_list([#("word", 1)])) } pub fn count_one_of_each_word_test() { "one of each" |> word_count.count_words - |> should.equal(map.from_list([#("one", 1), #("of", 1), #("each", 1)])) + |> should.equal(dict.from_list([#("one", 1), #("of", 1), #("each", 1)])) } pub fn multiple_occurrences_of_a_word_test() { "one fish two fish red fish blue fish" |> word_count.count_words - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("one", 1), #("fish", 4), #("two", 1), @@ -34,19 +34,19 @@ pub fn multiple_occurrences_of_a_word_test() { pub fn handles_cramped_lists_test() { "one,two,three" |> word_count.count_words - |> should.equal(map.from_list([#("one", 1), #("two", 1), #("three", 1)])) + |> should.equal(dict.from_list([#("one", 1), #("two", 1), #("three", 1)])) } pub fn handles_expanded_lists_test() { "one,\ntwo,\nthree" |> word_count.count_words - |> should.equal(map.from_list([#("one", 1), #("two", 1), #("three", 1)])) + |> should.equal(dict.from_list([#("one", 1), #("two", 1), #("three", 1)])) } pub fn ignore_punctuation_test() { "car: carpet as java: javascript!!&@$%^&" |> word_count.count_words - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("car", 1), #("carpet", 1), #("as", 1), @@ -58,19 +58,19 @@ pub fn ignore_punctuation_test() { pub fn include_numbers_test() { "testing, 1, 2 testing" |> word_count.count_words - |> should.equal(map.from_list([#("testing", 2), #("1", 1), #("2", 1)])) + |> should.equal(dict.from_list([#("testing", 2), #("1", 1), #("2", 1)])) } pub fn normalize_case_test() { "go Go GO Stop stop" |> word_count.count_words - |> should.equal(map.from_list([#("go", 3), #("stop", 2)])) + |> should.equal(dict.from_list([#("go", 3), #("stop", 2)])) } pub fn with_apostrophes_test() { "'First: don't laugh. Then: don't cry. You're getting it.'" |> word_count.count_words - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("first", 1), #("don't", 2), #("laugh", 1), @@ -85,7 +85,7 @@ pub fn with_apostrophes_test() { pub fn with_quotations_test() { "Joe can't tell between 'large' and large." |> word_count.count_words - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("joe", 1), #("can't", 1), #("tell", 1), @@ -98,7 +98,7 @@ pub fn with_quotations_test() { pub fn substrings_from_the_beginning_test() { "Joe can't tell between app, apple and a." |> word_count.count_words - |> should.equal(map.from_list([ + |> should.equal(dict.from_list([ #("joe", 1), #("can't", 1), #("tell", 1), @@ -113,17 +113,17 @@ pub fn substrings_from_the_beginning_test() { pub fn multiple_spaces_not_detected_as_a_word_test() { " multiple whitespaces" |> word_count.count_words - |> should.equal(map.from_list([#("multiple", 1), #("whitespaces", 1)])) + |> should.equal(dict.from_list([#("multiple", 1), #("whitespaces", 1)])) } pub fn alternating_word_separators_not_detected_as_a_word_test() { ",\n,one,\n ,two \n 'three'" |> word_count.count_words - |> should.equal(map.from_list([#("one", 1), #("two", 1), #("three", 1)])) + |> should.equal(dict.from_list([#("one", 1), #("two", 1), #("three", 1)])) } pub fn quotation_for_word_with_apostrophe_test() { "can, can't, 'can't'" |> word_count.count_words - |> should.equal(map.from_list([#("can", 1), #("can't", 2)])) + |> should.equal(dict.from_list([#("can", 1), #("can't", 2)])) } diff --git a/exercises/practice/yacht/.meta/example.gleam b/exercises/practice/yacht/.meta/example.gleam index 908bd91ca..e3d004915 100644 --- a/exercises/practice/yacht/.meta/example.gleam +++ b/exercises/practice/yacht/.meta/example.gleam @@ -1,7 +1,7 @@ import gleam/list import gleam/int import gleam/function -import gleam/map +import gleam/dict import gleam/pair pub type Category { @@ -85,7 +85,7 @@ fn score_yacht(dice: List(Int)) -> Int { fn dice_with_count(dice: List(Int)) -> List(#(Int, Int)) { dice |> list.group(function.identity) - |> map.map_values(fn(_, matching_dice) { list.length(matching_dice) }) - |> map.to_list() + |> dict.map_values(fn(_, matching_dice) { list.length(matching_dice) }) + |> dict.to_list() |> list.sort(fn(a, b) { int.compare(pair.second(a), pair.second(b)) }) }