Wednesday, April 13, 2016

Kata: FizzBuzz in Clojure using cond->

To learn how to use the cond-> macro, I did the FizzBuzz kata with it:

I also added this example to ClojureDocs cond-> documentation.

11 comments:

  1. Why it is so complicated?

    (defn say [n]
    (let [append #(cond-> %1 (divisible-by? %2 n) (str %3))]
    (-> nil
    (append 3 "Fizz")
    (append 3 "Buzz")
    (append 3 "Bang")
    (or (str n)))))

    ReplyDelete
    Replies
    1. I constrained myself to only use cond-> in order to learn more about it. Of course, using other features of the languages makes it much simpler.

      Thank you very much for your feedback.

      Delete
  2. My fizzbuzz with density level over 9000 :)

    (defn fizzbuzz [n]
    (letfn [(x [d v] (if (= (mod n d) 0) v ""))]
    (join "" [(x 3 "Fizz") (x 5 "Buzz")])))

    To run
    (map fizzbuzz (range 1 100))

    ReplyDelete
    Replies
    1. Nice solution!

      Some time ago, I did it with the constraint of not using conditionals (if, cond, etc.) and I got this:
      Kata: FizzBuzz with no conditionals in Clojure
      http://garajeando.blogspot.com.es/2014/11/kata-fizzbuzz-with-no-conditionals-in.html

      Thanks a lot for commenting.

      Delete
  3. Did you consider showing your code in a live environment with KLIPSE:
    See: http://bit.ly/1S9YTbH

    ReplyDelete
    Replies
    1. I'll have a look at it.
      Thanks Yeonathan

      Delete
  4. Hi Manuel,

    Did you take a look at KLIPSE? Would you like to have a POC for KLIPSE on your blog?

    ReplyDelete
  5. Your can reach me on twitter @viebel

    ReplyDelete
  6. Remember that the `cond->` macro automatically adds the argument in as the first arg for you, so all the lines that look like `(#(str % "Fizz")` can be replaced with just `(str "Fizz")` for the exact same functionality

    ReplyDelete
    Replies
    1. Thank you very much Ryan!
      You're totally right.
      I changed the code as you say.
      Best regards

      Delete