Hacker News new | past | comments | ask | show | jobs | submit login

My "I'm going to hell, but that's okay" C version:

  #include <stdio.h>
  #include <stdbool.h>

  #define when(mod, msg) do { if((i mod) == 0) { fputs(#msg, stdout); *hit = true; } } while(0)
  #define through ; i <=
  #define engine(range) \
  void rules(int, bool *);\
  int main(void) { for(int i = range; ++i) { bool hit = false; rules(i, &hit); if(!hit) printf("%d", i); putchar('\n'); } } \
  void rules(int i, bool *hit)

  engine(1 through 100) {
    when(% 3, Fizz);
    when(% 5, Buzz);
  }
Super extensible!



I'm not sure even hell will take you after doing what you just did. There is no metaphysical consequence great enough save to have experienced the writing of that.


My "I'm going to hell" C++ version: https://gist.github.com/3838042

Or, since github's being broken right now:

    #include <iostream>
    #include <arpa/inet.h>
    
    template <int multiples_of, uint32_t noise>
    class Noisemaker {
    private:
        union {
            uint32_t noise_as_int;
            char noise_as_cstr[5];
        };
    public:
        Noisemaker() {
            noise_as_int = htonl(noise);
            noise_as_cstr[4] = '\0';
        }
        bool operator()(std::ostream &out, int i) {
            if(i % multiples_of == 0) {
                out << noise_as_cstr;
                return true;
            }
            return false;
        }
    };
    
    template <typename Noise1, typename Noise2>
    class NoisemakerPair {
    private:
        Noise1 noise1;
        Noise2 noise2;
    public:
        bool operator()(std::ostream &out, int i) {
            bool matched = false;
            matched |= noise1(out, i);
            matched |= noise2(out, i);
            return matched;
        }
    };
    
    int main(int argc, char *argv[]) {
        NoisemakerPair<Noisemaker<3, 'Fizz'>, Noisemaker<5, 'Buzz'> > fizzbuzz;
        for(int i=1;i<=100;++i) {
            if(!fizzbuzz(std::cout, i))
                std::cout << i;
            std::cout << std::endl;
        }
    }
You can nest NoisemakerPairs to add more Noisemakers. But good luck with strings longer than 4 characters… for some absurd reason, string literals can't be template parameters. Go figure.


My going to hell version:

https://github.com/rcs/fizzbuzz/blob/master/bitwise.c

Choice excerpts:

  char fmts[] = "FizzBuzz%u";
and

      // Default start is 8
    unsigned int start =
      // Shift down 1 if div5
      (8 >> DIV5(mask))
      // Shift down another 4 if div3 */
      >> ( DIV3(mask) << 2);




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: