News for hackers. Stuff that matters.
Re:The most elegant implementation of FizzBuzz by viebel on Wednesday October 26, 2016 @03:56PM (#12799882)
It's amazing how scala syntax is verbose - not to say ugly!
Re:The most elegant implementation of FizzBuzz by AdieuToLogic on Wednesday October 26, 2016 @05:20PM (#12800685)
I was being intentionally verbose so that readers not familiar with Scala or the collections types/methods used would have an easier time following it. I also was avoiding use of pattern matching as they are cousins to conditionals IMHO.

However, below is another version which employs idioms more commonly found:

  object Fizzbuzz
  {
      val fizzes = None :: None :: Some ("Fizz") :: Nil;
      val buzzes = None :: None :: None :: None :: Some ("Buzz") :: Nil;

      def apply (amount : Int) : TraversableOnce[String] =
      {
          val alternates = Stream.continually (fizzes).flatten
            .zip (Stream.continually (buzzes).flatten)
            .take (amount)
            .toArray;

          0.until (amount).map { n =>
              alternates (n) match {
                  case (Some (f), Some (b)) => f + b;
                  case (Some (f), None) => f;
                  case (None, Some (b)) => b;
                  case _ => (n + 1).toString
                  }
              }
      }
  }

  Fizzbuzz (100).foreach (println);
Re:The most elegant implementation of FizzBuzz by viebel on Wednesday October 26, 2016 @11:16PM (#12802671)
Oh! I see. It's much more concise and clean now.

But still I prefer the clojure version using pattern matching over the scala one: http://blog.klipse.tech/clojure/2016/10/25/core-match.html#f...

Re:The most elegant implementation of FizzBuzz by AdieuToLogic on Thursday October 27, 2016 @12:41AM (#12802966)
Gotcha. Then here's a translation into Scala from the link you provided which I believe maintains the spirit of the clojure version:

  object Fizzbuzz
  {
      def apply (amount : Int) : TraversableOnce[String] =
          1.to (amount).map { n =>
              (n % 3, n % 5) match {
                  case (0, 0) => "FizzBuzz";
                  case (0, _) => "Fizz";
                  case (_, 0) => "Buzz";
                  case _ => n.toString
                  }
              }
  }
  
  Fizzbuzz (100).foreach (println);

EDIT: When I said "Gotcha", I meant that as in "understood" and not in an adversarial manner. Quite the opposite, actually, as this is a respectful thread which I believe both parties have enjoyed.