(ns iol-ion.utils
  (:require [datomic.api :as dc])
  (:import [java.util UUID]))

(defn random-tempid []
  (str (UUID/randomUUID)))

(defn by
  ([f xs]
   (by f identity xs))
  ([f fv xs]
   (reduce
     #(assoc %1 (f %2) (fv %2))
     {}
     xs)))

(defn pull-many [db read ids ]
  (->> (dc/q '[:find (pull ?e r)
               :in $ [?e ...] r]
             db
             ids
             read)
       (map first)))

(defn remove-nils [m]
  (let [result (reduce-kv
                 (fn [m k v]
                   (if (not (nil? v))
                     (assoc m k v)
                     m
                     ))
                 {}
                 m)]
    (if (seq result)
      result
      nil)))
