General FizzBuzz: Difference between revisions

Content added Content deleted
(OCaml implementation)
(→‎Names solution: Added syntactic sugar.)
Line 2,881: Line 2,881:


===Names solution===
===Names solution===
If we deviate from the task's example of how to input parameters and instead use R's names function to make our (number, name) pairs, we get a much cleaner solution.
If we deviate from the task's example of how to input parameters and instead use R's names facilities to make our (number, name) pairs, we get a much cleaner solution.
<lang r>namedGenFizzBuzz<-function(n,namedNums)
<lang r>namedGenFizzBuzz<-function(n,namedNums)
{
{
Line 2,892: Line 2,892:
invisible()
invisible()
}
}
namedNums<-c(Fizz=3,Buzz=5,Baxx=7)#Notice that we can name our inputs without a call to names
namedNums<-c(3,5,7); names(namedNums)<-c("Fizz","Buzz","Baxx")
namedGenFizzBuzz(105,namedNums)
namedGenFizzBuzz(105,namedNums)
shuffledNamedNums<-c(5,9,3,7); names(shuffledNamedNums)<-c("Buzz","Prax","Fizz","Baxx")
shuffledNamedNums<-c(Buzz=5,Prax=9,Fizz=3,Baxx=7)
namedGenFizzBuzz(105,shuffledNamedNums)</lang>
namedGenFizzBuzz(105,shuffledNamedNums)</lang>