Skip to content

Instantly share code, notes, and snippets.

@bhenry
Created September 22, 2010 15:34
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 bhenry/91de400e806503b4ba3c to your computer and use it in GitHub Desktop.
Save bhenry/91de400e806503b4ba3c to your computer and use it in GitHub Desktop.
;;a-set1 looks like:
({:selected true, :weight 1.0, :text "Yes"}
{:selected false, :weight 0.0, :text "No"}
{:selected false, :weight -1.0, :text "NA"})
;;;===================
;; is there a better way to avoid two map calls?
;;;===================
(defn select-answer [a-set weight]
"selects answer by weight returning original a-set if no matching weight"
(if (empty? (filter #(= % weight) (map :weight a-set)))
a-set
(map #(if (= (:weight %) weight)
(select-ans %)
(deselect-ans %)) a-set)))
qa.scorecard> (select-answer a-set1 -3)
(#:qa.db.Answer{:text "Yes", :weight 1.0, :selected true}
#:qa.db.Answer{:text "No", :weight 0.0, :selected false}
#:qa.db.Answer{:text "NA", :weight -1.0, :selected false})
qa.scorecard> (select-answer a-set1 -1)
(#:qa.db.Answer{:text "Yes", :weight 1.0, :selected false}
#:qa.db.Answer{:text "No", :weight 0.0, :selected false}
#:qa.db.Answer{:text "NA", :weight -1.0, :selected true})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment