Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Created September 8, 2023 14:11
Show Gist options
  • Save JosiahParry/d7b2e5b3c6c51023b7ac4277c1383b59 to your computer and use it in GitHub Desktop.
Save JosiahParry/d7b2e5b3c6c51023b7ac4277c1383b59 to your computer and use it in GitHub Desktop.
# https://fosstodon.org/@coolbutuseless/111029564256521659
x <- letters
x <- structure(letters, class = "zero_indexed")
`[.zero_indexed` <- function(obj, i, ...) {
if (is.numeric(i)) {
i <- i + 1
NextMethod()
} else if (is.character(i)) {
x[setNames(0:(length(x)-1), x)[i]]
}
}
x[0]
x[25]
x[c("a", "z")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment