                     (set (keys attrs2))))
 (defn concatv [& xs]
   (vec (apply concat xs)))
-
-(defn multimap [k+vs]
-  (reduce (fn [acc [k v]] (update-in acc [k] (fnil conj []) v))  {} k+vs))
 
 (defn source? [sym]
   (and (symbol? sym)


(defn -collect
  ([context symbols]
    (let [symbols-map (multimap (map vector symbols (range)))
          rels        (:rels context)
          data        (-collect [(make-array (count symbols))] rels symbols-map)]
      (set (map vec data))))
  ([acc rels symbols-map]
    (if-let [rel (first rels)]
      (let [keep-attrs (vec (intersect-keys (:attrs rel) symbols-map))]
        (if (empty? keep-attrs)
          (recur acc (next rels) symbols-map)
          (let [from+to (map #(array ((:attrs rel) %) (symbols-map %)) keep-attrs)]
            (recur (for [t1 acc
                         t2 (:tuples rel)]
                     (let [res (.slice t1 0)] ;; clone
                       (doseq [idxs from+to
                               :let [from (aget idxs 0)]
                               to   (aget idxs 1)]
                         (aset res to (aget t2 from)))
                       res))
                   (next rels)
                   symbols-map))))
      acc)))

(defn find-attrs [q]

                   ["Medusa" 1]
                   ["Cyclops" 1]
                   ["Chimera" 1] ]]
  (q '[:find (sum ?heads) (min ?heads) (max ?heads) (count ?heads)
       :with ?monster
       :in   [[?monster ?heads]] ]
    monsters))

#_(q '[ :find ?color (max ?amount ?x) (min ?amount ?x)
      :in   [[?color ?x]] ?amount ]
