Created
June 9, 2011 04:02
-
-
Save lamf-lamf/1016032 to your computer and use it in GitHub Desktop.
Macro Stuff
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
;;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)) | |
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
(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))) | |
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 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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.