I was messing around online at work the other day when our phone systems went down. I came across the R programming language. Now I'm not guru with programming by any means but I like to mess around with different languages here and there, I believe it helps me to become a better programmer. So I decided that I would try to solve the fizzbuzz challenge in R for those of you that don't know what fizzbuzz is:Write a program that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzzThis is the first thing I've ever written in R and I would like to have some critique done on what I've accomplished so far:fizzBuzz = function(range, x, y){    for (i in seq(1, range, by=1)){        if (i %% x == 0 & i %% y == 0){            print('FizzBuzz')        }        else if (i %% y ==0){            print('Buzz')        }        else if (i %% x == 0){            print('Fizz')        }        else{            print(i)        }    }}fizzBuzz(100, 3, 5)Some key things I'd like to focus on, per norm feel free to critique everything;Is it a little bit over kill to put this all in a function, obviously I could do it simply without the function, but is this considered good practice in R to put your solutions inside of functions? I only ask because I looked around at peoples code and didn't see very many functions in it.Are there easier ways to create the range? I feel like seq(from, to, by=1) is a little harder to understand then range(from, to)
Fizz, Buzz, or FizzBuzz?
beginner;programming challenge;r;fizzbuzz
To answer your initial questions:I can't speak for R good practices in general - but I likefunctions, and I'm still reusing many of my functions created in myfirst R script. They also work well with parallelization. You can use a range shorthand syntax such as 1:n to create a range from 1 through n. Alternatively seq(1, n) would suffice in your use case.Moreover:Printing values generally isn't very useful. It doesn't work well with parallelization / vectorization. And in R studio, you can store values in variables and use them later; this wouldn't work with printing on the go.Loop based approaches, in my experience, are quite slow. Consider using lapply.I prefer <- over = just to remind me it's not C. (very highly subjective). When its not a top level assignment, e.g., named arguments, I use = due to the difference in scope.Here's how I would write it. I've also included a parallel function, parallelization works quicker than lapply when range is sufficiently high. The code is supplemented with a mini benchmark, I've adjusted your for-loop code to be compatible, and inlined the integers 3 and 5 where appropriate.# Run this once to install benchmark suite:#install.packages(c(microbenchmark, stringr), dependencies = TRUE)require(microbenchmark)library(parallel)# Setup parallelization particulars.cores <- detectCores()cluster <- makeCluster(cores)gerardFizzBuzz <- function(i) {    fizz <- i %% 3    buzz <- i %% 5    if (fizz == 0 & buzz == 0) {      return('FizzBuzz')    }    else if (buzz == 0) {      return('Buzz')    }    else if (fizz == 0) {      return('Fizz')    }    return(i)}applyFizzBuzz <- function(range) {    return(lapply(1:range, gerardFizzBuzz))}parallelFizzBuzz <- function(range) {    return(parLapply(cluster, 1:range, gerardFizzBuzz))}vectorizedFizzBuzz <- function(range) {    v <- Vectorize(gerardFizzBuzz)    return(v(1:range))}papasmurfFizzBuzz <- function (range) {    res <- seq(1, range)    for (i in res){      if (i %% 3 == 0 & i %% 5 == 0){        res[i] <- 'FizzBuzz'      }      else if (i %% 5 ==0){        res[i] <- 'Buzz'      }      else if (i %% 3 == 0){        res[i] <- 'Fizz'      }      else{        res[i] <- i      }    }    return(res)}range <- 100000;perf <- microbenchmark(applyFizzBuzz(range), vectorizedFizzBuzz(range), parallelFizzBuzz(range), papasmurfFizzBuzz(range), times=20)# note the log scale.boxplot(perf, names = c(lapply, Vectorized, parLapply, forloop))stopCluster(cluster)This is the result of running the above script on my machine (4 cores), with range <- 100000 and times=20.