gagolews / stringi

Fast and portable character string processing in R (with the Unicode ICU)
https://stringi.gagolewski.com/
Other
304 stars 44 forks source link

`%s+%` is not always performing string concatination #281

Closed AndreMikulec closed 6 years ago

AndreMikulec commented 7 years ago

%s+% supposed to perform string concatination. It does not always do that.

library(stringi)

> { if(letters[2] == 'a') { 'A' } } %s+% { if(letters[1] == 'a') { 'A' } }
character(0)

> { if(letters[1] == 'a') { 'A' } } %s+% { if(letters[2] == 'a') { 'A' } }
character(0)

This happens because one side ( eithor left or right ) is returning NULL. The output is correct (as far as I know) behaviour.

I recommend (feature request) that a safer concat operator be added.

Since situations are cumbersome to program 'around' these 'correct cases', I recommend that a safer concatenation infix operator be added to stringi. Call it 'Capital S plus.'

This is what I use in my own work.

`%S+%` <- function(x,y) {

  if(is.null(x) || is.na(x) || !length(x) ) '' -> x
  if(is.null(y) || is.na(y) || !length(y) ) '' -> y

  return(stringi::stri_join(x, y, sep=""))

}

> NULL %S+% NULL
[1] ""

> NA %S+% NA
[1] ""

> c() %S+% c()
[1] ""

> { if(letters[2] == 'a') { 'A' } } %S+% { if(letters[1] == 'a') { 'A' } }
[1] "A"

> { if(letters[1] == 'a') { 'A' } } %S+% { if(letters[2] == 'a') { 'A' } }
[1] "A"

Just my two cents. Andre Mikulec

gagolews commented 7 years ago

I like your proposal. However, also other operators like %S< should be overloaded too. Could you prepare a pull request (operators' definitions and updated manual). Thanks!

AndreMikulec commented 7 years ago

O.K. I will start working on that. Work will use up some time though.

gagolews commented 7 years ago

no worries, take your time, I'm busy too..

gagolews commented 6 years ago

I gave it a thought, I want to keep the behavior consistent with, among others;

> c() + 1
numeric(0)
> NULL + 1
numeric(0)

but stri_paste has had the ignore_null arg for a long time