diff --git a/expensereport-gleam/.gitignore b/expensereport-gleam/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/expensereport-gleam/.gitignore @@ -0,0 +1 @@ +build diff --git a/expensereport-gleam/README.md b/expensereport-gleam/README.md new file mode 100644 index 0000000..196dc7a --- /dev/null +++ b/expensereport-gleam/README.md @@ -0,0 +1,14 @@ +# Installing and Running + +This kata was created for gleam 0.30.0. +You may have to run + +``` +gleam update +``` + +to get up to date `stdlib` if you run a newer version of the compiler. Then: + +``` +gleam run +``` diff --git a/expensereport-gleam/gleam.toml b/expensereport-gleam/gleam.toml new file mode 100644 index 0000000..28c4348 --- /dev/null +++ b/expensereport-gleam/gleam.toml @@ -0,0 +1,2 @@ +name = "expense_report" +dependencies = { gleam_stdlib = "~> 0.30" , birl = "~> 0.14"} diff --git a/expensereport-gleam/manifest.toml b/expensereport-gleam/manifest.toml new file mode 100644 index 0000000..9bc4a03 --- /dev/null +++ b/expensereport-gleam/manifest.toml @@ -0,0 +1,12 @@ +# This file was generated by Gleam +# You typically do not need to edit this file + +packages = [ + { name = "birl", version = "0.14.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "4C9A60041E770D02FBD65A5B774026CD536B45A6AFDDBFC601A4450567AEE313" }, + { name = "gleam_stdlib", version = "0.30.2", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "8D8BF3790AA31176B1E1C0B517DD74C86DA8235CF3389EA02043EE4FD82AE3DC" }, + { name = "ranger", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "A3D54DD2BFCC654F1AA7A0D245F6F59A5D9FF6B2D68449C849BFE321E945EA7A" }, +] + +[requirements] +birl = { version = "~> 0.14" } +gleam_stdlib = { version = "~> 0.30" } diff --git a/expensereport-gleam/src/expense_report.gleam b/expensereport-gleam/src/expense_report.gleam new file mode 100644 index 0000000..d67a3e8 --- /dev/null +++ b/expensereport-gleam/src/expense_report.gleam @@ -0,0 +1,60 @@ +import gleam/int +import gleam/list.{fold} +import gleam/io.{println} +import birl/time + +type ExpenseType { + Dinner + Breakfast + CarRental +} + +type Expense { + Expense(expense_type: ExpenseType, amount: Int) +} + +fn print_report(expenses: List(Expense)) { + println("Expense Report: " <> time.to_http(time.now())) + let [meal_expenses, total] = + fold( + expenses, + [0, 0], + fn(acc, expense) { + let [meal_expenses, total] = acc + let meal_expenses = case + expense.expense_type == Dinner || expense.expense_type == Breakfast + { + True -> meal_expenses + expense.amount + False -> meal_expenses + } + let expense_name = case expense.expense_type { + Dinner -> "Dinner" + Breakfast -> "Breakfast" + CarRental -> "Car Rental" + } + let meal_over_expenses_marker = case + expense.expense_type == Dinner && expense.amount > 5000 || expense.expense_type == Breakfast && expense.amount > 1000 + { + True -> "X" + False -> " " + } + println( + expense_name <> "\t" <> int.to_string(expense.amount) <> "\t" <> meal_over_expenses_marker, + ) + let total = total + expense.amount + [meal_expenses, total] + }, + ) + println("Meal Expenses: " <> int.to_string(meal_expenses)) + println("Total Expenses: " <> int.to_string(total)) +} + +pub fn main() { + print_report([ + Expense(Dinner, 5000), + Expense(Dinner, 5001), + Expense(Breakfast, 1000), + Expense(Breakfast, 1001), + Expense(CarRental, 4), + ]) +}