        coll))
 
 (defn- counts-flatten
-  "Flatten COLL by K."
+  "Flatten COLL by K.
+
+    (counts-flatten [{:a [{:b 1} {:b 2}], :c 2}
+                     {:a {:b 3}, :c 4}] :a)
+      -> [{:b 1} {:b 2} {:b 3}]"
   [coll k]
   {:pre [(sequential? coll)
          (keyword? k)]}

              (map var-get)
              (filter (u/fn-> type (= :korma.core/Entity)))
              (filter :hydration-keys)
              (mapcat (fn [{:keys [batched-hydration-keys] :as entity}]
                        (assert (and (set? batched-hydration-keys) (every? keyword? batched-hydration-keys))
                                (str ":hydration-keys should be a set of keywords. In: " entity))
                        (map (u/rpartial vector entity)
                             batched-hydration-keys)))
              (into {}))))

(def ^:private batched-hydration-keys

                 (if (sequential? x)  x
                     [x])))))

(defn- counts-unflatten
  "Unflatten COLL by K using COUNTS from `counts-of`.

    (counts-unflatten [{:b 2} {:b 4} {:b 6}] :a [2 :atom])
      -> [{:a [{:b 2} {:b 4}]}
          {:a {:b 6}}]"
  ([coll k counts]
   (counts-unflatten [] coll k counts))
  ([acc coll k [count & more]]
   (let [[unflattend coll] (condp = count
                             nil   [nil (rest coll)]
                             :atom [(first coll) (rest coll)]
                             :nil  [:nil (rest coll)]
                             (split-at count coll))
         acc (conj acc unflattend)]
     (if-not (seq more) (map (fn [x]
                               (when x
