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.
Maps 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 map, and they do not guarantee the order of their entries when accessed or returned.
Maps are created and manipulated using functions from the gleam/map
module.
// Create an empty map
let map1 = map.new()
// Create a map with some values
let map2 = map.from_list([
#("name", "Gleam"),
#("colour", "Pink"),
])
// Add a value to a map
let map3 = map.insert(map2, "website", "https://gleam.run")
// Get a value from a map
let name = map.get(map3, "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 6 functions to implement, mostly related to manipulating an object that holds high scores.
Create a function create_score_board
that returns a map 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 1000000
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 map 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 map 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 map with the updated score.
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 map with the bonus points added for each player that is listed on that board.
Sign up to Exercism to learn and master Gleam with 36 concepts, 124 exercises, and real human mentoring, all for free.