Skip to content

Instantly share code, notes, and snippets.

@lamf-lamf
Created June 9, 2011 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamf-lamf/1016032 to your computer and use it in GitHub Desktop.
Save lamf-lamf/1016032 to your computer and use it in GitHub Desktop.
Macro Stuff
;;pass in a list of lists. Lists could contains N number of sub lists. Need an expression for each in each part of this function,
(defn do-something [lol]
(let [la (first lol)
lb (second lol)]
(map #(hash-map :a %1 :b %2)
la, lb))
(defprotocol FieldSpecifier
(get-selector [_])
(extract-field [_ node])
(get-key [_]))
;; pass in a list of fields and be able to build selectors and extractors
(defrecord FieldSpec [key selector-key extractor-key]
FieldSpecifier
(get-selector [_] (selectors selector-key))
(extract-field [_ node] ((extractors extractor-key) node))
(get-key [_] key))
(defmacro select-fields [ns fields]
(let [fcoll (var-get (resolve fields))
build-syms (fn [prefix] (map #(-> % get-key gensym) fcoll))
outer-syms (build-syms "outer")
inner-syms (build-syms "inner")
sym-to-sel (interleave outer-syms (map (fn [field] (get-selector field)) fcoll))
hash-vals (interleave (map (fn [field] (get-key field)) fcoll) (map (fn [s] `(extract-field ~s)) inner-syms))]
`(enlv/let-select
~ns [~@sym-to-sel]
(map (fn [~@inner-syms] (hash-map ~@hash-vals))
~@outer-syms)))
(defn select-subs [ns]
(first
(enlv/let-select
ns [titles (selectors :sub-titles)
times (selectors :sub-times)
points (selectors :sub-points)
users (selectors :sub-users)
comments (selectors :sub-com-urls)]
(map #(Submission.
((extractors :text) %1)
((extractors :url) %1)
((extractors :time) %2)
((extractors :num) %3)
((extractors :text) %4)
((extractors :url) %5)
((extractors :num) %5))
titles times points users comments))))
@lamf-lamf
Copy link
Author

Idea is that instead of selecting every field. The caller of select-fields can pass a collection of FieldSpecs and a form will be built containing only the desired fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment