I wrote a tail-end recursive function to compute all the possible words that can be composed with a 10-digit phone number.
The mapping I used from digit to letter is:
digit_to_characters <- c("1" = "", "2" = "abc", "3" = "def", "4" = "ghi",
"5" = "jkl", "6" = "mno", "7" = "pqrs",
"8" = "tuv", "9" = "wxyz", "0" = "")
Without using any of these two versions of trampolin, using straight explicit recursion fills up the stack and I cannot do more than 5 effective digits (that is not counting 0).
Then I used the original version from alandipert in my function to_vector_of_strings_1 and the second version without the do.call in my function to_vector_of_strings_2.
I compared the execution time of the two versions of the trampoline and here are the results:
> s_to_parse <- "4039829222"
> microbenchmark(to_vector_of_strings_1(s_to_parse),times = 10L)
Unit: seconds
expr min lq mean median uq max neval
to_vector_of_strings_1(s_to_parse) 10.55397 11.845 12.42362 11.89739 12.22428 15.9142 10
> microbenchmark(to_vector_of_strings_2(s_to_parse),times = 10L)
Unit: seconds
expr min lq mean median uq max neval
to_vector_of_strings_2(s_to_parse) 11.90121 12.12616 12.38451 12.37848 12.56244 13.07632 10
The change to using an S3 class and avoiding the do.call does not seem to provide a meaningful improvement in execution time as reported by microbench.
Both versions work well in R, I am using RStudio Version 1.1.463 and R version 3.4.4:
> version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 4.4
year 2018
month 03
day 15
svn rev 74408
language R
version.string R version 3.4.4 (2018-03-15)
nickname Someone to Lean On