choose(clojure.contrib.probabilities.finite-distributions)

Construct a distribution from an explicit list of probabilities and values. They are given in the form of a vector of probability-value pairs. In the last pair, the probability can be given by the keyword :else, which stands for 1 minus the total of the other probabilities.

; clojure/contrib/probabilities/finite_distributions.clj:94
(defn choose
  [& choices]
  (letfn [(add-choice [dist [p v]]
	    (cond (nil? p) dist
		  (= p :else)
		        (let [total-p (reduce + (vals dist))]
			  (assoc dist v (- 1 total-p)))
		  :else (assoc dist v p)))]
    (reduce add-choice {} (partition 2 choices))))

Copyright (c) Rich Hickey. All rights reserved.

The use and distribution terms for this software are covered by the Eclipse Public License 1.0, which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound by the terms of this license. You must not remove this notice, or any other, from this software.