Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-1256: Add gleam support to grep challenge #77

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiled_starters/gleam/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions compiled_starters/gleam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
39 changes: 39 additions & 0 deletions compiled_starters/gleam/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/grep.png)

This is a starting point for Gleam solutions to the
["Build Your Own grep" Challenge](https://app.codecrafters.io/courses/grep/overview).

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression)
(Regexes, for short) are patterns used to match character combinations in
strings. [`grep`](https://en.wikipedia.org/wiki/Grep) is a CLI tool for
searching using Regexes.

In this challenge you'll build your own implementation of `grep`. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `grep` implementation is in `src/grep.gleam`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `gleam (1.0)` installed locally
1. Run `./your_grep.sh` to run your program, which is implemented in
`src/grep.gleam`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions compiled_starters/gleam/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Gleam version used to run your code
# on Codecrafters.
#
# Available versions: gleam-1.0
language_pack: gleam-1.0
21 changes: 21 additions & 0 deletions compiled_starters/gleam/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name = "grep"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "username", repo = "project" }
# links = [{ title = "Website", href = "https://gleam.run" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
argv = ">= 1.0.2 and < 2.0.0"
gleam_erlang = "~> 0.25"
gleam_stdlib = "~> 0.34 or ~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
15 changes: 15 additions & 0 deletions compiled_starters/gleam/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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 = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
]

[requirements]
argv = { version = ">= 1.0.2 and < 2.0.0" }
gleam_erlang = { version = "~> 0.25" }
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
39 changes: 39 additions & 0 deletions compiled_starters/gleam/src/grep.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import argv
import gleam/erlang
import gleam/io
import gleam/string

pub fn main() {
// You can use print statements as follows for debugging, they'll be visible when running tests.
io.println("Logs from your program will appear here!")
ryan-gang marked this conversation as resolved.
Show resolved Hide resolved

let args = argv.load().arguments
let assert Ok(input_line) = erlang.get_line("")

// Uncomment this to pass the first stage
// case args {
// ["-E", pattern, ..] -> {
// case match_pattern(input_line, pattern) {
ryan-gang marked this conversation as resolved.
Show resolved Hide resolved
// True -> exit(0)
// False -> exit(1)
// }
// }
// _ -> {
// io.println("Expected first argument to be '-E'")
// exit(1)
// }
// }
}

fn match_pattern(input_line: String, pattern: String) -> Bool {
case string.length(pattern) {
1 -> string.contains(input_line, pattern)
_ -> {
io.println("Unhandled pattern: " <> pattern)
False
}
}
}

@external(erlang, "erlang", "halt")
pub fn exit(code: Int) -> Int
8 changes: 8 additions & 0 deletions compiled_starters/gleam/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
exec gleam run -- "$@"
5 changes: 5 additions & 0 deletions dockerfiles/gleam-1.0.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM ghcr.io/gleam-lang/gleam:v1.0.0-erlang-alpine

# Pre-compile steps
RUN printf "cd \${CODECRAFTERS_SUBMISSION_DIR} && gleam build" > /codecrafters-precompile.sh
RUN chmod +x /codecrafters-precompile.sh
1 change: 1 addition & 0 deletions solutions/gleam/01-cq2/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions solutions/gleam/01-cq2/code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
39 changes: 39 additions & 0 deletions solutions/gleam/01-cq2/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/grep.png)

This is a starting point for Gleam solutions to the
["Build Your Own grep" Challenge](https://app.codecrafters.io/courses/grep/overview).

[Regular expressions](https://en.wikipedia.org/wiki/Regular_expression)
(Regexes, for short) are patterns used to match character combinations in
strings. [`grep`](https://en.wikipedia.org/wiki/Grep) is a CLI tool for
searching using Regexes.

In this challenge you'll build your own implementation of `grep`. Along the way
we'll learn about Regex syntax, how parsers/lexers work, and how regular
expressions are evaluated.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your `grep` implementation is in `src/grep.gleam`. Study and
uncomment the relevant code, and push your changes to pass the first stage:

```sh
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `gleam (1.0)` installed locally
1. Run `./your_grep.sh` to run your program, which is implemented in
`src/grep.gleam`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
11 changes: 11 additions & 0 deletions solutions/gleam/01-cq2/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the Gleam version used to run your code
# on Codecrafters.
#
# Available versions: gleam-1.0
language_pack: gleam-1.0
21 changes: 21 additions & 0 deletions solutions/gleam/01-cq2/code/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name = "grep"
version = "1.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "username", repo = "project" }
# links = [{ title = "Website", href = "https://gleam.run" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.

[dependencies]
argv = ">= 1.0.2 and < 2.0.0"
gleam_erlang = "~> 0.25"
gleam_stdlib = "~> 0.34 or ~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
15 changes: 15 additions & 0 deletions solutions/gleam/01-cq2/code/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 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 = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_stdlib", version = "0.38.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "663CF11861179AF415A625307447775C09404E752FF99A24E2057C835319F1BE" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
]

[requirements]
argv = { version = ">= 1.0.2 and < 2.0.0" }
gleam_erlang = { version = "~> 0.25" }
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
35 changes: 35 additions & 0 deletions solutions/gleam/01-cq2/code/src/grep.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import argv
import gleam/erlang
import gleam/io
import gleam/string

pub fn main() {
let args = argv.load().arguments
let assert Ok(input_line) = erlang.get_line("")

case args {
["-E", pattern, ..] -> {
case match_pattern(input_line, pattern) {
True -> exit(0)
False -> exit(1)
}
}
_ -> {
io.println("Expected first argument to be '-E'")
exit(1)
}
}
}

fn match_pattern(input_line: String, pattern: String) -> Bool {
case string.length(pattern) {
1 -> string.contains(input_line, pattern)
_ -> {
io.println("Unhandled pattern: " <> pattern)
False
}
}
}

@external(erlang, "erlang", "halt")
pub fn exit(code: Int) -> Int
8 changes: 8 additions & 0 deletions solutions/gleam/01-cq2/code/your_grep.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# DON'T EDIT THIS!
#
# CodeCrafters uses this file to test your code. Don't make any changes here!
#
# DON'T EDIT THIS!
exec gleam run -- "$@"
52 changes: 52 additions & 0 deletions solutions/gleam/01-cq2/diff/src/grep.gleam.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@@ -1,39 +1,35 @@
import argv
import gleam/erlang
import gleam/io
import gleam/string

pub fn main() {
- // You can use print statements as follows for debugging, they'll be visible when running tests.
- io.println("Logs from your program will appear here!")
-
let args = argv.load().arguments
let assert Ok(input_line) = erlang.get_line("")

- // Uncomment this to pass the first stage
- // case args {
- // ["-E", pattern, ..] -> {
- // case match_pattern(input_line, pattern) {
- // True -> exit(0)
- // False -> exit(1)
- // }
- // }
- // _ -> {
- // io.println("Expected first argument to be '-E'")
- // exit(1)
- // }
- // }
+ case args {
+ ["-E", pattern, ..] -> {
+ case match_pattern(input_line, pattern) {
+ True -> exit(0)
+ False -> exit(1)
+ }
+ }
+ _ -> {
+ io.println("Expected first argument to be '-E'")
+ exit(1)
+ }
+ }
}

fn match_pattern(input_line: String, pattern: String) -> Bool {
case string.length(pattern) {
1 -> string.contains(input_line, pattern)
_ -> {
io.println("Unhandled pattern: " <> pattern)
False
}
}
}

@external(erlang, "erlang", "halt")
pub fn exit(code: Int) -> Int
27 changes: 27 additions & 0 deletions solutions/gleam/01-cq2/explanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
The entry point for your grep implementation is in `src/grep.gleam`.

Study and uncomment the relevant code:

```gleam
// Uncomment this to pass the first stage
case args {
["-E", pattern, ..] -> {
case match_pattern(input_line, pattern) {
True -> exit(0)
False -> exit(1)
}
}
_ -> {
io.println("Expected first argument to be '-E'")
exit(1)
}
}
```

Push your changes to pass the first stage:

```
git add .
git commit -m "pass 1st stage" # any msg
git push origin master
```
Loading
Loading