Skip to content

Instantly share code, notes, and snippets.

@edw
Created December 13, 2010 19:49
Show Gist options
  • Save edw/739492 to your computer and use it in GitHub Desktop.
Save edw/739492 to your computer and use it in GitHub Desktop.
Wildcarded variable subsets in Cascalog
(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