Skip to content

Instantly share code, notes, and snippets.

@cgrand
Forked from scientific-coder/fib-seq-1.clj
Created March 6, 2012 17:35
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 cgrand/1987654 to your computer and use it in GitHub Desktop.
Save cgrand/1987654 to your computer and use it in GitHub Desktop.
Code snippets for Duchess-fr Battle Language 2012-02-29 test it on http://tryclj.com/ !
(def fib-seq
"lazy seq of Fibonacci numbers"
(lazy-cat [0 1] (map + (rest fib-seq) fib-seq)))
(def fib-seq
(map first (iterate (fn[[a b]] [b (+ a b)]) [0 1])))
(def fizzbuzz
"lazy seq of fizzbuzz"
(lazy-seq (map #(let [s (str (if (zero? (rem % 3)) "Fizz")
(if (zero? (rem % 5)) "Buzz"))]
(if (empty? s) % s))
(iterate inc 1))))
(def fizzbuzzzapp
"lazy seq of fizzbuzzzapp more interesting (imo)than fizzbuzz
because it starts to pay off to factor some code "
(lazy-seq (map #(let [s (apply str (for [[val name] [[3 "Fizz"]
[5 "Buzz"]
[7 "Zapp"]]]
(if (zero? (rem % val)) name)))]
(if (empty? s) % s))
(iterate inc 1))))
(defn menu [items budget]
"items is a collection of [food price]
returns the combination of food and total price with max price <= budget"
(let [submenus (fn [items]
(if-let [[[food price] & xs] (seq items)]
(let [menus (submenus xs)]
(concat menus (for [[m p] menus
:let [p (+ p price)]
:when (<= p budget)]
[(conj m food) p])))
[[#{} 0]]))]
(apply max-key second (submenus items))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment