Skip to content

Instantly share code, notes, and snippets.

@scotthaleen
Created May 24, 2018 21:20
Show Gist options
  • Select an option

  • Save scotthaleen/6101d0f12a5c02b45e7a17e11b84cf27 to your computer and use it in GitHub Desktop.

Select an option

Save scotthaleen/6101d0f12a5c02b45e7a17e11b84cf27 to your computer and use it in GitHub Desktop.
clojure fizzbuzz with y-combinator
(defn fizzbuzz
"
rules - map of number and string ex. {3 \"fizz\" 5 \"buzz\"}
limit - number to count too
"
[rules limit]
(((fn [f] (f f))
(fn [f]
(fn [x]
(if (zero? x) []
(conj
((f f) (dec x))
(or
(some
(fn [[n s]]
(cond (zero? (mod x n)) s))
(reduce
(fn [acc [n s]]
(concat
(for [[nn ss] acc]
[(* nn n) (str ss s)])
(conj acc [n s]))) []
(sort (seq rules))))
(str x)))))))
limit))
;;(fizzbuzz {3 "fizz" 5 "buzz" 7 "bazz"} 20)
;;=> ["1" "2" "fizz" "4" "buzz" "fizz" "bazz" "8" "fizz" "buzz" "11" "fizz" "13" "bazz" "fizzbuzz" "16" "17" "fizz" "19" "buzz"]
;;http://blog.klipse.tech/lambda/2016/08/07/almost-y-combinator-clojure.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment