chrisschuerz / pasta

Intuitive and readable infix functions to paste strings together
8 stars 0 forks source link

Pasta-operator for paths #2

Open danklotz opened 7 years ago

danklotz commented 7 years ago

I am not sure yet if it is a good idea, but what do you think about an operator which robustly paste path together, by always adding a / while avoiding // sign (I often do not want to have '//' sings in the string for pasting reasons). The realisation could look something like this:

# Pasta Operator
'%+/%' <- function(a,b) {
  paste(a,"/",sep ="") %>% # add '/' as separator
    gsub("[/]{2}","/",.) %>% # remove twice appearing '/'
    paste(.,b, sep ="") # paste a to b
}

Example:

  a1 <- "my_path/my_folder"
  a2 <- "my_path/my_folder/"
  b <- "my_prog"

  a1 %+/% b
  # "my_path/my_folder/my_prog"
  a2 %+/% b
  # "my_path/my_folder/my_prog"
danklotz commented 7 years ago

even better solution

a1 <- "//my_path//my_folder"
a2 <- "my_path/my_folder/"
b <- "my_prog"

'%+/%' <- function(a,b) {
  ifelse( substr(a,nchar(a),nchar(a)) == "/",
          paste(a,b, sep =""),
          paste(a,b, sep = "/"))
}

a1 %+/% b
# [1] "//my_path//my_folder/my_prog"
danklotz commented 7 years ago

some ideas for possible the operator signs:

%///%
%?/%
%/|//%
%>-(((°>% # fish :D
%:p% # licking a stamp 
%add%
%and%
%//-/%
%-/%