Re:FizzBuzz in ten languages
by Spivak on Monday December 31, 2018 @01:43PM
(#18796145)
Your bash example could be a little cleaner. Since 0 is truthy in bash and [ `expr something` ] is, for most purposes, (( )).
Bash also has a decently powerful matching statement so you can do something similar to the rust example.
[ Parent
| on HN ]
#/bin/bash
for n in {1..100} ; do
f=""
((n % 3)) || f="Fizz"
((n % 5)) || f="${f}Buzz"
echo ${f:-$n}
doneMore bikeshedding for the new year:
#!/bin/bash
for n in {1..100}; do
f=
((n % 3)) || f=Fizz
((n % 5)) || f+=Buzz
echo ${f:-$n}
doneI like it! I was avoiding doing the string append version since the author avoided it.
Re:FizzBuzz in ten languages
by gshubert17 on Monday December 31, 2018 @11:09PM
(#18798875)
Bash exit code 0 is truthy, but that's not how the if statement works:
if ((0)); then echo 0=true; else echo 0=false; fi
if ((1)); then echo 1=true; else echo 1=false; fi
# output
0=false
1=true
To work as expected, the if statements should be rewritten as
if ((i % 3 == 0 && i % 5 == 0)); then echo FizzBuzz
and so on.