 ;;        )
 ;;    ))
 
-;; (import java.util.NavigableSet)
-;; (import java.util.TreeSet)
-
 ;; (def #^NavigableSet pivot-years
 ;;      (TreeSet.
 ;;       (list

	 (fn [hook]
	     (applicator
	      (fn [src-model src-key]
		  (println "COUNT(): " src-model src-key tgt-key)
		  (let [tgt-model (do-reduce-count src-model src-key-type src-key tgt-metadata tgt-key)]
		    (insert *metamodel* tgt-model)
		    ;;(connect src-model tgt-model)
		    (hook tgt-model tgt-key)
		    tgt-model))))])))

(defn msplit [chain split-key mutable]
  (fn []
      (let [[downstream-metadata-accessor applicator] (chain)
	    [src-metadata src-key] (downstream-metadata-accessor)]
	[ ;;n get metadata / key
	 (fn [] [src-metadata split-key])
	 ;; apply
	 (fn [hook]
	     (applicator
	      (fn [src-model src-key]
		  (dsplit src-model split-key mutable
			  (fn [tgt-model tgt-key]
			      (insert *metamodel* tgt-model)
			      (hook tgt-model tgt-key)
			      tgt-model)))))
	 ])
      ))

;; next step e.g. (select [a <model.field> b <model.field> c <model.field>] from [model....] where [(= a 10)...]....)

(defn execute [monad]
  ((second (monad)) (fn [model key])))

(def my-whales (mlift whales "my-whales"))

(execute
 (munion
  (mcount 
   (msplit my-whales :type false)
   "count()")
  "Whales.union(count(split(:type)))"))

;;--------------------------------------------------------------------------------


	       (hook tgt-model tgt-key)
	       (applicator
		(fn [src-model src-key]
		    (println "UNION: " src-key src-model)
		    (connect src-model tgt-model)
		    tgt-model))))
	 ])
      ))

