Public Vars

Back

min-key (clj)

(source)

function

(min-key k x) (min-key k x y) (min-key k x y & more)
Returns the x for which (k x), a number, is least. If there are multiple such xs, the last one is returned.

Examples

typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

cc/max-key (t/All [x] [[x :-> t/Num] (t/+ x) :-> x])
cc/min-key (t/All [x] [[x :-> t/Num] (t/+ x) :-> x])
aliostad/deep-learning-lang-detection
(ns fluxion-test
  (:require [clojure.core.async :refer [chan <!! onto-chan]]
            [fluxion :refer :all]
            [clojure.test :refer :all]
            [clojure.set :as set]
            [clojure.data.generators :as gen]
            [clojure.test.generative :refer [defspec]]
            [clojure.test.generative.runner :as runner]))

;; Do too much math so that missing one beat doesn't mean each
;; subsequent beat is 100% error in the check.
(defspec timer-is-roughly-periodic
  (fn [interval beats]
    (let [t (timer interval)
          ticks (loop [ticks []
                       beats-to-go beats]
                  (if (< 0 beats-to-go)
                    (recur (conj ticks (<!! t))
                           (dec beats-to-go))
                    ticks))
          lt (last ticks)
          target-schedule (take-while #(<= % lt) (iterate (partial + interval) (first ticks)))]
      [ticks target-schedule]))
  [^{:tag (gen/uniform 80 120)} interval ^{:tag (gen/uniform 80 120)} beats]
  (let [ticks (first %)
        target-schedule (nth % 1)
        error (memoize (fn [target observed]
                         (Math/abs (- target observed))))
        errors (into {}
                     (map
                      (fn [[k v]]
                        [k (sort-by last v)])
                      (group-by first
                                (for [target target-schedule]
                                  (let [nearest-tick (apply min-key
                                                            (partial error target)
                                                            ticks)]
                                    [nearest-tick target (error target nearest-tick)])))))
        tick-errors (for [tick ticks]
                      (/ (or (last (first (errors tick)))
                             interval) interval))
        avg-error (/ (apply + tick-errors) (count tick-errors))
        picked-targets  (map #(nth (first %) 1) (vals errors))
        missed-targets (set/difference (set target-schedule)
                                       (set picked-targets))]
    (assert (<= avg-error 1/10) (str "Average error " 1/10 " greater than " 1/10))
    (assert (< (/ (count missed-targets) beats) 1/10)
            (str "Missed target points " (count missed-targets)
                 " greater than " (* 1/10 beats)))))