Skip to content

Instantly share code, notes, and snippets.

@mccraigmccraig
Created September 27, 2012 14:52
Show Gist options
  • Select an option

  • Save mccraigmccraig/3794445 to your computer and use it in GitHub Desktop.

Select an option

Save mccraigmccraig/3794445 to your computer and use it in GitHub Desktop.
wrapping/unwrapping with cascalog defparallelagg
(use 'cascalog.api)
(use 'cascalog.playground)
(require '[clojure.tools.logging :as log])
(require '[clojure.pprint :as pp])
(bootstrap)
(defn vectorvector
[& vals]
[(apply vector vals)])
(def faves [["alice" "cat" "grey"] ["alice" "dog" "white"] ["alice" "mouse" "spotted"] ["bob" "cat" "black"] ["bob" "dog" "brown"]])
(defn choose-first [ & choices] (log/info "***************") (log/info (with-out-str (pp/pprint choices))) (log/info "***************") (first choices))
(defn choose-first-wrap [ & choices] (log/info "***************") (log/info (with-out-str (pp/pprint choices))) (log/info "***************") [(first choices)])
;; FAIL: (choose-first "cat" "grey" "dog" "white")
(defparallelagg choose :init-var #'vector :combine-var #'choose-first)
(??- (<- [?p ?a ?c] (faves ?p ?ana ?ac) (choose ?ana ?ac :> ?a ?c)))
;; FAIL: (choose-first ["cat" "grey"] ["dog" "white"]) (choose-first ["cat" "grey" ["mouse" "spotted"]])
(defparallelagg wrap-choose :init-var #'vectorvector :combine-var #'choose-first)
(??- (<- [?p ?a ?c] (faves ?p ?ana ?ac) (wrap-choose ?ana ?ac :> ?a ?c)))
;; OK: (choose-first-wrap ["cat" "grey"] ["dog" "white"]) (choose-first-wrap ["cat" "grey"] ["mouse" "spotted"])
;; FAIL combining... operation added the wrong number of fields, expected: ['?p', '!__gen27', '!__gen28'], got result size: 2
(defparallelagg wrap-choose-wrap :init-var #'vectorvector :combine-var #'choose-first-wrap)
(??- (<- [?p ?a ?c] (faves ?p ?ana ?ac) (wrap-choose-wrap ?ana ?ac :> ?a ?c)))
;; OK: flattens the aggregation result in a map
(??- (<- [?p ?a ?c] (faves ?p ?ana ?ac)
(wrap-choose-wrap ?ana ?ac :> ?a-and-c)
(flatten ?a-and-c :> ?a ?c)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment