Created
September 8, 2023 14:11
-
-
Save JosiahParry/d7b2e5b3c6c51023b7ac4277c1383b59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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