Code-golfing FizzBuzz

how do you know that such solution exists?

See this previous post I made

1 Like

Not less, more infact, 67 char/byte but one I like
1:100 .|>x->max(("Fizz","Buzz").^(x.%(3,5).<1)|>prod,"$x")|>println

Found a way to use 99, 65/63

0:99 .|>i->println(max("$(i+1)","Fizz"^(i%3÷2)*"Buzz"^(i%5÷4)))

and 64/62

i=0:99;@.println(max(repr(i+1),"Fizz"^(i%3÷2)*"Buzz"^(i%5÷4)))
2 Likes

Couple fun solutions
73Byte/Char
\ =(x,y)->x%y<1;x=1:100;@.max("Fizz"^(x\3)*"Buzz"^(x\5),repr(x))|>println
72Byte/Char
\ =(x,y)->x%y<1;1:100 .|>x->max("Fizz"^(x\3)*"Buzz"^(x\5),"$x")|>println

And got rid of max but not for a shorter solution
68Byte/Char
1:100 .|>x->"Fizz"^(x%3<1)*"Buzz"^(1>x%5)*"$x"^(x%3>0<x%5)|>println

Improved this to 64Byte/Char
i<n=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<0

4 Likes

can you explain step by step how this function works?

i<n=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<0

Not for the println(...) part which is “simple”, but for the i<n=102>i+1< ... ;1<0 part

i<n=...

overloads < operator but it keeps its precedence. We could have written f(i,n)=...

Here is where the thing gets interesting

i<n=102>i+1<...

The first part, 102>i+1, is a comparison, the second is a function call! Let’s rewrite it as

function f(i, n)
    if 102>i+1
        f(i+1, println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i")))
    end
end

as soon as 102>i+1 == false the execution stops. In all the other cases, we call again our function with an increased i and we give the println(...) as the n argument. The println statement is ran at f call time, so it doesn’t really matter what type that argument is.

The magic happens thanks to the chaining properties of < and >. It’s perfectly legit julia code to compare the same integer with two boundaries as 10 < x > 3, as this translates into 10 < x && x > 3.

Finally

...;1<0

launches the recursive function. The first argument is important as it’s your index I, the second one is not that interesting but you need something so 0 it is.

hope it makes sense

8 Likes

thank you very much.
this was the part i was missing…

PS
also need a “;” at the end to avoid having a “false” at the end of the output!?

1 Like

the ; is used to indicate the end of the function. That’s simply to inline the solution rather than having

i<n=...
1<0

both solutions have the same number of bytes, but I find easier to keep everything on a single line.

If instead you are referring to a ; after 1<0, that is not needed as that’s only to avoid the REPL to print the function return argument. In code.golf they use the Julia interpreter and read from stdout. This behaviour can sometimes be abused (e.g. some solutions may stop execution with an error, but that doesn’t show in stdout :stuck_out_tongue: )

this is exactly what I was referring to.
Thanks again for your clear and precise explanations!

To complete, does “false” come from the moment in which i=101 and 102>102 is the last calculated value and is it the one implicitly “returned” by <(i,n)?

PS
What happens to the “nothing” returned by the println(...) function?

PPS
does it make the idea better?

i<n::Nothing=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<nothing

To complete, does “false” come from the moment in which i=101 and 102>102 is the last calculated value and is it the one implicitly “returned” by <(i,n) ?

yes correct, the comparison doesn’t hold and false is returned before the rest of the code is executed. I believe this is called short-circuiting boolean operators

What happens to the “nothing” returned by the println(...) function?

that nothing is never returned, as false is returned first

does it make the idea better?

that is correct but it is preventing to use both Int and Nothing as second argument to <, which is the trick that makes the solution golfy

Interestingly enough, can also end with ;1<2 and still will execute the recursion successfully.

In the REPL you get
ERROR: error in method definition: function Base.< must be explicitly imported to be extended

This recursive solution is 2B/C more so its 66B/C but I like it
n<f=max("Fizz"^(1>n%3)*"Buzz"^(1>n%5),"$n")|>println,101>n+1<f;1<0

1 Like

like this one as it floads the REPL with nested tuples :smiley:

Chaining seems to have potential for getting to the solution but for that to work it seems there must be another ‘trick’ to put in there with it.

something along this line

but replacing gcd with some dark magic?

and finally making it recursive

Yeah that might work,I switched to the Fib problem to see if I find any enlightenment with recursion to take to FizzBuzz,

Maybe this is of no use, but is there a way you have seen to nest the recursion possible using chaining? I suspect that is not of help though

is there a way you have seen to nest the recursion possible using chaining?

not sure I’m following

As in having an outside chaining and and inner chaining, in the recursive manner. I think if thats possible its not of use here so I guess never mind. More of a curiosity on its limit.