a gleam implementation of a CS assignment originally written in cpp
1# Lab 6 - Gleamified
2
3[](https://hex.pm/packages/bible_search)
4[](https://hexdocs.pm/bible_search/)
5
6```gleam
7pub fn bible_scan(
8 reference reference: Reference,
9 bible bible: String,
10) -> ScanResult {
11 let inital_state =
12 ScanState(
13 phase: Book,
14 ref: reference,
15 found_book: False,
16 found_chapter: False,
17 verse_text: Error("Verse not found"),
18 )
19
20 let bible_lines = string.split(bible, "\n")
21
22 let result =
23 list.fold(bible_lines, inital_state, fn(state, line) -> ScanState {
24 case state.phase {
25 Done -> state
26 Book ->
27 case line {
28 "THE BOOK OF " <> book ->
29 case book == state.ref.book {
30 True -> ScanState(..state, phase: Chapter, found_book: True)
31 False -> state
32 }
33 _ -> state
34 }
35 Chapter ->
36 case line {
37 "CHAPTER " <> chapter ->
38 case result.unwrap(int.parse(chapter), 0) == state.ref.chapter {
39 True -> ScanState(..state, phase: Verse, found_chapter: True)
40 False -> state
41 }
42 _ -> state
43 }
44 Verse ->
45 fn() {
46 let parts = string.split(line, " ")
47 case parts {
48 [first, ..rest] ->
49 case result.unwrap(int.parse(first), 0) == state.ref.verse {
50 True ->
51 ScanState(
52 ..state,
53 phase: Done,
54 verse_text: Ok(string.join(rest, " ")),
55 )
56 False -> state
57 }
58 _ -> state
59 }
60 }()
61 }
62 })
63
64 ScanResult(ref: reference, verse: result.verse_text)
65}
66```
67
68For this lab the program must parse `OT.txt` (format below) to find specific references and produce scoped errors if the reference isn't found.
69
70```text
71THE BOOK OF GENESIS
72
73CHAPTER 1
741 In the beginning God created the heaven and the earth.
752 And the earth was without form, and void; and darkness [was] upon the face of the deep. And the Spirit of God moved upon the face of the waters.
763 And God said, Let there be light: and there was light.
774 And God saw the light, that [it was] good: and God divided the light from the darkness.
78
79THE BOOK OF PSALMS
80
81PSALM 1
821 Blessed [is] the man that walketh not in the counsel of the ungodly, nor standeth in the way of sinners, nor sitteth in the seat of the scornful.
832 But his delight [is] in the law of the LORD; and in his law doth he meditate day and night.
843 And he shall be like a tree planted by the rivers of water, that bringeth forth his fruit in his season; his leaf also shall not wither; and whatsoever he doeth shall prosper.
85```
86
87## Development
88
89You need to have the [gleam runtime](https://gleam.run/getting-started/installing/) installed as well as erlang for the beam runtime.
90
91```sh
92gleam run
93gleam test # runs all the test functions in /tests postfixed with _test
94```
95
96<p align="center">
97 <img src="https://raw.githubusercontent.com/taciturnaxolotl/carriage/main/.github/images/line-break.svg" />
98</p>
99
100<p align="center">
101 <i><code>© 2025-present <a href="https://github.com/taciturnaxololt">Kieran Klukas</a></code></i>
102</p>
103
104<p align="center">
105 <a href="https://github.com/cu-cs1210/lab-6-kieranklukas/blob/main/LICENSE.md"><img src="https://img.shields.io/static/v1.svg?style=for-the-badge&label=License&message=MIT&logoColor=d9e0ee&colorA=363a4f&colorB=b7bdf8"/></a>
106</p>