psolymos / intrval

Relational Operators for Intervals
41 stars 4 forks source link

reverse type if the interval limits are swapped #12

Closed mgacc0 closed 7 years ago

mgacc0 commented 7 years ago

When an interval is expressed in reverse order, it would be coherent to swap the order of parentheses and brackets:

expect_false(.intrval(4, c(5, 4), "[)"))
# Error: .intrval(4, c(5, 4), "[)") isn't false.

expect_false(.intrval(4, c(4, 5), "(]"))

expect_true(.intrval(5, c(5, 4), "[)"))
# Error: .intrval(5, c(5, 4), "[)") isn't true.

reverse_type <- function (type) {
  ifelse(type=="(]", "[)",
         ifelse(type=="[)", "(]", type))
}
reverse_type("(]")
# [1] "[)"
reverse_type("[)")
# [1] "(]"

Or maybe a warning should be shown when a swapped interval is found?

psolymos commented 7 years ago

That is so by design explained in this blog post among the motivation. The interval operator expresses the type of lower (left bracket) and upper (right bracket) endpoint, while in the RHS the values are sorted, so that smaller becomes the lower endpoint irrespective of sorting.