Hey guys, I'm looking for _any_ feedback/thoughts on this Clojure code I wrote. I just feel like the entire thing is way too complex, but I'm not sure about how to simplify it. I wanted to try something "real world" so I made a simple shopping cart ref to put in a session:
(defstruct cart :line-items :total) (defstruct line-item :product :qty :subtotal) (defn create-line-item [product qty] (struct line-item product qty (* qty (product :price)))) (defn add-line-item [cart line-item] (assoc cart :line-items (conj (cart :line-items) line-item) :total (+ (cart :total) (line-item :subtotal)))) (defn update-line-item [line-items product qty] (cond (empty? line-items) () (= ((first line-items) :product) product) (cons (assoc (first line-items) :qty qty :subtotal (* qty (((first line-items) :product) :price))) (rest line-items)) :else (cons (first line-items) (update-line-item (rest line-items) product qty)))) (defn remove-line-item [cart line-item] (assoc cart :line-items (remove #{line-item} (cart :line-items)) :total (- (cart :total) (line-item :subtotal)))) (defn add-to-cart [product qty cart] (dosync (let [li (create-line-item product qty)] (alter cart add-line-item li)))) (defn update-cart-helper [cart product qty] (let [uli (update-line-item (cart :line-items) product qty)] (assoc cart :line-items uli :total (reduce + (map #(% :subtotal) uli))))) (defn update-cart [product qty cart] (dosync (alter cart update-cart-helper product qty))) (defn remove-from-cart [line-item cart] (dosync (alter cart remove-line-item line-item))) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---