A type alias can be used in Gleam to give a convenient name for an existing type that would be otherwise cumbersome to write.
pub type Headers =
List(#(String, String))
pub fn html_headers() -> Headers {
[
#("content-type", "text/html"),
#("x-frame-options", "DENY"),
]
}
When written with pub type
the alias can be used outside of the module it is defined in. If pub
is omitted then the alias is private and cannot be referenced in other modules.
Dicts in Gleam are the data structure for storing information in key-value pairs. In other languages, these might also be known as associative arrays, hashes, or dictionaries.
Any type can be used for the keys and values in a dict, and they do not guarantee the order of their entries when accessed or returned.
Dicts are created and manipulated using functions from the gleam/dict
module.
// Create an empty dict
let dict1 = dict.new()
// Create a dict with some values
let dict2 = dict.from_list([
#("name", "Gleam"),
#("colour", "Pink"),
])
// Add a value to a dict
let dict3 = dict.insert(dict2, "website", "https://gleam.run")
// Get a value from a dict
let name = dict.get(dict3, "name")
// -> Ok("Gleam")
In this exercise, you are implementing a way to keep track of the high scores for the most popular game in your local arcade hall.
You have 5 functions to implement, mostly related to manipulating an object that holds high scores.
Create a function create_score_board
that returns a dict that serves as a high score board.
The keys of this object will be the names of the players, the values will be their scores.
For testing purposes, you want to directly include one entry in the object.
This initial entry should consist of "The Best Ever"
as player name and 1_000_000
as score.
create_score_board()
// returns an object with one initial entry
To add a player to the high score board, define the function add_player
.
It accepts 3 parameters:
The function returns a dict with the new player and score added.
If players violate the rules of the arcade hall, they are manually removed from the high score board.
Define remove_player
which takes 2 parameters:
This function should return the dict without the player that was removed.
If the player was not on the board in the first place, nothing should happen to the board, it should be returned as is.
If a player finishes another game at the arcade hall, a certain amount of points will be added to the previous score on the board.
Implement update_score
, which takes 3 parameters:
The function should return a dict with the updated score.
If the player was not on the board in the first place, nothing should happen to the board, it should be returned as is.
The arcade hall keeps a separate score board on Mondays. At the end of the day, each player on that board gets 100 additional points.
Implement the function apply_monday_bonus
that accepts a score board.
The function returns a dict with the bonus points added for each player that is listed on that board.
Sign up to Exercism to learn and master Gleam with 33 concepts, 122 exercises, and real human mentoring, all for free.