<commit_before>(ns mokkameister.random)

(defn rand-nth-weighted
  "Return a random element from the weighted collection.

  A weighted collection can be any seq of [choice, weight] elements.  The
  weights can be arbitrary numbers -- they do not need to add up to anything
  specific.

  Examples:
  (rand-nth-weighted [[:a 0.50] [:b 0.20] [:c 0.30]])
  (rand-nth-weighted {:a 10 :b 200})
  "
  [coll]
  (let [total (reduce + (map second coll))]
    (loop [i (rand total)
           [[choice weight] & remaining] (seq coll)]
      (if (>= weight i)
        choice
        (recur (- i weight) remaining)))))

(defmacro rand-sexp
  "Select and run random s-expression"
  [& sexps]
  (letfn [(wrap-fn [sexp] `(fn [] ~sexp))]
    `((rand-nth [~@(map wrap-fn sexps)]))))<commit_msg>rand-sexp without function wrapping<commit_after>(ns mokkameister.random)

(defn rand-nth-weighted
  "Return a random element from the weighted collection.

  A weighted collection can be any seq of [choice, weight] elements.  The
  weights can be arbitrary numbers -- they do not need to add up to anything
  specific.

  Examples:
  (rand-nth-weighted [[:a 0.50] [:b 0.20] [:c 0.30]])
  (rand-nth-weighted {:a 10 :b 200})
  "
  [coll]
  (let [total (reduce + (map second coll))]
    (loop [i (rand total)
           [[choice weight] & remaining] (seq coll)]
      (if (>= weight i)
        choice
        (recur (- i weight) remaining)))))

(defmacro rand-sexp
  "Select random s-expression"
  [& sexps]
  `(case (rand-int ~(count sexps))
     ~@(apply concat (map-indexed list sexps))))
{'fraction_non_alphanumeric': 0.14486279514996808, 'fraction_numerical': 0.017868538608806637, 'mean_word_length': 3.0412371134020617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '908490', 'n_tokens_mistral': 575, 'n_tokens_neox': 534, 'n_words': 186}