## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup, message = FALSE---------------------------------------------------
library(vecvec)
library(S7)
library(vctrs)

## ----inner--------------------------------------------------------------------
new_amount <- function(x = double(), currency = "USD") {
  structure(as.double(x), currency = currency, class = "amount")
}

format.amount <- function(x, ...) {
  paste(formatC(unclass(x), format = "f", digits = 2), attr(x, "currency"))
}

Ops.amount <- function(e1, e2) {
  cur <- unique(c(attr(e1, "currency"), attr(e2, "currency")))
  if (length(cur) > 1) {
    stop("Can't combine ", paste(cur, collapse = " and "), ".", call. = FALSE)
  }
  out <- get(.Generic)(unclass(e1), unclass(e2))
  if (.Generic %in% c("+", "-", "*", "/")) new_amount(out, cur) else out
}

## ----container----------------------------------------------------------------
class_money <- new_class(
  "money",
  parent = class_vecvec,
  # Set automatically when defined inside a package
  package = "mypkg"
)

money <- function(x = double(), currency = "USD") {
  class_money(list(new_amount(x, currency)))
}

method(vec_ptype_full, class_money) <- function(x, ...) "money"
method(vec_ptype_abbr, class_money) <- function(x, ...) "money"

## ----combine------------------------------------------------------------------
x <- c(money(c(10, 25), "AUD"), money(5, "EUR"))
x

## ----onload, eval = FALSE-----------------------------------------------------
# .onLoad <- function(libname, pkgname) {
#   class_money <<- S7::new_class("money", parent = vecvec::class_vecvec)
#   S7::methods_register()
#   vecvec::vecvec_register(
#     class_money,
#     f_ptype2 = vec_ptype2_money,
#     f_cast_to = vec_cast_to_money
#   )
# }

## ----register-----------------------------------------------------------------
vec_ptype2_money <- function(x, y, ...) class_money()

vec_cast_to_money <- function(x, to, ...) {
  if (S7_inherits(x, class_money)) return(x)
  if (is.numeric(x)) return(money(x))
  stop_incompatible_cast(x, to, x_arg = "", to_arg = "")
}

vecvec_register(
  class_money,
  f_ptype2 = vec_ptype2_money,
  f_cast_to = vec_cast_to_money
)

vec_c(x, 1)
tibble::tibble(x)

## ----free, error = TRUE-------------------------------------------------------
try({
x * 2
x > 7
x + c(money(c(1, 2), "AUD"), money(3, "EUR"))
x + money(1, "AUD")
})

## ----rep----------------------------------------------------------------------
str(rep(x, 1000)@x)

## ----apply-fn-----------------------------------------------------------------
currency <- function(x) UseMethod("currency")
currency.amount <- function(x) rep(attr(x, "currency"), length(x))
method(currency, class_money) <- vecvec_apply_fn(currency, ptype = character())

currency(x)

## ----apply--------------------------------------------------------------------
vecvec_apply(x, function(a) new_amount(unclass(a) * 1.1, attr(a, "currency")))

## ----mapply-------------------------------------------------------------------
within_budget <- function(x, limit) {
  vecvec_mapply(
    list(x, limit),
    function(amount, limit) unclass(amount) <= limit,
    ptype = logical()
  )
}
within_budget(x, c(20, 20, 10))

