Created
December 13, 2010 19:49
-
-
Save edw/739492 to your computer and use it in GitHub Desktop.
Wildcarded variable subsets in Cascalog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn wildcard? [var] | |
(= "*" (subs (name var) 0 1))) | |
(defn make-output-values [vars] | |
(let [wildcards (filter wildcard? vars) | |
subsets (map set (clojure.contrib.combinatorics/subsets wildcards))] | |
(map #(vec (filter identity | |
(map (fn [var] | |
(if (wildcard? var) | |
(and (contains? % var) | |
(symbol (subs (name var) 1))) | |
var)) | |
vars))) subsets))) | |
(defmacro ?*<- [thunk vs & predicates] | |
(let [ovs (make-output-values vs)] | |
(cons 'do (map (fn [ov] | |
`(?<- (~thunk '~ov) | |
~ov | |
~@predicates)) | |
ovs)))) | |
(def genders (memory-source-tap | |
[["Joe" "Blow" "male"] | |
["Jane" "Blow" "female"] | |
["John" "Doe" "male"] | |
["Janet" "Blow" "female"] | |
["James" "Doe" "male"]])) | |
(def ages (memory-source-tap | |
[["Joe" "Blow" 41] | |
["Jane" "Blow" 35] | |
["John" "Doe" 37] | |
["Janet" "Blow" 19] | |
["James" "Doe" 26]])) | |
(macroexpand-1 | |
(quote (?*<- (fn [vs] | |
(lfs-textline | |
(str "avg-ages/" (clojure.string/join | |
"-" (map #(subs (name %) 1) vs))))) | |
[?avg-age ?count *?lname *?gender] | |
(genders ?fname ?lname ?gender) | |
(ages ?fname ?lname ?age) | |
(c/sum ?age :> ?sum-age) | |
(c/count ?count) | |
(div ?sum-age ?count :> ?avg-age)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment