Skip to content

Instantly share code, notes, and snippets.

@scientific-coder
Created March 1, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save scientific-coder/1951396 to your computer and use it in GitHub Desktop.
Save scientific-coder/1951396 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 [budget items ]
"items is a collection of [food price] by C.Grand
returns the combination of food and total price with max price <= budget"
(letfn [(submenus [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))))
(menu 1505 [["Mixed Fruit" 215]["French Fries" 275]["Side Salad" 335]["Hot Wings" 355]["Mozzarella Sticks" 420]["Sampler Plate" 580]] )
(defn menu [budget items]
"items is a collection of [food price]
returns the combination of food and total price with max price <= budget"
(apply max-key second
(reduce (fn [current-menus [food price]]
(reduce (fn [cc-menus [current-menu current-price]]
(let [new-price (+ current-price price)]
(if (<= new-price budget)
(conj cc-menus [(conj current-menu food) new-price])
cc-menus)))
current-menus
current-menus))
[[[] 0]]
items)))
(menu 1505 [["Mixed Fruit" 215]["French Fries" 275]["Side Salad" 335]["Hot Wings" 355]["Mozzarella Sticks" 420]["Sampler Plate" 580]] )
(defn menu
"items is a collection of [food price] by C.Grand
returns the combination of food and total price with max price <= budget"
[budget items]
(->
(reduce (fn [menus [food price]]
(into menus (for [[total foods] menus
:let [p (+ total price)]
:when (<= p budget)]
[p (conj foods food)])))
(sorted-map 0 #{}) items)
rseq
first))
(menu 1505 [["Mixed Fruit" 215]["French Fries" 275]["Side Salad" 335]["Hot Wings" 355]["Mozzarella Sticks" 420]["Sampler Plate" 580]] )
(defn menu [items budget]
"items is a collection of [food price]
returns the combination of food and total price with max price <= budget"
(apply max-key #(get % 1); take max price
(filter (fn [[food price]] (<= price budget )); budget restriction
(for [ i (range 0 (bit-shift-left 1 (count items)))]
(reduce (fn [[food price] [f p]] [(conj food f) (+ price p)])
[[] 0]; combine food and sum prices
(keep-indexed #(if (not= 0 (bit-and i (bit-shift-left 1 %1)))
%2)
items))))))
(menu [["Mixed Fruit" 215]["French Fries" 275]["Side Salad" 335]["Hot Wings" 355]["Mozzarella Sticks" 420]["Sampler Plate" 580]] 1505)
@cgrand
Copy link

cgrand commented Mar 7, 2012

letfn pas nécessairement : il autorise juste la recursion mutuelle. Mais il y avait bien une erreur qui était corrigeable en répétant le nom de le fonction juste après fn.
Mais il y a mieux : on peut écrire ce programme en utilisant reduce et sans utiliser la recursion qui est le GOTO de la prog fonc.

@scientific-coder
Copy link
Author

Merci pour la suggestion de reduce (cf menu-reduce.clj) : c'est effectivement mieux (car plus simple) à mon avis, dommage que cela soit plus difficile pour moi ☹.

@cgrand
Copy link

cgrand commented Mar 8, 2012 via email

@cgrand
Copy link

cgrand commented Mar 8, 2012

Un conseil que je donne à ceux désireux de casser leurs habitudes est de s'interdire certaines structures trop familières :

  • pas de loop pas de recur, pas même de recursion
  • pas de mutations
  • pas d'indices
  • pas de count
  • pas de lazy-seq ou de lazy-cat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment