An APL one-liner where INDEX ORIGIN is 0 :
(L,‘Fizz’ ‘Buzz’ ‘FizzBuzz’)[1+(LW=0)+W{assign}(100~0=W)+W{assign}{declose}+/1 20=3 5|{enclose}L{assign}1+{index}100]
“{index}100” creates a numeric vector from 0 to 99, “1+” brings it up to 1…100, which we {assign} to L.
“0=3 5|{enclose}L” compute the modulo of 3 and 5 for L. Enclosing vector L makes it a scalar, which can then be taken as argument of a modulo (primitives in APL requires vectors of same length on each side, or a vector and a scalar, the latter being applied to each item of said vector). This gives us a vector of two vectors (one for modulo 3, the other for modulo 5). “0=” gives us two binary vector where 1s point every solution where the number is a multiple of either 3 or 5.
“W{assign}{declose}+/1 2” multiplies these vectors by 1 and two respectively (now 1s represent modulo 3, and 2s represent modulo 5), and then we sum the two vectors. “/” is an operator which introduce the function between each item of a vector : +/1 2 3 = 1 + 2 + 3
In this case : +/(0 0 1 0 0 1 …)(0 0 0 0 2 0 …) = (0 0 1 0 0 1 …) + (0 0 0 0 2 0 …). Since the result is still a vector of vectors (with only one item), we {declose} it, which makes it a simple vector, and {assign} it to W.
“W{assign}(100~W=0)+” : we then add this vector to another where every number not 0 is replaced by 100. “~W=0” (~ means not) creates a binary vector with 1s everywhere that’s not 0, which we multiply by 100. So now we have (0 0 101 0 102 0 …). We {assign} it to W.
“1+(LW=0)+” : we use the reverse operation “=0” to create a binary vector with 1s everywhere that’s not a multiple of 3 or 5 and “L” to replace every 1 with the original number. We then add this to W, and “voil !”, every number a multiple of 3 or 5 or both is replaced by 101, 102 or 103. Since indexing starts from 0, we substract 1 from the vector “1+” (- is reserved for substraction, indicates negative numbers : 3-1 = 4).
“(L,‘Fizz’ ‘Buzz’ ‘FizzBuzz’)[ …the rest… ]” creates a vector where ‘Fizz’, ‘Buzz’ and ‘FizzBuzz’ are concatenated at the end of L, and [] indicates that we use what’s inside the brackets to index the vector to the left : ‘abc’[1 0 2] = ‘bac’
And there you go : Fizz, Buzz and FizzBuzz replace multiples of 3, 5 or both.
It is possible to create more readable code, but not as fun !
This code, which can be the body of a function, does the same thing in easy to read steps :
List{assign}1+{index}100
Fizz{assign}0=3|List
Buzz{assign}20=5|L
W{assign}Fizz+Buzz
W{assign}W+100~0=W
(L,‘Fizz’ ‘Buzz’ ‘FizzBuzz’)[1+(ListW=0)+W]