Skip to content

Commit 20db5f4

Browse files
committed
feat: add gleam starter for sqlite course
1 parent b710fe2 commit 20db5f4

File tree

12 files changed

+131
-231
lines changed

12 files changed

+131
-231
lines changed

compiled_starters/gleam/gleam.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ version = "1.0.0"
1414

1515
[dependencies]
1616
gleam_stdlib = "~> 0.34 or ~> 1.0"
17-
gleam_erlang = "~> 0.25"
18-
gleam_otp = "~> 0.10"
19-
glisten = "~> 2.0"
17+
argv = ">= 1.0.2 and < 2.0.0"
18+
file_streams = ">= 0.4.2 and < 1.0.0"
2019

2120
[dev-dependencies]
2221
gleeunit = "~> 1.0"

compiled_starters/gleam/manifest.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
6-
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
7-
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
5+
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6+
{ name = "file_streams", version = "0.4.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "file_streams", source = "hex", outer_checksum = "86775D2F36B74C54C64CA9080ACA9B9CF4655759E8D3E87E1A2EA9B0FA8D0804" },
7+
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
88
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
9-
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" },
109
]
1110

1211
[requirements]
13-
gleam_erlang = { version = "~> 0.25" }
14-
gleam_otp = { version = "~> 0.10" }
12+
argv = { version = ">= 1.0.2 and < 2.0.0" }
13+
file_streams = { version = ">= 0.4.2 and < 1.0.0" }
1514
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
1615
gleeunit = { version = "~> 1.0" }
17-
glisten = { version = "~> 2.0"}
Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
1+
import argv
2+
import file_streams/read_stream
3+
import gleam/bit_array
4+
import gleam/int.{to_string}
15
import gleam/io
2-
import gleam/string
3-
import gleam/int
4-
import gleam/list
5-
import gleam/result
6+
import gleam/list.{each}
67

7-
// You can use print statements as follows for debugging, they'll be visible when running tests.
8-
io.println("Logs from your program will appear here!")
9-
10-
// Uncomment this block to pass the first stage
11-
// pub fn main() {
12-
// let args = io.argv()
13-
//
14-
// case args {
15-
// [_, database_file_path, command] -> {
16-
// case command {
17-
// ".dbinfo" -> {
18-
// io.open(database_file_path, io.Read)
19-
// |> result.then(fn(file) {
20-
// io.seek(file, 16)
21-
// |> result.then(fn(_) {
22-
// io.read_bytes(file, 2)
23-
// |> result.then(fn(bytes) {
24-
// let page_size = bytes
25-
// |> list.reverse
26-
// |> int.from_bytes_be
27-
// io.println(string.concat(["database page size: ", int.to_string(page_size)]))
28-
// |> result.map(fn(_) { io.close(file) })
29-
// })
30-
// })
31-
// })
32-
// |> result.unwrap(crash)
33-
// }
34-
// _ -> io.println(string.concat(["Invalid command: ", command]))
35-
// }
36-
// }
37-
// _ -> io.println("Usage: gleam run database_file_path command")
38-
// }
39-
// }
8+
pub fn main() {
9+
// You can use print statements as follows for debugging, they'll be visible when running tests.
10+
io.println("Logs from your program will appear here!")
11+
let args = argv.load().arguments
12+
// Uncomment this to pass the first stage
13+
// case args {
14+
// [database_file_path, ".dbinfo", ..] -> {
15+
// let assert Ok(rs) = read_stream.open(database_file_path)
16+
// let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16)
17+
// let assert Ok(int_value) = read_stream.read_int16_be(rs)
18+
//
19+
// io.print("database page size: ")
20+
// io.println(to_string(int_value))
21+
// }
22+
// _ -> {
23+
// io.println("No match")
24+
// }
25+
// }
26+
}

solutions/gleam/01-init/code/gleam.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ version = "1.0.0"
1414

1515
[dependencies]
1616
gleam_stdlib = "~> 0.34 or ~> 1.0"
17-
gleam_erlang = "~> 0.25"
18-
gleam_otp = "~> 0.10"
19-
glisten = "~> 2.0"
17+
argv = ">= 1.0.2 and < 2.0.0"
18+
file_streams = ">= 0.4.2 and < 1.0.0"
2019

2120
[dev-dependencies]
2221
gleeunit = "~> 1.0"

solutions/gleam/01-init/code/manifest.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
6-
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
7-
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
5+
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6+
{ name = "file_streams", version = "0.4.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "file_streams", source = "hex", outer_checksum = "86775D2F36B74C54C64CA9080ACA9B9CF4655759E8D3E87E1A2EA9B0FA8D0804" },
7+
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
88
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
9-
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" },
109
]
1110

1211
[requirements]
13-
gleam_erlang = { version = "~> 0.25" }
14-
gleam_otp = { version = "~> 0.10" }
12+
argv = { version = ">= 1.0.2 and < 2.0.0" }
13+
file_streams = { version = ">= 0.4.2 and < 1.0.0" }
1514
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
1615
gleeunit = { version = "~> 1.0" }
17-
glisten = { version = "~> 2.0"}
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
1+
import argv
2+
import file_streams/read_stream
3+
import gleam/bit_array
4+
import gleam/int.{to_string}
15
import gleam/io
2-
import gleam/string
3-
import gleam/int
4-
import gleam/list
5-
import gleam/result
6+
import gleam/list.{each}
67

78
pub fn main() {
8-
let args = io.argv()
9-
9+
let args = argv.load().arguments
1010
case args {
11-
[_, database_file_path, command] -> {
12-
case command {
13-
".dbinfo" -> {
14-
io.open(database_file_path, io.Read)
15-
|> result.then(fn(file) {
16-
io.seek(file, 16)
17-
|> result.then(fn(_) {
18-
io.read_bytes(file, 2)
19-
|> result.then(fn(bytes) {
20-
let page_size = bytes
21-
|> list.reverse
22-
|> int.from_bytes_be
23-
io.println(string.concat(["database page size: ", int.to_string(page_size)]))
24-
|> result.map(fn(_) { io.close(file) })
25-
})
26-
})
27-
})
28-
|> result.unwrap(crash)
29-
}
30-
_ -> io.println(string.concat(["Invalid command: ", command]))
31-
}
11+
[database_file_path, ".dbinfo", ..] -> {
12+
let assert Ok(rs) = read_stream.open(database_file_path)
13+
let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16)
14+
let assert Ok(int_value) = read_stream.read_int16_be(rs)
15+
16+
io.print("database page size: ")
17+
io.println(to_string(int_value))
18+
}
19+
_ -> {
20+
io.println("No match")
3221
}
33-
_ -> io.println("Usage: gleam run database_file_path command")
3422
}
35-
}
23+
}
Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,40 @@
1-
@@ -1,39 +1,35 @@
1+
@@ -1,26 +1,23 @@
2+
import argv
3+
import file_streams/read_stream
4+
import gleam/bit_array
5+
import gleam/int.{to_string}
26
import gleam/io
3-
import gleam/string
4-
import gleam/int
5-
import gleam/list
6-
import gleam/result
7+
import gleam/list.{each}
78

8-
-// You can use print statements as follows for debugging, they'll be visible when running tests.
9-
-io.println("Logs from your program will appear here!")
10-
+pub fn main() {
11-
+ let args = io.argv()
12-
13-
-// Uncomment this block to pass the first stage
14-
-// pub fn main() {
15-
-// let args = io.argv()
16-
-//
17-
-// case args {
18-
-// [_, database_file_path, command] -> {
19-
-// case command {
20-
-// ".dbinfo" -> {
21-
-// io.open(database_file_path, io.Read)
22-
-// |> result.then(fn(file) {
23-
-// io.seek(file, 16)
24-
-// |> result.then(fn(_) {
25-
-// io.read_bytes(file, 2)
26-
-// |> result.then(fn(bytes) {
27-
-// let page_size = bytes
28-
-// |> list.reverse
29-
-// |> int.from_bytes_be
30-
-// io.println(string.concat(["database page size: ", int.to_string(page_size)]))
31-
-// |> result.map(fn(_) { io.close(file) })
32-
-// })
33-
-// })
34-
-// })
35-
-// |> result.unwrap(crash)
36-
-// }
37-
-// _ -> io.println(string.concat(["Invalid command: ", command]))
38-
-// }
39-
-// }
40-
-// _ -> io.println("Usage: gleam run database_file_path command")
41-
-// }
42-
-// }
43-
\ No newline at end of file
9+
pub fn main() {
10+
- // You can use print statements as follows for debugging, they'll be visible when running tests.
11+
- io.println("Logs from your program will appear here!")
12+
let args = argv.load().arguments
13+
- // Uncomment this to pass the first stage
14+
- // case args {
15+
- // [database_file_path, ".dbinfo", ..] -> {
16+
- // let assert Ok(rs) = read_stream.open(database_file_path)
17+
- // let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16)
18+
- // let assert Ok(int_value) = read_stream.read_int16_be(rs)
19+
- //
20+
- // io.print("database page size: ")
21+
- // io.println(to_string(int_value))
22+
- // }
23+
- // _ -> {
24+
- // io.println("No match")
25+
- // }
26+
- // }
4427
+ case args {
45-
+ [_, database_file_path, command] -> {
46-
+ case command {
47-
+ ".dbinfo" -> {
48-
+ io.open(database_file_path, io.Read)
49-
+ |> result.then(fn(file) {
50-
+ io.seek(file, 16)
51-
+ |> result.then(fn(_) {
52-
+ io.read_bytes(file, 2)
53-
+ |> result.then(fn(bytes) {
54-
+ let page_size = bytes
55-
+ |> list.reverse
56-
+ |> int.from_bytes_be
57-
+ io.println(string.concat(["database page size: ", int.to_string(page_size)]))
58-
+ |> result.map(fn(_) { io.close(file) })
59-
+ })
60-
+ })
61-
+ })
62-
+ |> result.unwrap(crash)
63-
+ }
64-
+ _ -> io.println(string.concat(["Invalid command: ", command]))
65-
+ }
28+
+ [database_file_path, ".dbinfo", ..] -> {
29+
+ let assert Ok(rs) = read_stream.open(database_file_path)
30+
+ let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16)
31+
+ let assert Ok(int_value) = read_stream.read_int16_be(rs)
32+
+
33+
+ io.print("database page size: ")
34+
+ io.println(to_string(int_value))
35+
+ }
36+
+ _ -> {
37+
+ io.println("No match")
6638
+ }
67-
+ _ -> io.println("Usage: gleam run database_file_path command")
6839
+ }
69-
+}
70-
\ No newline at end of file
40+
}

solutions/gleam/01-init/explanation.md

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,18 @@ The entry point for your SQLite implementation is in `src/sqlite.gleam`.
33
Study and uncomment the relevant code:
44

55
```gleam
6-
// Uncomment this block to pass the first stage
7-
pub fn main() {
8-
let args = io.argv()
6+
// Uncomment this to pass the first stage
7+
case args {
8+
[database_file_path, ".dbinfo", ..] -> {
9+
let assert Ok(rs) = read_stream.open(database_file_path)
10+
let assert Ok(bytes) = read_stream.read_bytes_exact(rs, 16)
11+
let assert Ok(int_value) = read_stream.read_int16_be(rs)
912
10-
case args {
11-
[_, database_file_path, command] -> {
12-
case command {
13-
".dbinfo" -> {
14-
io.open(database_file_path, io.Read)
15-
|> result.then(fn(file) {
16-
io.seek(file, 16)
17-
|> result.then(fn(_) {
18-
io.read_bytes(file, 2)
19-
|> result.then(fn(bytes) {
20-
let page_size = bytes
21-
|> list.reverse
22-
|> int.from_bytes_be
23-
io.println(string.concat(["database page size: ", int.to_string(page_size)]))
24-
|> result.map(fn(_) { io.close(file) })
25-
})
26-
})
27-
})
28-
|> result.unwrap(crash)
29-
}
30-
_ -> io.println(string.concat(["Invalid command: ", command]))
31-
}
32-
}
33-
_ -> io.println("Usage: gleam run database_file_path command")
13+
io.print("database page size: ")
14+
io.println(to_string(int_value))
15+
}
16+
_ -> {
17+
io.println("No match")
3418
}
3519
}
3620
```

starter_templates/gleam/gleam.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ version = "1.0.0"
1414

1515
[dependencies]
1616
gleam_stdlib = "~> 0.34 or ~> 1.0"
17-
gleam_erlang = "~> 0.25"
18-
gleam_otp = "~> 0.10"
19-
glisten = "~> 2.0"
17+
argv = ">= 1.0.2 and < 2.0.0"
18+
file_streams = ">= 0.4.2 and < 1.0.0"
2019

2120
[dev-dependencies]
2221
gleeunit = "~> 1.0"

starter_templates/gleam/manifest.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
6-
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
7-
{ name = "gleam_stdlib", version = "0.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
5+
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6+
{ name = "file_streams", version = "0.4.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "file_streams", source = "hex", outer_checksum = "86775D2F36B74C54C64CA9080ACA9B9CF4655759E8D3E87E1A2EA9B0FA8D0804" },
7+
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
88
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
9-
{ name = "glisten", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "CF3A9383E9BA4A8CBAF2F7B799716290D02F2AC34E7A77556B49376B662B9314" },
109
]
1110

1211
[requirements]
13-
gleam_erlang = { version = "~> 0.25" }
14-
gleam_otp = { version = "~> 0.10" }
12+
argv = { version = ">= 1.0.2 and < 2.0.0" }
13+
file_streams = { version = ">= 0.4.2 and < 1.0.0" }
1514
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
1615
gleeunit = { version = "~> 1.0" }
17-
glisten = { version = "~> 2.0"}

0 commit comments

Comments
 (0)