Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
feat: Let's do a bit of a kata in gleam💎✨
Browse files Browse the repository at this point in the history
Co-authored-by: Ashish Sehra <ashish.sehra@armakuni.com>
Co-authored-by: Mark Bradley <mark.bradley@armkauni.com>
Co-authored-by: Tom Oram <tom.oram@armakuni.com>
Co-authored-by: Ryan Tiffany <ryan.tiffany@armakuni.com>
  • Loading branch information
5 people committed Feb 23, 2024
0 parents commit 77ba46a
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: test

on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: erlef/setup-beam@v1
with:
otp-version: "26.0.2"
gleam-version: "0.34.1"
rebar3-version: "3"
# elixir-version: "1.15.4"
- run: gleam deps download
- run: gleam test
- run: gleam format --check src test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
/build
erl_crash.dump
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ak_day_play_gleam

[![Package Version](https://img.shields.io/hexpm/v/ak_day_play_gleam)](https://hex.pm/packages/ak_day_play_gleam)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/ak_day_play_gleam/)

```sh
gleam add ak_day_play_gleam
```
```gleam
import ak_day_play_gleam
pub fn main() {
// TODO: An example of the project in use
}
```

Further documentation can be found at <https://hexdocs.pm/ak_day_play_gleam>.

## Development

```sh
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell
```
19 changes: 19 additions & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "ak_day_play_gleam"
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]
gleam_stdlib = "~> 0.34 or ~> 1.0"

[dev-dependencies]
gleeunit = "~> 1.0"
11 changes: 11 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
]

[requirements]
gleam_stdlib = { version = "~> 0.34 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
24 changes: 24 additions & 0 deletions src/ak_day_play_gleam.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import gleam/io
import gleam/int
import gleam/result
import gleam/list

pub fn main() {
io.println("Hello from ak_day_play_gleam!")
}

pub fn score(rolls) {
let frames = list.range(0, 9)
int.sum(list.map(frames, fn(x) { frame_score(x, rolls) }))
}

fn frame_score(frame_number, rolls) {
let initial_index = 2 * frame_number
case
result.unwrap(list.at(rolls, initial_index), 0)
+ result.unwrap(list.at(rolls, initial_index + 1), 0)
{
10 -> 10 + result.unwrap(list.at(rolls, initial_index + 2), 0)
value -> value
}
}
71 changes: 71 additions & 0 deletions test/ak_day_play_gleam_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import gleeunit
import gleeunit/should
import ak_day_play_gleam

pub fn main() {
gleeunit.main()
}

// gleeunit test functions end in `_test`
pub fn gutter_ball_game_test() {
ak_day_play_gleam.score([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(0)
}

pub fn score_of_1_test() {
ak_day_play_gleam.score([
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(1)
}

pub fn score_of_1_not_in_first_frame_test() {
ak_day_play_gleam.score([
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(1)
}

pub fn score_of_2_with_two_singles_test() {
ak_day_play_gleam.score([
0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(2)
}

pub fn score_of_3_with_two_non_zero_rolls_test() {
ak_day_play_gleam.score([
0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(3)
}

pub fn score_spare_test() {
ak_day_play_gleam.score([
6, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(12)
}

pub fn score_spare_test_doubling_two_test() {
ak_day_play_gleam.score([
6, 4, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(14)
}

pub fn score_strike_doubles_the_next_frame_test() {
ak_day_play_gleam.score([
10, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])
|> should.equal(14)
}

pub fn score_adds_up_to_eleven_but_no_spare_test() {
ak_day_play_gleam.score([
6, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0,
])
|> should.equal(11)
}

0 comments on commit 77ba46a

Please sign in to comment.