Skip to content

Instantly share code, notes, and snippets.

@zoldar
Last active December 26, 2019 23:25
Show Gist options
  • Save zoldar/8778b6777906c5a1370028fa9fe605b0 to your computer and use it in GitHub Desktop.
Save zoldar/8778b6777906c5a1370028fa9fe605b0 to your computer and use it in GitHub Desktop.
// pickaroo: Money
pub type Currency {
USD
EUR
PLN
}
pub type Money {
Money(cents: Int, currency: Currency)
}
pub type MoneyError {
CurrenciesMismatch(c1: Currency, c2: Currency)
}
pub fn add(m1: Money, m2: Money) {
case m1, m2 {
Money(cents1, currency), Money(cents2, currency) -> Ok(Money(cents1 + cents2, currency))
Money(currency: c1), Money(currency: c2) -> Error(CurrenciesMismatch(c1, c2))
}
}
import pickaroo
import gleam/expect
pub fn add_ok_test() {
pickaroo.add(pickaroo.Money(100, pickaroo.USD), pickaroo.Money(200, pickaroo.USD))
|> expect.equal(_, Ok(pickaroo.Money(300, pickaroo.USD)))
}
pub fn add_mismatch_test() {
pickaroo.add(pickaroo.Money(100, pickaroo.USD), pickaroo.Money(200, pickaroo.EUR))
|> expect.equal(_, Error(pickaroo.CurrenciesMismatch(pickaroo.USD, pickaroo.EUR)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment