Record of experiments, readings, links, videos and other things that I find on the long road.
Registro de experimentos, lecturas, links, vídeos y otras cosas que voy encontrando en el largo camino.
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:
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
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
Why it is so complicated?
ReplyDelete(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)))))
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.
DeleteThank you very much for your feedback.
My fizzbuzz with density level over 9000 :)
ReplyDelete(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))
Nice solution!
DeleteSome 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.
Nice !
DeleteThanks !
Did you consider showing your code in a live environment with KLIPSE:
ReplyDeleteSee: http://bit.ly/1S9YTbH
I'll have a look at it.
DeleteThanks Yeonathan
Hi Manuel,
ReplyDeleteDid you take a look at KLIPSE? Would you like to have a POC for KLIPSE on your blog?
Your can reach me on twitter @viebel
ReplyDeleteRemember 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
ReplyDeleteThank you very much Ryan!
DeleteYou're totally right.
I changed the code as you say.
Best regards