Skip to content

Commit

Permalink
Update exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Jan 16, 2024
1 parent 0bb626b commit bef98bd
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 378 deletions.
1 change: 0 additions & 1 deletion .tool-versions

This file was deleted.

42 changes: 25 additions & 17 deletions exercise_generator/src/exercise_generator.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
Expand Down Expand Up @@ -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, _)) =
Expand Down Expand Up @@ -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")

Expand All @@ -238,7 +244,7 @@ fn write_solution_files(
}

fn functions_to_implement(test_cases: List(TestCase)) -> Map(String, Function) {
list.fold(over: test_cases, from: map.new(), with: check_test_case)
list.fold(over: test_cases, from: dict.new(), with: check_test_case)
}

fn check_test_case(
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -368,18 +374,20 @@ 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()
_ -> print_comments(comments)
}
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 ->
Expand Down Expand Up @@ -427,11 +435,11 @@ fn get_expected_value(
functions: Map(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) <> ")"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import gleam/map.{type Map}
import gleam/dict.{type Dict}

pub type ScoreBoard =
Map(String, Int)
Expand Down
22 changes: 11 additions & 11 deletions exercises/concept/high-score-board/test/high_score_board_test.gleam
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import high_score_board
import exercism/test_runner
import exercism/should
import gleam/map
import gleam/dict

pub fn main() {
test_runner.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),
Expand All @@ -25,17 +25,17 @@ 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),
]))
}

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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down
14 changes: 5 additions & 9 deletions exercises/practice/accumulate/test/accumulate_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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"]])
}
Loading

0 comments on commit bef98bd

Please sign in to comment.