Skip to content

Instantly share code, notes, and snippets.

@nathanmarz
Created April 27, 2012 05:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathanmarz/2506020 to your computer and use it in GitHub Desktop.
Save nathanmarz/2506020 to your computer and use it in GitHub Desktop.
Concise inline operations with Cascalog serfn branch
(use 'cascalog.playground) (bootstrap)
(require '[clojure.set :as set])
(require '[cascalog.vars :as v])
(defn all-syms [form]
(if (coll? form)
(reduce (fn [curr elem] (set/union curr (all-syms elem)))
#{}
form)
#{form}))
(defmacro imapcatop [& body]
(let [used-vars (->> body all-syms (filter symbol?) (filter v/cascalog-var?))
impl `(cascalog.api/mapcatop ~(vec used-vars) ~@body)]
`(cascalog.api/predmacro [invars# outvars#]
{:pre [(zero? (clojure.core/count invars#))]}
[[~impl :<< ~(vec (map str used-vars)) :>> outvars#]]
)))
(?<- (stdout) [?word ?count]
(sentence ?sentence)
((imapcatop (.split ?sentence " ")) :> ?word)
(c/count ?count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment