Skip to content

Instantly share code, notes, and snippets.

@kgadek
Last active September 26, 2015 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgadek/1119119 to your computer and use it in GitHub Desktop.
Save kgadek/1119119 to your computer and use it in GitHub Desktop.
Clojure: 99 bottles (II)
(ns org.gadek.fun.bottles)
(defn bottle-cnt [stock]
(str (if (zero? stock) "No more" stock)
" bottle" (when (not (= 1 stock)) "s")
" of beer"))
(defn verses [& [stock prev]]
(lazy-seq (let [stock (or stock 0) ; start from 0
prev (or prev (bottle-cnt 99)) ; even earlier -- 99 bottles
num-bottles (bottle-cnt stock) ; hey, joe! how many bottles?
command (if (zero? stock) ; what to do with it?
"Go to the store and buy some more"
"Take one down and pass it around")]
(cons (str num-bottles " on the wall, " (.toLowerCase num-bottles) ".\n"
command ", " (.toLowerCase prev) " on the wall.\n\n")
(verses (inc stock) (bb stock))))))
(def sing #(->> (verses) (take 100) reverse (apply str) println))
; take 100 verses (0->99), reverse them (99->0), join and print it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment