"This works with any single-index value, and lets all the existing operations for such values continue to work." As Peter Dalgaard already pointed out, that is false.> x <- 1:4 > x[-1][1] 2 3 4> elt(x,-0)[1] 1 Cheers, Bert On Tue, Apr 23, 2024 at 4:55?PM Richard O'Keefe <raoknz at gmail.com> wrote:> > Does it have to use square bracket syntax? > elt <- function (x, i) x[i+1] > "elt<-" <- function (x, i, value) { x[i+1] <- value; x } > > > u <- c("A","B","C") > > elt(u,0) > [1] "A" > > elt(u,length(u)-1) > [1] "C" > > elt(u,0) <- "Z" > > u > [1] "Z" "B" "C" > > This works with any single-index value, and lets all the existing > operations for such values continue to work. It seems to me to be the > simplest and cleanest way to do things, and has the advantage of > highlighting to a human reader that this is NOT normal R indexing. > > On Sun, 21 Apr 2024 at 19:56, Hans W <hwborchers at gmail.com> wrote: > > > > As we all know, in R indices for vectors start with 1, i.e, x[0] is not a > > correct expression. Some algorithms, e.g. in graph theory or combinatorics, > > are much easier to formulate and code if 0 is an allowed index pointing to > > the first element of the vector. > > > > Some programming languages, for instance Julia (where the index for normal > > vectors also starts with 1), provide libraries/packages that allow the user > > to define an index range for its vectors, say 0:9 or 10:20 or even negative > > indices. > > > > Of course, this notation would only be feasible for certain specially > > defined vectors. Is there a library that provides this functionality? > > Or is there a simple trick to do this in R? The expression 'x[0]' must > > be possible, does this mean the syntax of R has to be twisted somehow? > > > > Thanks, Hans W. > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
No, my claim ISN'T false, In fact, it CANNOT be. Here's my claim again, in slow speed. 1. The goal is to get the EFFECT of 0-origin indexing, Not necessarily the syntax. 2. Rule A. NO NEW DATA TYPES. 3, Rule B. NO CHANGES TO EXISTING OPERATIONS, INCLUDING "[" and "[<-",>From rules A and B, it follows that EXISTING OPERATIONS (which aren't changed)continue to work on EXISTING DATA TYPES (which aren't changed) because NOTHING RELEVANT CHANGES. 4. Rule C. As a consequence of rules A and B, to accomplish the goal we should DEFINE A NEW OPERATION with a new name that does whatever we want it to. 5. Because existing code does not call the new operation (it's a new name), it does not break. (OK, if something depends on the absence of "elt", that could break. But code that depends on the absence of variables is broken already.) 6. The specific proposal was elt <- function (x, i) x[i+1] "elt<-" <- function (x, i, value) { x[i+1] <- value; x } 7, with the implication that you can use any name you like 8. and the perhaps less obvious implication that these definitions can be local to a function or a namespace. The idea is that instead of foobar[0] with a modified data type for foobar or a modified definition for "[" you write elt(foobar, 0) which obviously isn't a normal use of "[". If you can explain how this breaks normal indexing, pray do so. On Wed, 24 Apr 2024 at 13:20, Bert Gunter <bgunter.4567 at gmail.com> wrote:> > "This works with any single-index value, and lets all the existing > operations for such values continue to work." > > As Peter Dalgaard already pointed out, that is false. > > > x <- 1:4 > > x[-1] > [1] 2 3 4 > > elt(x,-0) > [1] 1 > > Cheers, > Bert > > On Tue, Apr 23, 2024 at 4:55?PM Richard O'Keefe <raoknz at gmail.com> wrote: > > > > Does it have to use square bracket syntax? > > elt <- function (x, i) x[i+1] > > "elt<-" <- function (x, i, value) { x[i+1] <- value; x } > > > > > u <- c("A","B","C") > > > elt(u,0) > > [1] "A" > > > elt(u,length(u)-1) > > [1] "C" > > > elt(u,0) <- "Z" > > > u > > [1] "Z" "B" "C" > > > > This works with any single-index value, and lets all the existing > > operations for such values continue to work. It seems to me to be the > > simplest and cleanest way to do things, and has the advantage of > > highlighting to a human reader that this is NOT normal R indexing. > > > > On Sun, 21 Apr 2024 at 19:56, Hans W <hwborchers at gmail.com> wrote: > > > > > > As we all know, in R indices for vectors start with 1, i.e, x[0] is not a > > > correct expression. Some algorithms, e.g. in graph theory or combinatorics, > > > are much easier to formulate and code if 0 is an allowed index pointing to > > > the first element of the vector. > > > > > > Some programming languages, for instance Julia (where the index for normal > > > vectors also starts with 1), provide libraries/packages that allow the user > > > to define an index range for its vectors, say 0:9 or 10:20 or even negative > > > indices. > > > > > > Of course, this notation would only be feasible for certain specially > > > defined vectors. Is there a library that provides this functionality? > > > Or is there a simple trick to do this in R? The expression 'x[0]' must > > > be possible, does this mean the syntax of R has to be twisted somehow? > > > > > > Thanks, Hans W. > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code.
Note that elt(x, -1) is NOT AN OCCURRENCE OF AN EXISTING OPERATION. It's a call to elt(-,-), which has its own semantics. And the semantics of (x, i) when i is outside the interval [0,length(x)) is UNDEFINED. # elt(x, i) where i is numeric and all(0 <= i & i < length(x)) selects one or more # elements from x using 0-origin elements. i should not be logical and must not # be character. This cannot be used for deleting. elt <- function (x, i) x[i+1] # elt(x, i) <- value where i is numeric and all(0 <= i & i < length(x)) replaces one # or more elements of x with (corresponding elements of) value using 0-origin indexing. # i should not be logical and must not be character. "elt<-" <- function (x, i, value) { x[i+1] <- value; x} The goal was to get the effect of 0-origin indexing, and that's all elt(-,-) is defined to do. Recall the starting point of this thread. There is existing code in another language or other languages where 0-origin is the norm, and it would be pleasant to transliterate it into R introducing fewer errors. That language/those languages almost certainly do not use R's negative-indexing- for-deletion. Even APL doesn't do that. Therefore elt(-,-) need not do that. And it doesn't, And a limitation like this is useful. If you want to change/extend the semantics of :"[", you have to worry about ALL the legal ways to use "[". But if you are introducing a NEW operation you only have to worry about the things it's intended to do (like play the limited role that indexing plays in some other language(s)). On Wed, 24 Apr 2024 at 13:20, Bert Gunter <bgunter.4567 at gmail.com> wrote:> > "This works with any single-index value, and lets all the existing > operations for such values continue to work." > > As Peter Dalgaard already pointed out, that is false. > > > x <- 1:4 > > x[-1] > [1] 2 3 4 > > elt(x,-0) > [1] 1 > > Cheers, > Bert > > On Tue, Apr 23, 2024 at 4:55?PM Richard O'Keefe <raoknz at gmail.com> wrote: > > > > Does it have to use square bracket syntax? > > elt <- function (x, i) x[i+1] > > "elt<-" <- function (x, i, value) { x[i+1] <- value; x } > > > > > u <- c("A","B","C") > > > elt(u,0) > > [1] "A" > > > elt(u,length(u)-1) > > [1] "C" > > > elt(u,0) <- "Z" > > > u > > [1] "Z" "B" "C" > > > > This works with any single-index value, and lets all the existing > > operations for such values continue to work. It seems to me to be the > > simplest and cleanest way to do things, and has the advantage of > > highlighting to a human reader that this is NOT normal R indexing. > > > > On Sun, 21 Apr 2024 at 19:56, Hans W <hwborchers at gmail.com> wrote: > > > > > > As we all know, in R indices for vectors start with 1, i.e, x[0] is not a > > > correct expression. Some algorithms, e.g. in graph theory or combinatorics, > > > are much easier to formulate and code if 0 is an allowed index pointing to > > > the first element of the vector. > > > > > > Some programming languages, for instance Julia (where the index for normal > > > vectors also starts with 1), provide libraries/packages that allow the user > > > to define an index range for its vectors, say 0:9 or 10:20 or even negative > > > indices. > > > > > > Of course, this notation would only be feasible for certain specially > > > defined vectors. Is there a library that provides this functionality? > > > Or is there a simple trick to do this in R? The expression 'x[0]' must > > > be possible, does this mean the syntax of R has to be twisted somehow? > > > > > > Thanks, Hans W. > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code.