Skip to content

Commit 7dd4558

Browse files
committed
Added wordcount
1 parent 2e501f5 commit 7dd4558

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

gleam/gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ version = "1.0.0"
1515
[dependencies]
1616
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
1717
argv = ">= 1.0.2 and < 2.0.0"
18-
gleam_erlang = ">= 0.34.0 and < 1.0.0"
18+
gleam_regexp = ">= 1.1.1 and < 2.0.0"
1919

2020
[dev-dependencies]
2121
gleeunit = ">= 1.0.0 and < 2.0.0"

gleam/manifest.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
packages = [
55
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6-
{ name = "gleam_erlang", version = "0.34.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "0C38F2A128BAA0CEF17C3000BD2097EB80634E239CE31A86400C4416A5D0FDCC" },
6+
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
77
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
88
{ name = "gleeunit", version = "1.3.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A7DD6C07B7DA49A6E28796058AA89E651D233B357D5607006D70619CD89DAAAB" },
99
]
1010

1111
[requirements]
1212
argv = { version = ">= 1.0.2 and < 2.0.0" }
13-
gleam_erlang = { version = ">= 0.34.0 and < 1.0.0" }
13+
gleam_regexp = { version = ">= 1.1.1 and < 2.0.0" }
1414
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
1515
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }

gleam/src/wordcount.gleam

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import argv
2+
import gleam/dict
3+
import gleam/int
4+
import gleam/io
5+
import gleam/list
6+
import gleam/option.{type Option, None, Some}
7+
import gleam/regexp
8+
import gleam/string
9+
10+
pub fn main() {
11+
//////////////////////////////////////////////////////////////
12+
// Warning constantly stated if you don't use the Option type.
13+
// This is implemented to not get bombarded by Gleam warning
14+
// messages when running some of the examples. This is not
15+
// apart of the example whatsoever.
16+
let m: List(Option(Int)) = [Some(1), None]
17+
list.each(m, fn(x) {
18+
case x {
19+
Some(i) -> i
20+
None -> 1
21+
}
22+
})
23+
//////////////////////////////////////////////////////////////
24+
25+
let assert Ok(re) = regexp.from_string("[a-z\\']+")
26+
27+
argv.load().arguments
28+
|> list.map( fn(line) {
29+
line
30+
|> string.lowercase
31+
|> regexp.scan(with: re)
32+
})
33+
|> list.flatten
34+
|> list.map( fn (word) { word.content } )
35+
|> list.fold(dict.from_list([]), fn(freq, word) {
36+
dict.upsert(freq, word, fn(n) {
37+
case n {
38+
Some(i) -> i + 1
39+
None -> 1
40+
}
41+
})
42+
})
43+
|> dict.to_list
44+
|> list.sort( fn(s1, s2) { string.compare(s1.0, s2.0) } )
45+
|> list.each( fn(item) { io.println(item.0 <> " " <> int.to_string(item.1)) })
46+
}

gleam/test.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Set-Location "$PSScriptRoot"
1414

1515
# Download dependencies if needed.
1616
gleam add argv
17-
gleam add gleam_erlang
17+
gleam add gleam_regexp@1
1818

1919
$Error.clear()
2020
gleam run -m anagrams_library rats |
@@ -45,6 +45,9 @@ gleam run -m type_aliases &&
4545
gleam run -m type_enum &&
4646
gleam run -m type_record &&
4747
gleam run -m unqualified_imports &&
48+
gleam run -m wordcount (Get-Content "$PSScriptRoot\..\test\wordcount_ascii_input") |
49+
Compare-Object (Get-Content "$PSScriptRoot\..\test\wordcount_ascii_expected") |
50+
Assert-MatchTests &&
4851
ForEach-Object 'foo';
4952

5053
if ($Error -or !$?) {

0 commit comments

Comments
 (0)