Compare commits
10 Commits
2ac630dd8c
...
fad9b41d97
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fad9b41d97 | ||
|
|
89739137e7 | ||
|
|
c2bd0afb7d | ||
|
|
74e8c6d6dc | ||
|
|
05a0ab003b | ||
|
|
c0308da8c5 | ||
|
|
2c54444d46 | ||
|
|
1c8dcb28be | ||
|
|
0df72539ef | ||
|
|
b8181175c9 |
24
go/.exercism/config.json
Normal file
24
go/.exercism/config.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"lpil"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/go.gleam"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/go_test.gleam"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/example.gleam"
|
||||||
|
],
|
||||||
|
"invalidator": [
|
||||||
|
"gleam.toml",
|
||||||
|
"manifest.toml"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"forked_from": [
|
||||||
|
"elm/go"
|
||||||
|
],
|
||||||
|
"blurb": "Learn usage of Result by applying the rules of the game of Go"
|
||||||
|
}
|
||||||
1
go/.exercism/metadata.json
Normal file
1
go/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"gleam","exercise":"go","id":"5f92ded7f0d94054b5eb825d783c3245","url":"https://exercism.org/tracks/gleam/exercises/go","handle":"fw353qwgs","is_requester":true,"auto_approve":false}
|
||||||
4
go/.gitignore
vendored
Normal file
4
go/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
build
|
||||||
|
erl_crash.dump
|
||||||
32
go/HELP.md
Normal file
32
go/HELP.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run the command `gleam test` from within the exercise directory.
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/go.gleam` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
|
||||||
|
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
To get help if you're having trouble, you can use one of the following resources:
|
||||||
|
|
||||||
|
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
|
||||||
|
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
|
||||||
|
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||||
5
go/HINTS.md
Normal file
5
go/HINTS.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Hints
|
||||||
|
|
||||||
|
- The [Result module][result-module] has some useful functions for working with the Result type, such as `result.map` and `result.try`.
|
||||||
|
|
||||||
|
[result-module]: https://hexdocs.pm/gleam_stdlib/gleam/result.html
|
||||||
107
go/README.md
Normal file
107
go/README.md
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
# Go
|
||||||
|
|
||||||
|
Welcome to Go on Exercism's Gleam Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## Results
|
||||||
|
|
||||||
|
Gleam doesn't use exceptions for error handling, instead the generic `Result` type is returned by functions that can either succeed or fail.
|
||||||
|
|
||||||
|
The `Result` type is built into the language so you don't need to define or import it, but if you were to define it yourself it would look like this:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pub type Result(value, error) {
|
||||||
|
Ok(value)
|
||||||
|
Error(error)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The `Ok` variant is returned when a function succeeds, and the `Error` variant is returned when a function fails.
|
||||||
|
|
||||||
|
Results are very common in Gleam, and Gleam programmers will commonly use the [`gleam/result` module](https://hexdocs.pm/gleam_stdlib/gleam/result.html) to make working with them easier.
|
||||||
|
|
||||||
|
The `result.map` function can be used to call a function on the value inside a result if it is an `Ok`, or to pass through an `Error` unchanged.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
Ok(1)
|
||||||
|
|> result.map(fn(x) { x + 1 })
|
||||||
|
// -> Ok(2)
|
||||||
|
|
||||||
|
Error("Oh no!")
|
||||||
|
|> result.map(fn(x) { x + 1 })
|
||||||
|
// -> Error("Oh no!")
|
||||||
|
```
|
||||||
|
|
||||||
|
The `result.try` function is similar, but the callback function is expected to return a result. This is useful for chaining together multiple functions that return results.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
Ok(1)
|
||||||
|
|> result.try(fn(x) { Ok(x + 1) })
|
||||||
|
// -> Ok(2)
|
||||||
|
|
||||||
|
Ok(1)
|
||||||
|
|> result.try(fn(x) { Error("Nope!") })
|
||||||
|
// -> Error("Nope!")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
In this exercise, you'll be applying the [rules of the game of Go](https://matmoore.github.io/learngo/). The rules themselves are already written, you just need to apply them in order to update the Game and to handle any errors / violations of the rules.
|
||||||
|
|
||||||
|
The game is represented as follows:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pub type Player {
|
||||||
|
Black
|
||||||
|
White
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub type Game {
|
||||||
|
Game(
|
||||||
|
white_captured_stones: Int,
|
||||||
|
black_captured_stones: Int,
|
||||||
|
player: Player,
|
||||||
|
error: String,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
There are 4 rules in the game:
|
||||||
|
|
||||||
|
- Each point can only have one stone.
|
||||||
|
- Opposition stones can be captured.
|
||||||
|
- You can not place a stone where it would capture itself.
|
||||||
|
- You can not use the same point twice.
|
||||||
|
|
||||||
|
## 1. Apply the rules
|
||||||
|
|
||||||
|
Write the content of the `apply_rules` function, which takes an initial `Game` and a set of rules, and returns the new `Game` after the rules have been applied.
|
||||||
|
|
||||||
|
Three of the rules all check for violations of the rules and may return an error, and so have the return type of `Result(Game, String)`. If any of these rules fail, the original game should be returned, but with the `error` field updated with the relevant error.
|
||||||
|
|
||||||
|
The other rule does not check for violations and so cannot fail (although it can return a changed `Game`), and so has the return type of `Game`.
|
||||||
|
|
||||||
|
If all the rules pass, then any changes to `Game` from the rules should be kept, and the player should be changed.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pub fn apply_rules(
|
||||||
|
game: Game,
|
||||||
|
rule1: fn(Game) -> Result(Game, String),
|
||||||
|
rule2: fn(Game) -> Game,
|
||||||
|
rule3: fn(Game) -> Result(Game, String),
|
||||||
|
rule4: fn(Game) -> Result(Game, String),
|
||||||
|
) -> Game {
|
||||||
|
// -> If all rules pass, return a `Game` with all changes from the rules applied, and change player
|
||||||
|
// -> If any rule fails, return the original Game, but with the error field set
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @lpil
|
||||||
14
go/gleam.toml
Normal file
14
go/gleam.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name = "go"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gleam_otp = "~> 0.7 or ~> 1.0"
|
||||||
|
gleam_stdlib = ">= 0.54.0 or ~> 1.0"
|
||||||
|
simplifile = "~> 1.0"
|
||||||
|
gleam_erlang = ">= 0.25.0 and < 1.0.0"
|
||||||
|
gleam_yielder = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_regexp = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_deque = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
exercism_test_runner = "~> 1.9"
|
||||||
31
go/manifest.toml
Normal file
31
go/manifest.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||||
|
{ name = "exercism_test_runner", version = "1.9.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "0B17BB25F2FF1E60266467C24FE0CA04005410306AA05E9A4B41B1852D72865C" },
|
||||||
|
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
|
||||||
|
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
|
||||||
|
{ name = "glance", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "E155BA1A787FD11827048355021C0390D2FE9A518485526F631A9D472858CC6D" },
|
||||||
|
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||||
|
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||||
|
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||||
|
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||||
|
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||||
|
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||||
|
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
|
||||||
|
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
|
||||||
|
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||||
|
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||||
|
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
exercism_test_runner = { version = "~> 1.9" }
|
||||||
|
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
|
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
|
||||||
|
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
|
||||||
|
gleam_regexp = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
gleam_stdlib = { version = ">= 0.54.0 or ~> 1.0" }
|
||||||
|
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
simplifile = { version = "~> 1.0" }
|
||||||
39
go/src/go.gleam
Normal file
39
go/src/go.gleam
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import gleam/result
|
||||||
|
|
||||||
|
pub type Player {
|
||||||
|
Black
|
||||||
|
White
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Game {
|
||||||
|
Game(
|
||||||
|
white_captured_stones: Int,
|
||||||
|
black_captured_stones: Int,
|
||||||
|
player: Player,
|
||||||
|
error: String,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn apply_rules(
|
||||||
|
game: Game,
|
||||||
|
rule1: fn(Game) -> Result(Game, String),
|
||||||
|
rule2: fn(Game) -> Game,
|
||||||
|
rule3: fn(Game) -> Result(Game, String),
|
||||||
|
rule4: fn(Game) -> Result(Game, String),
|
||||||
|
) -> Game {
|
||||||
|
game
|
||||||
|
|> rule1
|
||||||
|
|> result.map(rule2)
|
||||||
|
|> result.try(rule3)
|
||||||
|
|> result.try(rule4)
|
||||||
|
|> fn(result) {
|
||||||
|
case result {
|
||||||
|
Error(err) -> Game(..game, error: err)
|
||||||
|
Ok(g) ->
|
||||||
|
case g.player {
|
||||||
|
Black -> Game(..g, player: White)
|
||||||
|
White -> Game(..g, player: Black)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
110
go/test/go_test.gleam
Normal file
110
go/test/go_test.gleam
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import exercism/should
|
||||||
|
import exercism/test_runner
|
||||||
|
import go.{type Game, Black, Game, White}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
test_runner.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn identity(x: Game) -> Game {
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
fn identity_rule(game: Game) -> Result(Game, String) {
|
||||||
|
Ok(game)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn error_rule(_game: Game, message: String) -> Result(Game, String) {
|
||||||
|
Error(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_game() -> Game {
|
||||||
|
Game(0, 0, White, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ko_rule() -> String {
|
||||||
|
"Cannot repeat a previously played board position"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn liberty_rule() -> String {
|
||||||
|
"Cannot place a stone with no liberties"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn one_stone_per_point_rule() -> String {
|
||||||
|
"You can't put a stone on top of another stone"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_white_captured_stone(game: Game) -> Game {
|
||||||
|
Game(..game, white_captured_stones: 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn change_player_if_all_rules_pass_test() {
|
||||||
|
new_game()
|
||||||
|
|> go.apply_rules(identity_rule, identity, identity_rule, identity_rule)
|
||||||
|
|> should.equal(change_player(new_game()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn retain_error_and_player_if_ko_rule_fails_test() {
|
||||||
|
new_game()
|
||||||
|
|> go.apply_rules(identity_rule, identity, identity_rule, error_rule(
|
||||||
|
_,
|
||||||
|
ko_rule(),
|
||||||
|
))
|
||||||
|
|> should.equal(Game(..new_game(), error: ko_rule()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn retain_error_and_player_if_liberty_rule_fails_test() {
|
||||||
|
new_game()
|
||||||
|
|> go.apply_rules(
|
||||||
|
identity_rule,
|
||||||
|
identity,
|
||||||
|
error_rule(_, liberty_rule()),
|
||||||
|
identity_rule,
|
||||||
|
)
|
||||||
|
|> should.equal(Game(..new_game(), error: liberty_rule()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn retain_error_and_player_if_one_stone_per_point_rule_fails_test() {
|
||||||
|
new_game()
|
||||||
|
|> go.apply_rules(
|
||||||
|
error_rule(_, one_stone_per_point_rule()),
|
||||||
|
identity,
|
||||||
|
identity_rule,
|
||||||
|
identity_rule,
|
||||||
|
)
|
||||||
|
|> should.equal(Game(..new_game(), error: one_stone_per_point_rule()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn retain_changes_from_capture_rule_and_change_player_test() {
|
||||||
|
new_game()
|
||||||
|
|> go.apply_rules(
|
||||||
|
identity_rule,
|
||||||
|
add_white_captured_stone,
|
||||||
|
identity_rule,
|
||||||
|
identity_rule,
|
||||||
|
)
|
||||||
|
|> should.equal(
|
||||||
|
new_game()
|
||||||
|
|> add_white_captured_stone
|
||||||
|
|> change_player,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn discard_changes_from_capture_rule_if_subsequent_rule_fails_test() {
|
||||||
|
new_game()
|
||||||
|
|> go.apply_rules(
|
||||||
|
identity_rule,
|
||||||
|
add_white_captured_stone,
|
||||||
|
identity_rule,
|
||||||
|
error_rule(_, ko_rule()),
|
||||||
|
)
|
||||||
|
|> should.equal(Game(..new_game(), error: ko_rule()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn change_player(game: Game) -> Game {
|
||||||
|
let new_player = case game.player {
|
||||||
|
White -> Black
|
||||||
|
Black -> White
|
||||||
|
}
|
||||||
|
Game(..game, player: new_player)
|
||||||
|
}
|
||||||
24
high-school-sweetheart/.exercism/config.json
Normal file
24
high-school-sweetheart/.exercism/config.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"lpil"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/high_school_sweetheart.gleam"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/high_school_sweetheart_test.gleam"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/example.gleam"
|
||||||
|
],
|
||||||
|
"invalidator": [
|
||||||
|
"gleam.toml",
|
||||||
|
"manifest.toml"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"forked_from": [
|
||||||
|
"elixir/high-school-sweetheart"
|
||||||
|
],
|
||||||
|
"blurb": "Learn about the pipe operator by helping high school sweethearts profess their love on social media via ASCII art."
|
||||||
|
}
|
||||||
1
high-school-sweetheart/.exercism/metadata.json
Normal file
1
high-school-sweetheart/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"gleam","exercise":"high-school-sweetheart","id":"496bd682b8cc4070ab04077da46db44f","url":"https://exercism.org/tracks/gleam/exercises/high-school-sweetheart","handle":"fw353qwgs","is_requester":true,"auto_approve":false}
|
||||||
4
high-school-sweetheart/.gitignore
vendored
Normal file
4
high-school-sweetheart/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
build
|
||||||
|
erl_crash.dump
|
||||||
32
high-school-sweetheart/HELP.md
Normal file
32
high-school-sweetheart/HELP.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run the command `gleam test` from within the exercise directory.
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/high_school_sweetheart.gleam` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
|
||||||
|
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
To get help if you're having trouble, you can use one of the following resources:
|
||||||
|
|
||||||
|
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
|
||||||
|
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
|
||||||
|
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||||
30
high-school-sweetheart/HINTS.md
Normal file
30
high-school-sweetheart/HINTS.md
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# Hints
|
||||||
|
|
||||||
|
## General
|
||||||
|
|
||||||
|
- Browse the [functions available in the _string module_][string-module-functions] to discover which operations on strings Gleam's standard library offers.
|
||||||
|
|
||||||
|
## 1. Get the name's first letter
|
||||||
|
|
||||||
|
- There is a [function][string-first] to get the first character from a string.
|
||||||
|
- There are multiple [functions][string-trim] to remove leading, trailing, or leading and trailing whitespaces from a string.
|
||||||
|
|
||||||
|
## 2. Format the first letter as an initial
|
||||||
|
|
||||||
|
- There is a [function][string-upcase] to convert all characters in a string to their uppercase variant.
|
||||||
|
- The `<>` operator can be used to concatenate strings.
|
||||||
|
|
||||||
|
## 3. Split the full name into the first name and the last name
|
||||||
|
|
||||||
|
- There is a [function][string-split] that splits a string on whitespace characters.
|
||||||
|
- A few first elements of a list can be assigned to variables by pattern matching on the list.
|
||||||
|
|
||||||
|
## 4. Put the initials inside of the heart
|
||||||
|
|
||||||
|
- The `<>` operator can be used to concatenate strings.
|
||||||
|
|
||||||
|
[string-module-functions]: https://hexdocs.pm/gleam_stdlib/gleam/string.html
|
||||||
|
[string-first]: https://hexdocs.pm/gleam_stdlib/gleam/string.html#first
|
||||||
|
[string-trim]: https://hexdocs.pm/gleam_stdlib/gleam/string.html#trim
|
||||||
|
[string-upcase]: https://hexdocs.pm/gleam_stdlib/gleam/string.html#uppercase
|
||||||
|
[string-split]: https://hexdocs.pm/gleam_stdlib/gleam/string.html#split
|
||||||
127
high-school-sweetheart/README.md
Normal file
127
high-school-sweetheart/README.md
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
# High School Sweetheart
|
||||||
|
|
||||||
|
Welcome to High School Sweetheart on Exercism's Gleam Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## Pipe Operator
|
||||||
|
|
||||||
|
The `|>` operator is called the pipe operator. It can be used to chain function calls together in such a way that the value returned by the previous function call is passed to the next function call.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
"hello"
|
||||||
|
|> string.uppercase
|
||||||
|
|> string.append("?!")
|
||||||
|
// -> "HELLO?!"
|
||||||
|
```
|
||||||
|
|
||||||
|
The above code is equivalent to the following:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
string.append(string.uppercase("hello"), "?!")
|
||||||
|
```
|
||||||
|
|
||||||
|
The pipe operator will either pass the value as the first argument to the function call, or the only argument to a new call, selecting whichever would have the correct type.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
100
|
||||||
|
|> function_that_takes_two_arguments(1)
|
||||||
|
|
||||||
|
// Is equivalent to
|
||||||
|
function_that_takes_two_arguments(100, 1)
|
||||||
|
```
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
100
|
||||||
|
|> function_that_returns_a_function(1)
|
||||||
|
|
||||||
|
// Is equivalent to
|
||||||
|
function_that_returns_a_function(1)(100)
|
||||||
|
```
|
||||||
|
|
||||||
|
Sometimes we want to pass the value into another position, in this case the `_` placeholder can be used to indicate where the value should be inserted.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
100
|
||||||
|
|> some_function(1, _, 2)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
In this exercise, you are going to help high school sweethearts profess their love on social media by generating an ASCII heart with their initials:
|
||||||
|
|
||||||
|
```
|
||||||
|
****** ******
|
||||||
|
** ** ** **
|
||||||
|
** ** ** **
|
||||||
|
** * **
|
||||||
|
** **
|
||||||
|
** J. K. + M. B. **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
***
|
||||||
|
*
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1. Get the name's first letter
|
||||||
|
|
||||||
|
Implement the `first_letter` function. It should take a name and return its first letter. It should clean up any unnecessary whitespace from the name.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
first_letter("Jane")
|
||||||
|
// -> "J"
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Format the first letter as an initial
|
||||||
|
|
||||||
|
Implement the `initial` function. It should take a name and return its first letter, uppercase, followed by a dot. Make sure to reuse `first_letter` that you defined in the previous step.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
initial("Robert")
|
||||||
|
// -> "R."
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Split the full name into the first name and the last name
|
||||||
|
|
||||||
|
Implement the `initials` function. It should take a full name, consisting of a first name and a last name separated by a space, and return the initials. Make sure to reuse `initial` that you defined in the previous step.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
initials("Lance Green")
|
||||||
|
// -> "L. G."
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. Put the initials inside of the heart
|
||||||
|
|
||||||
|
Implement the `pair` function. It should take two full names and return the initials inside an ASCII heart. Make sure to reuse `initials` that you defined in the previous step.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pair("Blake Miller", "Riley Lewis")
|
||||||
|
// -> "
|
||||||
|
// ****** ******
|
||||||
|
// ** ** ** **
|
||||||
|
// ** ** ** **
|
||||||
|
// ** * **
|
||||||
|
// ** **
|
||||||
|
// ** B. M. + R. L. **
|
||||||
|
// ** **
|
||||||
|
// ** **
|
||||||
|
// ** **
|
||||||
|
// ** **
|
||||||
|
// ** **
|
||||||
|
// ** **
|
||||||
|
// ***
|
||||||
|
// *
|
||||||
|
// "
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @lpil
|
||||||
14
high-school-sweetheart/gleam.toml
Normal file
14
high-school-sweetheart/gleam.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name = "high_school_sweetheart"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gleam_otp = "~> 0.7 or ~> 1.0"
|
||||||
|
gleam_stdlib = ">= 0.54.0 or ~> 1.0"
|
||||||
|
simplifile = "~> 1.0"
|
||||||
|
gleam_erlang = ">= 0.25.0 and < 1.0.0"
|
||||||
|
gleam_yielder = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_regexp = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_deque = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
exercism_test_runner = "~> 1.9"
|
||||||
31
high-school-sweetheart/manifest.toml
Normal file
31
high-school-sweetheart/manifest.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||||
|
{ name = "exercism_test_runner", version = "1.9.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "0B17BB25F2FF1E60266467C24FE0CA04005410306AA05E9A4B41B1852D72865C" },
|
||||||
|
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
|
||||||
|
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
|
||||||
|
{ name = "glance", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "E155BA1A787FD11827048355021C0390D2FE9A518485526F631A9D472858CC6D" },
|
||||||
|
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||||
|
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||||
|
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||||
|
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||||
|
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||||
|
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||||
|
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
|
||||||
|
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
|
||||||
|
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||||
|
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||||
|
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
exercism_test_runner = { version = "~> 1.9" }
|
||||||
|
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
|
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
|
||||||
|
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
|
||||||
|
gleam_regexp = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
gleam_stdlib = { version = ">= 0.54.0 or ~> 1.0" }
|
||||||
|
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
simplifile = { version = "~> 1.0" }
|
||||||
34
high-school-sweetheart/src/high_school_sweetheart.gleam
Normal file
34
high-school-sweetheart/src/high_school_sweetheart.gleam
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import gleam/list
|
||||||
|
import gleam/result
|
||||||
|
import gleam/string
|
||||||
|
|
||||||
|
pub fn first_letter(name: String) {
|
||||||
|
name |> string.trim |> string.first |> result.unwrap("")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initial(name: String) {
|
||||||
|
name |> first_letter |> string.uppercase |> string.append(".")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initials(full_name: String) {
|
||||||
|
full_name |> string.split(" ") |> list.map(initial) |> string.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pair(full_name1: String, full_name2: String) {
|
||||||
|
"
|
||||||
|
****** ******
|
||||||
|
** ** ** **
|
||||||
|
** ** ** **
|
||||||
|
** * **
|
||||||
|
** **
|
||||||
|
** " <> initials(full_name1) <> " + " <> initials(full_name2) <> " **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
***
|
||||||
|
*
|
||||||
|
"
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
import exercism/should
|
||||||
|
import exercism/test_runner
|
||||||
|
import high_school_sweetheart
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
test_runner.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn first_letter_test() {
|
||||||
|
high_school_sweetheart.first_letter("Mary")
|
||||||
|
|> should.equal("M")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn first_letter_does_not_change_case_test() {
|
||||||
|
high_school_sweetheart.first_letter("john")
|
||||||
|
|> should.equal("j")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn first_letter_removes_whitespace_test() {
|
||||||
|
high_school_sweetheart.first_letter("\n\t Sarah ")
|
||||||
|
|> should.equal("S")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initial_test() {
|
||||||
|
high_school_sweetheart.initial("Betty")
|
||||||
|
|> should.equal("B.")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initial_uppercases_letter_test() {
|
||||||
|
high_school_sweetheart.initial("james")
|
||||||
|
|> should.equal("J.")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn initials_test() {
|
||||||
|
high_school_sweetheart.initials("Linda Miller")
|
||||||
|
|> should.equal("L. M.")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pair_test() {
|
||||||
|
high_school_sweetheart.pair("Avery Bryant", "Charlie Dixon")
|
||||||
|
|> should.equal(
|
||||||
|
"
|
||||||
|
****** ******
|
||||||
|
** ** ** **
|
||||||
|
** ** ** **
|
||||||
|
** * **
|
||||||
|
** **
|
||||||
|
** A. B. + C. D. **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
** **
|
||||||
|
***
|
||||||
|
*
|
||||||
|
",
|
||||||
|
)
|
||||||
|
}
|
||||||
26
resistor-color/.exercism/config.json
Normal file
26
resistor-color/.exercism/config.json
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"lpil"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"itsgreggreg"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/resistor_color.gleam"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/resistor_color_test.gleam"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.gleam"
|
||||||
|
],
|
||||||
|
"invalidator": [
|
||||||
|
"gleam.toml",
|
||||||
|
"manifest.toml"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Convert a resistor band's color to its numeric representation.",
|
||||||
|
"source": "Maud de Vries, Erik Schierboom",
|
||||||
|
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
|
||||||
|
}
|
||||||
1
resistor-color/.exercism/metadata.json
Normal file
1
resistor-color/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"gleam","exercise":"resistor-color","id":"f06cbb641e8b4bb498e5670da2e735a5","url":"https://exercism.org/tracks/gleam/exercises/resistor-color","handle":"fw353qwgs","is_requester":true,"auto_approve":false}
|
||||||
4
resistor-color/.gitignore
vendored
Normal file
4
resistor-color/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
build
|
||||||
|
erl_crash.dump
|
||||||
32
resistor-color/HELP.md
Normal file
32
resistor-color/HELP.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run the command `gleam test` from within the exercise directory.
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/resistor_color.gleam` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
|
||||||
|
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
To get help if you're having trouble, you can use one of the following resources:
|
||||||
|
|
||||||
|
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
|
||||||
|
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
|
||||||
|
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||||
58
resistor-color/README.md
Normal file
58
resistor-color/README.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# Resistor Color
|
||||||
|
|
||||||
|
Welcome to Resistor Color on Exercism's Gleam Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
If you want to build something using a Raspberry Pi, you'll probably use _resistors_.
|
||||||
|
For this exercise, you need to know two things about them:
|
||||||
|
|
||||||
|
- Each resistor has a resistance value.
|
||||||
|
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
|
||||||
|
|
||||||
|
To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values.
|
||||||
|
Each band has a position and a numeric value.
|
||||||
|
|
||||||
|
The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.
|
||||||
|
|
||||||
|
In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.
|
||||||
|
|
||||||
|
These colors are encoded as follows:
|
||||||
|
|
||||||
|
- black: 0
|
||||||
|
- brown: 1
|
||||||
|
- red: 2
|
||||||
|
- orange: 3
|
||||||
|
- yellow: 4
|
||||||
|
- green: 5
|
||||||
|
- blue: 6
|
||||||
|
- violet: 7
|
||||||
|
- grey: 8
|
||||||
|
- white: 9
|
||||||
|
|
||||||
|
The goal of this exercise is to create a way:
|
||||||
|
|
||||||
|
- to look up the numerical value associated with a particular color band
|
||||||
|
- to list the different band colors
|
||||||
|
|
||||||
|
Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array:
|
||||||
|
Better Be Right Or Your Great Big Values Go Wrong.
|
||||||
|
|
||||||
|
More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article][e-color-code].
|
||||||
|
|
||||||
|
[e-color-code]: https://en.wikipedia.org/wiki/Electronic_color_code
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @lpil
|
||||||
|
|
||||||
|
### Contributed to by
|
||||||
|
|
||||||
|
- @itsgreggreg
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1458
|
||||||
14
resistor-color/gleam.toml
Normal file
14
resistor-color/gleam.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name = "resistor_color"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gleam_otp = "~> 0.7 or ~> 1.0"
|
||||||
|
gleam_stdlib = ">= 0.54.0 or ~> 1.0"
|
||||||
|
simplifile = "~> 1.0"
|
||||||
|
gleam_erlang = ">= 0.25.0 and < 1.0.0"
|
||||||
|
gleam_yielder = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_regexp = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_deque = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
exercism_test_runner = "~> 1.9"
|
||||||
31
resistor-color/manifest.toml
Normal file
31
resistor-color/manifest.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||||
|
{ name = "exercism_test_runner", version = "1.9.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "0B17BB25F2FF1E60266467C24FE0CA04005410306AA05E9A4B41B1852D72865C" },
|
||||||
|
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
|
||||||
|
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
|
||||||
|
{ name = "glance", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "E155BA1A787FD11827048355021C0390D2FE9A518485526F631A9D472858CC6D" },
|
||||||
|
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||||
|
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||||
|
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||||
|
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||||
|
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||||
|
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||||
|
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
|
||||||
|
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
|
||||||
|
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||||
|
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||||
|
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
exercism_test_runner = { version = "~> 1.9" }
|
||||||
|
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
|
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
|
||||||
|
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
|
||||||
|
gleam_regexp = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
gleam_stdlib = { version = ">= 0.54.0 or ~> 1.0" }
|
||||||
|
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
simplifile = { version = "~> 1.0" }
|
||||||
42
resistor-color/src/resistor_color.gleam
Normal file
42
resistor-color/src/resistor_color.gleam
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
pub type Color {
|
||||||
|
Black
|
||||||
|
Brown
|
||||||
|
Red
|
||||||
|
Orange
|
||||||
|
Yellow
|
||||||
|
Green
|
||||||
|
Blue
|
||||||
|
Violet
|
||||||
|
Grey
|
||||||
|
White
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn code(color: Color) -> Int {
|
||||||
|
case find_index(colors(), fn(c) { c == color }) {
|
||||||
|
Error(_) -> -1
|
||||||
|
Ok(code) -> code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn colors() -> List(Color) {
|
||||||
|
[Black, Brown, Red, Orange, Yellow, Green, Blue, Violet, Grey, White]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_index(list: List(a), predicate: fn(a) -> Bool) -> Result(Int, Nil) {
|
||||||
|
find_index_loop(list, predicate, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_index_loop(
|
||||||
|
list: List(a),
|
||||||
|
predicate: fn(a) -> Bool,
|
||||||
|
index: Int,
|
||||||
|
) -> Result(Int, Nil) {
|
||||||
|
case list {
|
||||||
|
[] -> Error(Nil)
|
||||||
|
[head, ..tail] ->
|
||||||
|
case predicate(head) {
|
||||||
|
True -> Ok(index)
|
||||||
|
False -> find_index_loop(tail, predicate, index + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
77
resistor-color/test/resistor_color_test.gleam
Normal file
77
resistor-color/test/resistor_color_test.gleam
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
import exercism/should
|
||||||
|
import exercism/test_runner
|
||||||
|
import resistor_color.{
|
||||||
|
Black, Blue, Brown, Green, Grey, Orange, Red, Violet, White, Yellow,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
test_runner.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn black_test() {
|
||||||
|
resistor_color.code(Black)
|
||||||
|
|> should.equal(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn brown_test() {
|
||||||
|
resistor_color.code(Brown)
|
||||||
|
|> should.equal(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn red_test() {
|
||||||
|
resistor_color.code(Red)
|
||||||
|
|> should.equal(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn orange_test() {
|
||||||
|
resistor_color.code(Orange)
|
||||||
|
|> should.equal(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn yellow_test() {
|
||||||
|
resistor_color.code(Yellow)
|
||||||
|
|> should.equal(4)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn green_test() {
|
||||||
|
resistor_color.code(Green)
|
||||||
|
|> should.equal(5)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn blue_test() {
|
||||||
|
resistor_color.code(Blue)
|
||||||
|
|> should.equal(6)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn violet_test() {
|
||||||
|
resistor_color.code(Violet)
|
||||||
|
|> should.equal(7)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn grey_test() {
|
||||||
|
resistor_color.code(Grey)
|
||||||
|
|> should.equal(8)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn white_test() {
|
||||||
|
resistor_color.code(White)
|
||||||
|
|> should.equal(9)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn colors_test() {
|
||||||
|
let colors = [
|
||||||
|
Black,
|
||||||
|
Brown,
|
||||||
|
Red,
|
||||||
|
Orange,
|
||||||
|
Yellow,
|
||||||
|
Green,
|
||||||
|
Blue,
|
||||||
|
Violet,
|
||||||
|
Grey,
|
||||||
|
White,
|
||||||
|
]
|
||||||
|
|
||||||
|
resistor_color.colors()
|
||||||
|
|> should.equal(colors)
|
||||||
|
}
|
||||||
22
robot-simulator/.exercism/config.json
Normal file
22
robot-simulator/.exercism/config.json
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"jiegillet"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/robot_simulator.gleam"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/robot_simulator_test.gleam"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.gleam"
|
||||||
|
],
|
||||||
|
"invalidator": [
|
||||||
|
"gleam.toml",
|
||||||
|
"manifest.toml"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Write a robot simulator.",
|
||||||
|
"source": "Inspired by an interview question at a famous company."
|
||||||
|
}
|
||||||
1
robot-simulator/.exercism/metadata.json
Normal file
1
robot-simulator/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"gleam","exercise":"robot-simulator","id":"09f398f0a29f4e6d9e8c542184b210a7","url":"https://exercism.org/tracks/gleam/exercises/robot-simulator","handle":"fw353qwgs","is_requester":true,"auto_approve":false}
|
||||||
4
robot-simulator/.gitignore
vendored
Normal file
4
robot-simulator/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
build
|
||||||
|
erl_crash.dump
|
||||||
32
robot-simulator/HELP.md
Normal file
32
robot-simulator/HELP.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run the command `gleam test` from within the exercise directory.
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/robot_simulator.gleam` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
|
||||||
|
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
To get help if you're having trouble, you can use one of the following resources:
|
||||||
|
|
||||||
|
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
|
||||||
|
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
|
||||||
|
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||||
40
robot-simulator/README.md
Normal file
40
robot-simulator/README.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# Robot Simulator
|
||||||
|
|
||||||
|
Welcome to Robot Simulator on Exercism's Gleam Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Write a robot simulator.
|
||||||
|
|
||||||
|
A robot factory's test facility needs a program to verify robot movements.
|
||||||
|
|
||||||
|
The robots have three possible movements:
|
||||||
|
|
||||||
|
- turn right
|
||||||
|
- turn left
|
||||||
|
- advance
|
||||||
|
|
||||||
|
Robots are placed on a hypothetical infinite grid, facing a particular direction (north, east, south, or west) at a set of {x,y} coordinates,
|
||||||
|
e.g., {3,8}, with coordinates increasing to the north and east.
|
||||||
|
|
||||||
|
The robot then receives a number of instructions, at which point the testing facility verifies the robot's new position, and in which direction it is pointing.
|
||||||
|
|
||||||
|
- The letter-string "RAALAL" means:
|
||||||
|
- Turn right
|
||||||
|
- Advance twice
|
||||||
|
- Turn left
|
||||||
|
- Advance once
|
||||||
|
- Turn left yet again
|
||||||
|
- Say a robot starts at {7, 3} facing north.
|
||||||
|
Then running this stream of instructions should leave it at {9, 4} facing west.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @jiegillet
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Inspired by an interview question at a famous company.
|
||||||
14
robot-simulator/gleam.toml
Normal file
14
robot-simulator/gleam.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name = "robot_simulator"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gleam_otp = "~> 0.7 or ~> 1.0"
|
||||||
|
gleam_stdlib = ">= 0.54.0 or ~> 1.0"
|
||||||
|
simplifile = "~> 1.0"
|
||||||
|
gleam_erlang = ">= 0.25.0 and < 1.0.0"
|
||||||
|
gleam_yielder = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_regexp = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_deque = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
exercism_test_runner = "~> 1.9"
|
||||||
31
robot-simulator/manifest.toml
Normal file
31
robot-simulator/manifest.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||||
|
{ name = "exercism_test_runner", version = "1.9.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "0B17BB25F2FF1E60266467C24FE0CA04005410306AA05E9A4B41B1852D72865C" },
|
||||||
|
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
|
||||||
|
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
|
||||||
|
{ name = "glance", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "E155BA1A787FD11827048355021C0390D2FE9A518485526F631A9D472858CC6D" },
|
||||||
|
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||||
|
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||||
|
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||||
|
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||||
|
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||||
|
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||||
|
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
|
||||||
|
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
|
||||||
|
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||||
|
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||||
|
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
exercism_test_runner = { version = "~> 1.9" }
|
||||||
|
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
|
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
|
||||||
|
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
|
||||||
|
gleam_regexp = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
gleam_stdlib = { version = ">= 0.54.0 or ~> 1.0" }
|
||||||
|
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
simplifile = { version = "~> 1.0" }
|
||||||
84
robot-simulator/src/robot_simulator.gleam
Normal file
84
robot-simulator/src/robot_simulator.gleam
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import gleam/io
|
||||||
|
import gleam/string
|
||||||
|
|
||||||
|
pub type Robot {
|
||||||
|
Robot(direction: Direction, position: Position)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Direction {
|
||||||
|
North
|
||||||
|
East
|
||||||
|
South
|
||||||
|
West
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Turn {
|
||||||
|
Turn(left: Direction, right: Direction)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Position {
|
||||||
|
Position(x: Int, y: Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(direction: Direction, position: Position) -> Robot {
|
||||||
|
Robot(direction:, position:)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn move(
|
||||||
|
direction: Direction,
|
||||||
|
position: Position,
|
||||||
|
instructions: String,
|
||||||
|
) -> Robot {
|
||||||
|
string.split(instructions, "")
|
||||||
|
|> move_loop(#(direction, position))
|
||||||
|
|> fn(dp) { Robot(dp.0, dp.1) }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_loop(
|
||||||
|
instructions: List(String),
|
||||||
|
dp: #(Direction, Position),
|
||||||
|
) -> #(Direction, Position) {
|
||||||
|
io.debug(instructions)
|
||||||
|
io.debug(dp)
|
||||||
|
case instructions {
|
||||||
|
[] -> dp
|
||||||
|
[head, ..tail] -> handle_instruction(head, dp) |> move_loop(tail, _)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_instruction(
|
||||||
|
instruction: String,
|
||||||
|
dp: #(Direction, Position),
|
||||||
|
) -> #(Direction, Position) {
|
||||||
|
case instruction {
|
||||||
|
"L" | "R" -> #(turn(dp.0, instruction), dp.1)
|
||||||
|
"A" -> advance(dp)
|
||||||
|
_ -> dp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn turn(facing: Direction, to: String) -> Direction {
|
||||||
|
let turn = case facing {
|
||||||
|
North -> Turn(left: West, right: East)
|
||||||
|
East -> Turn(left: North, right: South)
|
||||||
|
South -> Turn(left: East, right: West)
|
||||||
|
West -> Turn(left: South, right: North)
|
||||||
|
}
|
||||||
|
|
||||||
|
case to {
|
||||||
|
"L" -> turn.left
|
||||||
|
"R" -> turn.right
|
||||||
|
_ -> facing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn advance(dp: #(Direction, Position)) -> #(Direction, Position) {
|
||||||
|
let #(facing, p) = dp
|
||||||
|
case facing {
|
||||||
|
North -> Position(..p, y: p.y + 1)
|
||||||
|
East -> Position(..p, x: p.x + 1)
|
||||||
|
South -> Position(..p, y: p.y - 1)
|
||||||
|
West -> Position(..p, x: p.x - 1)
|
||||||
|
}
|
||||||
|
|> fn(updated_position: Position) { #(facing, updated_position) }
|
||||||
|
}
|
||||||
97
robot-simulator/test/robot_simulator_test.gleam
Normal file
97
robot-simulator/test/robot_simulator_test.gleam
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
import exercism/should
|
||||||
|
import exercism/test_runner
|
||||||
|
import robot_simulator.{East, North, Position, Robot, South, West}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
test_runner.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_robot_at_origin_facing_north_test() {
|
||||||
|
robot_simulator.create(North, Position(x: 0, y: 0))
|
||||||
|
|> should.equal(Robot(North, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_robot_at_negative_position_facing_south_test() {
|
||||||
|
robot_simulator.create(South, Position(x: -1, y: -1))
|
||||||
|
|> should.equal(Robot(South, Position(x: -1, y: -1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_clockwise_changes_north_to_east_test() {
|
||||||
|
robot_simulator.move(North, Position(x: 0, y: 0), "R")
|
||||||
|
|> should.equal(Robot(East, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_clockwise_changes_east_to_south_test() {
|
||||||
|
robot_simulator.move(East, Position(x: 0, y: 0), "R")
|
||||||
|
|> should.equal(Robot(South, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_clockwise_changes_south_to_west_test() {
|
||||||
|
robot_simulator.move(South, Position(x: 0, y: 0), "R")
|
||||||
|
|> should.equal(Robot(West, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_clockwise_changes_west_to_north_test() {
|
||||||
|
robot_simulator.move(West, Position(x: 0, y: 0), "R")
|
||||||
|
|> should.equal(Robot(North, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_counter_clockwise_changes_north_to_west_test() {
|
||||||
|
robot_simulator.move(North, Position(x: 0, y: 0), "L")
|
||||||
|
|> should.equal(Robot(West, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_counter_clockwise_changes_west_to_south_test() {
|
||||||
|
robot_simulator.move(West, Position(x: 0, y: 0), "L")
|
||||||
|
|> should.equal(Robot(South, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_counter_clockwise_changes_south_to_east_test() {
|
||||||
|
robot_simulator.move(South, Position(x: 0, y: 0), "L")
|
||||||
|
|> should.equal(Robot(East, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn rotating_counter_clockwise_changes_east_to_north_test() {
|
||||||
|
robot_simulator.move(East, Position(x: 0, y: 0), "L")
|
||||||
|
|> should.equal(Robot(North, Position(x: 0, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn moving_forward_one_facing_north_increments_y_test() {
|
||||||
|
robot_simulator.move(North, Position(x: 0, y: 0), "A")
|
||||||
|
|> should.equal(Robot(North, Position(x: 0, y: 1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn moving_forward_one_facing_south_decrements_y_test() {
|
||||||
|
robot_simulator.move(South, Position(x: 0, y: 0), "A")
|
||||||
|
|> should.equal(Robot(South, Position(x: 0, y: -1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn moving_forward_one_facing_east_increments_x_test() {
|
||||||
|
robot_simulator.move(East, Position(x: 0, y: 0), "A")
|
||||||
|
|> should.equal(Robot(East, Position(x: 1, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn moving_forward_one_facing_west_decrements_x_test() {
|
||||||
|
robot_simulator.move(West, Position(x: 0, y: 0), "A")
|
||||||
|
|> should.equal(Robot(West, Position(x: -1, y: 0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn follow_series_of_instructions_moving_east_and_north_from_readme_test() {
|
||||||
|
robot_simulator.move(North, Position(x: 7, y: 3), "RAALAL")
|
||||||
|
|> should.equal(Robot(West, Position(x: 9, y: 4)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn follow_series_of_instructions_moving_west_and_north_test() {
|
||||||
|
robot_simulator.move(North, Position(x: 0, y: 0), "LAAARALA")
|
||||||
|
|> should.equal(Robot(West, Position(x: -4, y: 1)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn follow_series_of_instructions_moving_west_and_south_test() {
|
||||||
|
robot_simulator.move(East, Position(x: 2, y: -7), "RRAAAAALA")
|
||||||
|
|> should.equal(Robot(South, Position(x: -3, y: -8)))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn follow_series_of_instructions_moving_east_and_north_test() {
|
||||||
|
robot_simulator.move(South, Position(x: 8, y: 4), "LAAARRRALLLL")
|
||||||
|
|> should.equal(Robot(North, Position(x: 11, y: 5)))
|
||||||
|
}
|
||||||
25
tisbury-treasure-hunt/.exercism/config.json
Normal file
25
tisbury-treasure-hunt/.exercism/config.json
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"lpil"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/tisbury_treasure_hunt.gleam"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/tisbury_treasure_hunt_test.gleam"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/example.gleam"
|
||||||
|
],
|
||||||
|
"invalidator": [
|
||||||
|
"gleam.toml",
|
||||||
|
"manifest.toml"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"forked_from": [
|
||||||
|
"elm/tisbury-treasure-hunt",
|
||||||
|
"fsharp/tisbury-treasure-hunt"
|
||||||
|
],
|
||||||
|
"blurb": "Learn usage of Tuples by designing a pirate treasure game"
|
||||||
|
}
|
||||||
1
tisbury-treasure-hunt/.exercism/metadata.json
Normal file
1
tisbury-treasure-hunt/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"gleam","exercise":"tisbury-treasure-hunt","id":"44a79fd118554307960fba0f9a0d6637","url":"https://exercism.org/tracks/gleam/exercises/tisbury-treasure-hunt","handle":"fw353qwgs","is_requester":true,"auto_approve":false}
|
||||||
4
tisbury-treasure-hunt/.gitignore
vendored
Normal file
4
tisbury-treasure-hunt/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
build
|
||||||
|
erl_crash.dump
|
||||||
32
tisbury-treasure-hunt/HELP.md
Normal file
32
tisbury-treasure-hunt/HELP.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run the command `gleam test` from within the exercise directory.
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/tisbury_treasure_hunt.gleam` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
|
||||||
|
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
To get help if you're having trouble, you can use one of the following resources:
|
||||||
|
|
||||||
|
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
|
||||||
|
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
|
||||||
|
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||||
13
tisbury-treasure-hunt/HINTS.md
Normal file
13
tisbury-treasure-hunt/HINTS.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Hints
|
||||||
|
|
||||||
|
## 1. Convert locations
|
||||||
|
|
||||||
|
- Elements can be accessed from a tuple using pattern matching or indexing.
|
||||||
|
|
||||||
|
## 2. Compare treasure and place locations
|
||||||
|
|
||||||
|
- Tuples can be compared for equality if they have the same length and the same types in the same order.
|
||||||
|
|
||||||
|
## 3. Count treasure locations
|
||||||
|
|
||||||
|
- The `filter` function from the `gleam/list` module can be used to remove elements from a list based on a function that returns a bool.
|
||||||
155
tisbury-treasure-hunt/README.md
Normal file
155
tisbury-treasure-hunt/README.md
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
# Tisbury Treasure Hunt
|
||||||
|
|
||||||
|
Welcome to Tisbury Treasure Hunt on Exercism's Gleam Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## Tuples
|
||||||
|
|
||||||
|
A tuple is an ordered container of values. Like all Gleam data types tuples are immutable.
|
||||||
|
Each element of a tuple can be of a different type -- they can even be other tuples.
|
||||||
|
|
||||||
|
Tuples are defined as comma-separated values between `#(` and `)`: `#(1, 2.0, "Three")`.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
#("one", 2) // Tuple pair (2 values)
|
||||||
|
#("one", 2, 3.0) // Tuple triplet (3 values)
|
||||||
|
```
|
||||||
|
|
||||||
|
Tuples with the same length and the same types (in the same order) can be compared for equality.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
#(1, 2) == #(1, 2) // Same length, same types, same values, same order
|
||||||
|
// -> True
|
||||||
|
|
||||||
|
#(1, 2) == #(2, 1) // Same length, same types, same values, different order
|
||||||
|
// -> False
|
||||||
|
|
||||||
|
#(1, 2) == #(1, "2") // Same length, different types
|
||||||
|
// Compile error!
|
||||||
|
|
||||||
|
#(1, 2) == #(1, 2, 3) // Different length
|
||||||
|
// Compile error!
|
||||||
|
```
|
||||||
|
|
||||||
|
There are three ways in which you can get the contained values out of a tuple:
|
||||||
|
|
||||||
|
- Indexing.
|
||||||
|
- Pattern matching with `let`.
|
||||||
|
- Pattern matching with `case`.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
let person = #("Jordan", 170)
|
||||||
|
|
||||||
|
// Option 1: Indexing
|
||||||
|
person.0 // -> "Jordan"
|
||||||
|
person.1 // -> 170
|
||||||
|
|
||||||
|
// Option 2: let
|
||||||
|
let #(name2, length2) = person
|
||||||
|
// -> name2 = "Jordan"
|
||||||
|
// -> length2 = 170
|
||||||
|
|
||||||
|
// Option 3: case
|
||||||
|
case person {
|
||||||
|
#(name3, length3) -> {
|
||||||
|
name3 // -> "Jordan"
|
||||||
|
length3 // -> 170
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Aazra and Rui are designing a pirate-themed treasure hunt. There is a list of treasures with map locations, the other a list of place names with map locations.
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<tr><th>Azara's List</th><th></th><th>Rui's List</th></tr>
|
||||||
|
<tr><td>
|
||||||
|
|
||||||
|
| Treasure | Location |
|
||||||
|
| --------------------------- | -------- |
|
||||||
|
| Amethyst Octopus | (1, F) |
|
||||||
|
| Angry Monkey Figurine | (5, B) |
|
||||||
|
| Antique Glass Fishnet Float | (3, D) |
|
||||||
|
| Brass Spyglass | (4, B) |
|
||||||
|
| Carved Wooden Elephant | (8, C) |
|
||||||
|
| Crystal Crab | (6, A) |
|
||||||
|
| Glass Starfish | (6, D) |
|
||||||
|
| Model Ship in Large Bottle | (8, A) |
|
||||||
|
| Pirate Flag | (7, F) |
|
||||||
|
| Robot Parrot | (1, C) |
|
||||||
|
| Scrimshaw Whale's Tooth | (1, F) |
|
||||||
|
| Silver Seahorse | (4, E) |
|
||||||
|
| Vintage Pirate Hat | (7, E) |
|
||||||
|
|
||||||
|
</td><td></td><td>
|
||||||
|
|
||||||
|
| Place Name | Location | Quadrant |
|
||||||
|
| ------------------------------------- | -------- | -------- |
|
||||||
|
| Seaside Cottages | (C, 1) | Blue |
|
||||||
|
| Aqua Lagoon (Island of Mystery) | (F, 1) | Yellow |
|
||||||
|
| Deserted Docks | (A, 2) | Blue |
|
||||||
|
| Spiky Rocks | (D, 3) | Yellow |
|
||||||
|
| Abandoned Lighthouse | (B, 4) | Blue |
|
||||||
|
| Hidden Spring (Island of Mystery) | (E, 4) | Yellow |
|
||||||
|
| Stormy Breakwater | (B, 5) | Purple |
|
||||||
|
| Old Schooner | (A, 6) | Purple |
|
||||||
|
| Tangled Seaweed Patch | (D, 6) | Orange |
|
||||||
|
| Quiet Inlet (Island of Mystery) | (E, 7) | Orange |
|
||||||
|
| Windswept Hilltop (Island of Mystery) | (F, 7) | Orange |
|
||||||
|
| Harbor Managers Office | (A, 8) | Purple |
|
||||||
|
| Foggy Seacave | (C, 8) | Purple |
|
||||||
|
|
||||||
|
</td></tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
But things are a bit disorganized: Aazra's coordinates appear to be formatted and sorted differently from Rui's, and they have to keep looking from one list to the other to figure out which treasures go with which locations. Being budding Gleamlins, they have come to you for help in writing a small program (a set of functions, really) to better organize their hunt information.
|
||||||
|
|
||||||
|
## 1. Convert locations
|
||||||
|
|
||||||
|
Implement the `place_location_to_treasure_location` function that takes a place Location (such as `#("C", 1)`) and converts it to a treasure location (such as `#(1, "C")`).
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
place_location_to_treasure_location(#("C", 1))
|
||||||
|
// -> #(1, "C")
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Compare treasure and place locations
|
||||||
|
|
||||||
|
Implement the `treasure_location_matches_place_location` function that takes a place location (such as `#("C", 1)`) and returns `True` if it matches a treasure location (such as `#(1, "C")`).
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
treasure_location_matches_place_location(#("C", 1), #(1, "C"))
|
||||||
|
// -> True
|
||||||
|
|
||||||
|
treasure_location_matches_place_location(#("C", 1), #(2, "C"))
|
||||||
|
// -> False
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Count treasure locations
|
||||||
|
|
||||||
|
Implement the `count_place_treasures` function, that takes a place (such as `#("Aqua Lagoon (Island of Mystery)", #("F", 1))`), and the list of treasures, and returns the number of treasures that can be found there.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
let place = #("Aqua Lagoon (Island of Mystery)", #("F", 1))
|
||||||
|
|
||||||
|
count_place_treasures(place, treasures)
|
||||||
|
// -> 2
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. Special Places
|
||||||
|
|
||||||
|
Implement the `special_case_swap_possible` function, which takes a treasure (such as `#("Amethyst Octopus", #(1, "F"))`), a Place (such as `#("Seaside Cottages", #("C", 1))`) and a desired treasure (such as `#("Crystal Crab", #(6, "A"))`), and returns `True` for the following combinations:
|
||||||
|
|
||||||
|
- The Brass Spyglass can be swapped for any other treasure at the Abandoned Lighthouse.
|
||||||
|
- The Amethyst Octopus can be swapped for the Crystal Crab or the Glass Starfish at the Stormy Breakwater.
|
||||||
|
- The Vintage Pirate Hat can be swapped for the Model Ship in Large Bottle or the Antique Glass Fishnet Float at the Harbor Managers Office.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @lpil
|
||||||
14
tisbury-treasure-hunt/gleam.toml
Normal file
14
tisbury-treasure-hunt/gleam.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name = "tisbury_treasure_hunt"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gleam_otp = "~> 0.7 or ~> 1.0"
|
||||||
|
gleam_stdlib = ">= 0.54.0 or ~> 1.0"
|
||||||
|
simplifile = "~> 1.0"
|
||||||
|
gleam_erlang = ">= 0.25.0 and < 1.0.0"
|
||||||
|
gleam_yielder = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_regexp = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_deque = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
exercism_test_runner = "~> 1.9"
|
||||||
31
tisbury-treasure-hunt/manifest.toml
Normal file
31
tisbury-treasure-hunt/manifest.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||||
|
{ name = "exercism_test_runner", version = "1.9.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "0B17BB25F2FF1E60266467C24FE0CA04005410306AA05E9A4B41B1852D72865C" },
|
||||||
|
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
|
||||||
|
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
|
||||||
|
{ name = "glance", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "E155BA1A787FD11827048355021C0390D2FE9A518485526F631A9D472858CC6D" },
|
||||||
|
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||||
|
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||||
|
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||||
|
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||||
|
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||||
|
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||||
|
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
|
||||||
|
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
|
||||||
|
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||||
|
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||||
|
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
exercism_test_runner = { version = "~> 1.9" }
|
||||||
|
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
|
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
|
||||||
|
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
|
||||||
|
gleam_regexp = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
gleam_stdlib = { version = ">= 0.54.0 or ~> 1.0" }
|
||||||
|
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
simplifile = { version = "~> 1.0" }
|
||||||
48
tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam
Normal file
48
tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
import gleam/list
|
||||||
|
|
||||||
|
pub fn place_location_to_treasure_location(
|
||||||
|
place_location: #(String, Int),
|
||||||
|
) -> #(Int, String) {
|
||||||
|
#(place_location.1, place_location.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn treasure_location_matches_place_location(
|
||||||
|
place_location: #(String, Int),
|
||||||
|
treasure_location: #(Int, String),
|
||||||
|
) -> Bool {
|
||||||
|
place_location_to_treasure_location(place_location) == treasure_location
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count_place_treasures(
|
||||||
|
place: #(String, #(String, Int)),
|
||||||
|
treasures: List(#(String, #(Int, String))),
|
||||||
|
) -> Int {
|
||||||
|
treasures
|
||||||
|
|> list.filter(fn(t) {
|
||||||
|
treasure_location_matches_place_location(place.1, t.1)
|
||||||
|
})
|
||||||
|
|> list.length
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn special_case_swap_possible(
|
||||||
|
found_treasure: #(String, #(Int, String)),
|
||||||
|
place: #(String, #(String, Int)),
|
||||||
|
desired_treasure: #(String, #(Int, String)),
|
||||||
|
) -> Bool {
|
||||||
|
case found_treasure, desired_treasure, place {
|
||||||
|
#("Brass Spyglass", _), _, #("Abandoned Lighthouse", _) -> True
|
||||||
|
#("Amethyst Octopus", _), #("Crystal Crab", _), #("Stormy Breakwater", _) ->
|
||||||
|
True
|
||||||
|
#("Amethyst Octopus", _), #("Glass Starfish", _), #("Stormy Breakwater", _) ->
|
||||||
|
True
|
||||||
|
#("Vintage Pirate Hat", _),
|
||||||
|
#("Model Ship in Large Bottle", _),
|
||||||
|
#("Harbor Managers Office", _)
|
||||||
|
-> True
|
||||||
|
#("Vintage Pirate Hat", _),
|
||||||
|
#("Antique Glass Fishnet Float", _),
|
||||||
|
#("Harbor Managers Office", _)
|
||||||
|
-> True
|
||||||
|
_, _, _ -> False
|
||||||
|
}
|
||||||
|
}
|
||||||
103
tisbury-treasure-hunt/test/tisbury_treasure_hunt_test.gleam
Normal file
103
tisbury-treasure-hunt/test/tisbury_treasure_hunt_test.gleam
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
import exercism/should
|
||||||
|
import exercism/test_runner
|
||||||
|
import tisbury_treasure_hunt
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
test_runner.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn place_location_to_treasure_location_test() {
|
||||||
|
tisbury_treasure_hunt.place_location_to_treasure_location(#("C", 1))
|
||||||
|
|> should.equal(#(1, "C"))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seaside_cottages_is_not_at_1f_test() {
|
||||||
|
let assert False =
|
||||||
|
tisbury_treasure_hunt.treasure_location_matches_place_location(#("C", 1), #(
|
||||||
|
1,
|
||||||
|
"F",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn aqua_lagoon_is_at_1f_test() {
|
||||||
|
let assert True =
|
||||||
|
tisbury_treasure_hunt.treasure_location_matches_place_location(#("F", 1), #(
|
||||||
|
1,
|
||||||
|
"F",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn places_should_know_how_many_treasures_are_available_test() {
|
||||||
|
let treasures = [
|
||||||
|
#("Amethyst Octopus", #(1, "F")),
|
||||||
|
#("Scrimshaw Whale's Tooth", #(1, "F")),
|
||||||
|
]
|
||||||
|
let assert 2 =
|
||||||
|
tisbury_treasure_hunt.count_place_treasures(
|
||||||
|
#("Aqua Lagoon (Island of Mystery)", #("F", 1)),
|
||||||
|
treasures,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn can_swap_amethyst_octopus_for_crystal_crab_at_stormy_breakwater_test() {
|
||||||
|
let assert True =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Amethyst Octopus", #(1, "F")),
|
||||||
|
#("Stormy Breakwater", #("B", 5)),
|
||||||
|
#("Crystal Crab", #(6, "A")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn can_swap_amethyst_octopus_for_glass_starfish_at_stormy_breakwater_test() {
|
||||||
|
let assert True =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Amethyst Octopus", #(1, "F")),
|
||||||
|
#("Stormy Breakwater", #("B", 5)),
|
||||||
|
#("Glass Starfish", #(6, "D")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cannot_swap_amethyst_octopus_for_angry_monkey_figurine_at_stormy_breakwater_test() {
|
||||||
|
let assert False =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Amethyst Octopus", #(1, "F")),
|
||||||
|
#("Stormy Breakwater", #("B", 5)),
|
||||||
|
#("Angry Monkey Figurine", #(5, "B")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn can_swap_vintage_pirate_hat_for_model_ship_in_large_bottle_at_harbor_managers_office_test() {
|
||||||
|
let assert True =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Vintage Pirate Hat", #(7, "E")),
|
||||||
|
#("Harbor Managers Office", #("A", 8)),
|
||||||
|
#("Model Ship in Large Bottle", #(8, "A")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn can_swap_vintage_pirate_hat_for_antique_glass_fishnet_float_at_harbor_managers_office_test() {
|
||||||
|
let assert True =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Vintage Pirate Hat", #(7, "E")),
|
||||||
|
#("Harbor Managers Office", #("A", 8)),
|
||||||
|
#("Antique Glass Fishnet Float", #(3, "D")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cannot_swap_vintage_pirate_hat_for_carved_wooden_elephant_at_harbor_managers_office_test() {
|
||||||
|
let assert False =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Vintage Pirate Hat", #(7, "E")),
|
||||||
|
#("Harbor Managers Office", #("A", 8)),
|
||||||
|
#("Carved Wooden Elephant", #(8, "C")),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cannot_swap_vintage_pirate_hat_for_model_ship_in_large_bottle_at_old_schooner_test() {
|
||||||
|
let assert False =
|
||||||
|
tisbury_treasure_hunt.special_case_swap_possible(
|
||||||
|
#("Vintage Pirate Hat", #(7, "E")),
|
||||||
|
#("Old Schooner", #("A", 6)),
|
||||||
|
#("Model Ship in Large Bottle", #(8, "A")),
|
||||||
|
)
|
||||||
|
}
|
||||||
25
tracks-on-tracks-on-tracks/.exercism/config.json
Normal file
25
tracks-on-tracks-on-tracks/.exercism/config.json
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"lpil"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"src/tracks_on_tracks_on_tracks.gleam"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/tracks_on_tracks_on_tracks_test.gleam"
|
||||||
|
],
|
||||||
|
"exemplar": [
|
||||||
|
".meta/example.gleam"
|
||||||
|
],
|
||||||
|
"invalidator": [
|
||||||
|
"gleam.toml",
|
||||||
|
"manifest.toml"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"forked_from": [
|
||||||
|
"fsharp/tracks-on-tracks-on-tracks",
|
||||||
|
"elm/tracks-on-tracks-on-tracks"
|
||||||
|
],
|
||||||
|
"blurb": "Learn abouts lists by keeping track of the programming languages you want to learn."
|
||||||
|
}
|
||||||
1
tracks-on-tracks-on-tracks/.exercism/metadata.json
Normal file
1
tracks-on-tracks-on-tracks/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"track":"gleam","exercise":"tracks-on-tracks-on-tracks","id":"45633801cbc44ee598fd630722a10e7e","url":"https://exercism.org/tracks/gleam/exercises/tracks-on-tracks-on-tracks","handle":"fw353qwgs","is_requester":true,"auto_approve":false}
|
||||||
4
tracks-on-tracks-on-tracks/.gitignore
vendored
Normal file
4
tracks-on-tracks-on-tracks/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
*.beam
|
||||||
|
*.ez
|
||||||
|
build
|
||||||
|
erl_crash.dump
|
||||||
32
tracks-on-tracks-on-tracks/HELP.md
Normal file
32
tracks-on-tracks-on-tracks/HELP.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
To run the tests, run the command `gleam test` from within the exercise directory.
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit src/tracks_on_tracks_on_tracks.gleam` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Gleam track's documentation](https://exercism.org/docs/tracks/gleam)
|
||||||
|
- The [Gleam track's programming category on the forum](https://forum.exercism.org/c/programming/gleam)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
To get help if you're having trouble, you can use one of the following resources:
|
||||||
|
|
||||||
|
- [gleam.run](https://gleam.run/documentation/) is the gleam official documentation.
|
||||||
|
- [Discord](https://discord.gg/Fm8Pwmy) is the discord channel.
|
||||||
|
- [StackOverflow](https://stackoverflow.com/questions/tagged/gleam) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||||
28
tracks-on-tracks-on-tracks/HINTS.md
Normal file
28
tracks-on-tracks-on-tracks/HINTS.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# Hints
|
||||||
|
|
||||||
|
## 1. Create a new list
|
||||||
|
|
||||||
|
- An empty list can be created using the `[]` syntax.
|
||||||
|
|
||||||
|
## 2. Define an existing list
|
||||||
|
|
||||||
|
- Lists with multiple elements can be created using the `["a", "b", "c"]` syntax.
|
||||||
|
|
||||||
|
## 3. Add a new language to a list
|
||||||
|
|
||||||
|
- The spread syntax (`[x, ..list]`) can be used to add an element to the beginning of a list.
|
||||||
|
|
||||||
|
## 4. Count the languages in the list
|
||||||
|
|
||||||
|
- There is a function in the `gleam/list` module to [count the elements in a list][length].
|
||||||
|
|
||||||
|
## 5. Reverse the list
|
||||||
|
|
||||||
|
- There is a function in the `gleam/list` module to [reverse a list][reverse].
|
||||||
|
|
||||||
|
## 6. Check if list is exciting
|
||||||
|
|
||||||
|
- You can use pattern matching using the list and list spread patterns to match on specific list structures.
|
||||||
|
|
||||||
|
[reverse]: https://hexdocs.pm/gleam_stdlib/gleam/list.html#reverse
|
||||||
|
[length]: https://hexdocs.pm/gleam_stdlib/gleam/list.html#length
|
||||||
141
tracks-on-tracks-on-tracks/README.md
Normal file
141
tracks-on-tracks-on-tracks/README.md
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
# Tracks on Tracks on Tracks
|
||||||
|
|
||||||
|
Welcome to Tracks on Tracks on Tracks on Exercism's Gleam Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
## Lists
|
||||||
|
|
||||||
|
A list in Gleam is an immutable collection of zero or more values. The values in a list must all have the same type. As lists are immutable, once a list has been constructed, its value can never change. Any functions/operators that appear to modify a list (such as adding an element), will actually return a new list.
|
||||||
|
|
||||||
|
Lists can be defined as follows:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
let empty = []
|
||||||
|
let singleValue = [5]
|
||||||
|
let threeValues = ["a", "b", "c"]
|
||||||
|
```
|
||||||
|
|
||||||
|
The most common way to add an element to a list is through the spread syntax:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
let two_to_four = [2, 3, 4]
|
||||||
|
let one_to_four = [1, ..two_to_four]
|
||||||
|
// -> [1, 2, 3, 4]
|
||||||
|
```
|
||||||
|
|
||||||
|
The [`gleam/list`](https://hexdocs.pm/gleam_stdlib/gleam/list.html) module in the Gleam standard library contains many useful functions for working with lists. This module is very commonly used in Gleam code so it is good to be familiar with it.
|
||||||
|
|
||||||
|
Lists patterns can be used in case expressions to match on lists and assign contained values to variables:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pub fn describe(list: List(String)) -> String {
|
||||||
|
case list {
|
||||||
|
[] -> "Empty list"
|
||||||
|
[x] -> "List with one item: " <> x
|
||||||
|
[x, y] -> "List with two items: " <> x <> " and " <> y
|
||||||
|
_ -> "List with three or more items"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
As well as matching on exact length lists, the spread syntax can be used to match on lists of at-least a certain length:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pub fn describe(list: List(String)) -> String {
|
||||||
|
case list {
|
||||||
|
[_, _, ..] -> "List with at least two items"
|
||||||
|
[_] -> "List with one item"
|
||||||
|
[] -> "Empty list"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The spread syntax can also be used to assign the rest of the list to a variable:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
pub fn remove_first_item(list: List(String)) -> List(String) {
|
||||||
|
case list {
|
||||||
|
// Return the list without the first item
|
||||||
|
[_, ..rest] -> rest
|
||||||
|
|
||||||
|
// There's no first item to remove, return an empty list
|
||||||
|
_ -> []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Case expressions should have a pattern for every possible value of the type being matched on, so a final discard pattern (`_`) is often used to handle any remaining values.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
In this exercise you'll be writing code to keep track of a list of programming languages you want to learn on Exercism.
|
||||||
|
|
||||||
|
You have six tasks, which will all involve dealing with lists.
|
||||||
|
|
||||||
|
## 1. Create a new list
|
||||||
|
|
||||||
|
To keep track of the languages you want to learn, you'll need to create a new list. Define the `new_list` function that returns a new, empty list.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
new_list()
|
||||||
|
// -> []
|
||||||
|
```
|
||||||
|
|
||||||
|
## 2. Define an existing list
|
||||||
|
|
||||||
|
Currently, you have a piece of paper listing the languages you want to learn: Gleam, Go, and TypeScript. Define the `existing_list` function to return this list.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
existing_list()
|
||||||
|
// -> ["Gleam", "Go", "TypeScript"]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3. Add a new language to a list
|
||||||
|
|
||||||
|
As you explore Exercism and find more interesting languages, you want to add them to your list. Implement the `add_language` function to add a new language to the beginning of your list.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
add_language(["OCaml", "Elixir"], "Scheme")
|
||||||
|
// -> ["Scheme", "OCaml", "Elixir"]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4. Count the languages in the list
|
||||||
|
|
||||||
|
Counting the languages one-by-one is inconvenient. Implement the `count_languages` function to count the number of languages on your list.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
count_languages(["jq", "Elm", "Rust", "Kotlin"])
|
||||||
|
// -> 4
|
||||||
|
```
|
||||||
|
|
||||||
|
## 5. Reverse the list
|
||||||
|
|
||||||
|
At some point, you realize that your list is actually ordered backwards! Implement the `reverse_list` function to reverse your list.
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
reverse_list(["Python", "Julia", "Idris", "COBOL"])
|
||||||
|
// -> ["COBOL", "Idris", "Julia", "Python"]
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6. Check if list is exciting
|
||||||
|
|
||||||
|
While you love all languages, Gleam has a special place in your heart. As such, you're really excited about a list of languages if:
|
||||||
|
|
||||||
|
- The first on the list is Gleam.
|
||||||
|
- The second item on the list is Gleam and the list contain either two or three languages.
|
||||||
|
|
||||||
|
Implement the `exciting_list` function to check if a list of languages is exciting:
|
||||||
|
|
||||||
|
```gleam
|
||||||
|
exciting_list(["Lua", "Gleam"])
|
||||||
|
// -> True
|
||||||
|
```
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @lpil
|
||||||
14
tracks-on-tracks-on-tracks/gleam.toml
Normal file
14
tracks-on-tracks-on-tracks/gleam.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name = "tracks_on_tracks_on_tracks"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
gleam_otp = "~> 0.7 or ~> 1.0"
|
||||||
|
gleam_stdlib = ">= 0.54.0 or ~> 1.0"
|
||||||
|
simplifile = "~> 1.0"
|
||||||
|
gleam_erlang = ">= 0.25.0 and < 1.0.0"
|
||||||
|
gleam_yielder = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_regexp = ">= 1.1.0 and < 2.0.0"
|
||||||
|
gleam_deque = ">= 1.0.0 and < 2.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
exercism_test_runner = "~> 1.9"
|
||||||
31
tracks-on-tracks-on-tracks/manifest.toml
Normal file
31
tracks-on-tracks-on-tracks/manifest.toml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# This file was generated by Gleam
|
||||||
|
# You typically do not need to edit this file
|
||||||
|
|
||||||
|
packages = [
|
||||||
|
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
|
||||||
|
{ name = "exercism_test_runner", version = "1.9.0", build_tools = ["gleam"], requirements = ["argv", "gap", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_json", "gleam_stdlib", "simplifile"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "0B17BB25F2FF1E60266467C24FE0CA04005410306AA05E9A4B41B1852D72865C" },
|
||||||
|
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
|
||||||
|
{ name = "gap", version = "1.1.3", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "6EF5E3B523FDFBC317E9EA28D5163EE04744A97C007106F90207569789612291" },
|
||||||
|
{ name = "glance", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "E155BA1A787FD11827048355021C0390D2FE9A518485526F631A9D472858CC6D" },
|
||||||
|
{ name = "gleam_community_ansi", version = "1.4.3", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_regexp", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8A62AE9CC6EA65BEA630D95016D6C07E4F9973565FA3D0DE68DC4200D8E0DD27" },
|
||||||
|
{ name = "gleam_community_colour", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "FDD6AC62C6EC8506C005949A4FCEF032038191D5EAAEC3C9A203CD53AE956ACA" },
|
||||||
|
{ name = "gleam_deque", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_deque", source = "hex", outer_checksum = "64D77068931338CF0D0CB5D37522C3E3CCA7CB7D6C5BACB41648B519CC0133C7" },
|
||||||
|
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
|
||||||
|
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
|
||||||
|
{ name = "gleam_otp", version = "0.16.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "50DA1539FC8E8FA09924EB36A67A2BBB0AD6B27BCDED5A7EF627057CF69D035E" },
|
||||||
|
{ name = "gleam_regexp", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "7F5E0C0BBEB3C58E57C9CB05FA9002F970C85AD4A63BA1E55CBCB35C15809179" },
|
||||||
|
{ name = "gleam_stdlib", version = "0.55.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "32D8F4AE03771516950047813A9E359249BD9FBA5C33463FDB7B953D6F8E896B" },
|
||||||
|
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
|
||||||
|
{ name = "glexer", version = "2.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "5C235CBDF4DA5203AD5EAB1D6D8B456ED8162C5424FE2309CFFB7EF438B7C269" },
|
||||||
|
{ name = "simplifile", version = "1.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "1D5DFA3A2F9319EC85825F6ED88B8E449F381B0D55A62F5E61424E748E7DDEB0" },
|
||||||
|
]
|
||||||
|
|
||||||
|
[requirements]
|
||||||
|
exercism_test_runner = { version = "~> 1.9" }
|
||||||
|
gleam_deque = { version = ">= 1.0.0 and < 2.0.0" }
|
||||||
|
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
|
||||||
|
gleam_otp = { version = "~> 0.7 or ~> 1.0" }
|
||||||
|
gleam_regexp = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
gleam_stdlib = { version = ">= 0.54.0 or ~> 1.0" }
|
||||||
|
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
|
||||||
|
simplifile = { version = "~> 1.0" }
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import gleam/list
|
||||||
|
|
||||||
|
pub fn new_list() -> List(String) {
|
||||||
|
[]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn existing_list() -> List(String) {
|
||||||
|
["Gleam", "Go", "TypeScript"]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_language(languages: List(String), language: String) -> List(String) {
|
||||||
|
[language, ..languages]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count_languages(languages: List(String)) -> Int {
|
||||||
|
list.length(languages)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reverse_list(languages: List(String)) -> List(String) {
|
||||||
|
list.reverse(languages)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn exciting_list(languages: List(String)) -> Bool {
|
||||||
|
// - The first on the list is Gleam.
|
||||||
|
// - The second item on the list is Gleam and the list contain either two or three languages.
|
||||||
|
case languages {
|
||||||
|
["Gleam", ..] -> True
|
||||||
|
[_, "Gleam"] -> True
|
||||||
|
[_, "Gleam", _] -> True
|
||||||
|
_ -> False
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,130 @@
|
|||||||
|
import exercism/should
|
||||||
|
import exercism/test_runner
|
||||||
|
import tracks_on_tracks_on_tracks
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
test_runner.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.new_list()
|
||||||
|
|> should.equal([])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn existing_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.existing_list()
|
||||||
|
|> should.equal(["Gleam", "Go", "TypeScript"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_language_to_new_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.add_language([], "Elixir")
|
||||||
|
|> should.equal(["Elixir"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_language_to_existing_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.add_language(["Gleam", "Go", "TypeScript"], "Lua")
|
||||||
|
|> should.equal(["Lua", "Gleam", "Go", "TypeScript"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_language_to_custom_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.add_language(["Scheme"], "Racket")
|
||||||
|
|> should.equal(["Racket", "Scheme"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count_languages_on_new_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.count_languages(
|
||||||
|
tracks_on_tracks_on_tracks.new_list(),
|
||||||
|
)
|
||||||
|
|> should.equal(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count_languages_on_existing_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.count_languages(
|
||||||
|
tracks_on_tracks_on_tracks.existing_list(),
|
||||||
|
)
|
||||||
|
|> should.equal(3)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn count_languages_on_custom_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.count_languages(["Python", "JavaScript"])
|
||||||
|
|> should.equal(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reverse_order_of_new_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.reverse_list(tracks_on_tracks_on_tracks.new_list())
|
||||||
|
|> should.equal([])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reverse_order_of_existing_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.reverse_list(
|
||||||
|
tracks_on_tracks_on_tracks.existing_list(),
|
||||||
|
)
|
||||||
|
|> should.equal(["TypeScript", "Go", "Gleam"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reverse_order_of_custom_list_test() {
|
||||||
|
tracks_on_tracks_on_tracks.reverse_list(["Kotlin", "Java", "Scala", "Clojure"])
|
||||||
|
|> should.equal(["Clojure", "Scala", "Java", "Kotlin"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn empty_list_is_not_exciting_test() {
|
||||||
|
let assert False = tracks_on_tracks_on_tracks.exciting_list([])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn singleton_list_with_gleam_is_exciting_test() {
|
||||||
|
let assert True = tracks_on_tracks_on_tracks.exciting_list(["Gleam"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn singleton_list_without_gleam_is_not_exciting_test() {
|
||||||
|
let assert False = tracks_on_tracks_on_tracks.exciting_list(["Go"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn two_item_list_with_gleam_as_first_item_is_exciting_test() {
|
||||||
|
let assert True =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Gleam", "Clojure"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn two_item_list_with_gleam_as_second_item_is_exciting_test() {
|
||||||
|
let assert True = tracks_on_tracks_on_tracks.exciting_list(["Raku", "Gleam"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn two_item_list_without_gleam_is_not_exciting_test() {
|
||||||
|
let assert False = tracks_on_tracks_on_tracks.exciting_list(["Python", "Go"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn three_item_list_with_gleam_as_first_item_is_exciting_test() {
|
||||||
|
let assert True =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Gleam", "Lisp", "Clojure"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn three_item_list_with_gleam_as_second_item_is_exciting_test() {
|
||||||
|
let assert True =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Java", "Gleam", "C#"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn three_item_list_with_gleam_as_third_item_is_not_exciting_test() {
|
||||||
|
let assert False =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Julia", "Assembly", "Gleam"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn four_item_list_with_gleam_as_first_item_is_exciting_test() {
|
||||||
|
let assert True =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Gleam", "C", "C++", "C#"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn four_item_list_with_gleam_as_second_item_is_not_exciting_test() {
|
||||||
|
let assert False =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Elm", "Gleam", "C#", "Scheme"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn four_item_list_with_gleam_as_third_item_is_not_exciting_test() {
|
||||||
|
let assert False =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list(["Delphi", "D", "Gleam", "Prolog"])
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn four_item_list_with_gleam_as_fourth_item_is_not_exciting_test() {
|
||||||
|
let assert False =
|
||||||
|
tracks_on_tracks_on_tracks.exciting_list([
|
||||||
|
"Julia", "Assembly", "Crystal", "Gleam",
|
||||||
|
])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user