Created
December 9, 2017 18:53
-
-
Save carcigenicate/f1fbfd7c0a866f3153595c2ef398be22 to your computer and use it in GitHub Desktop.
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
(ns bits.sql-syntax) | |
(def test-db [{:id 1, :name "A"}, | |
{:id 0, :name "B"}, | |
{:id 3, :name "C"}, | |
{:id 2, :name "D"}, | |
{:id 4, :name "E"}]) | |
(defn where [arg-vec] | |
(let [[k f comp-arg] arg-vec] | |
(fn [elem] (f (k elem) comp-arg)))) | |
(defn select [selection & args] | |
(let [arg-map (into {} (map vec (partition 2 args))) | |
{from :from, where-vec :where, order-key :order-by} arg-map] | |
(if from | |
(let [filt-ordered | |
(cond->> from | |
where-vec (filter (where where-vec)) | |
order-key (sort-by order-key))] | |
(mapcat #(map % filt-ordered) selection))))) | |
(select [:name] :from test-db :where [:id < 2] :order-by :id) | |
; Prints ("B" "A") |
(defmacro select
[vara _ coll _ wherearg _ orderarg]
`(filter
((fn
[[word func arg]]
(~func (~word ~coll) ~arg)
) ~wherearg )
) ~coll
)
You shouldn't be unquoting func
. This runs without error, although I haven't tested if it actually works:
(defmacro select
[vara _ coll _ wherearg _ orderarg]
`(filter
(fn [[word# func# arg#]]
(func# (word# ~coll) arg#))
~wherearg)
~coll)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(defmacro select ; define the macro that acts as a select command
[vara _ coll _ wherearg _ orderarg]
`(filter ;call filter command to select only those entries that match the criteria in where arg. Unquote filter block so it is not executed here
((fn ; The first argument to filter is this internally created function
[[word func arg]] ;which takes three arguments, an identifier(:id), a function(>) and a value(2)
(~func (~word ~coll) ~arg) ;call the passed in function(func) on each id in coll(persons)
) (first ~wherearg) (second ~wherearg) (third ~wherearg) ); the wherearg parameter (:id < 2) is passed into this internally created function
) ~coll ; the second argument to filter is coll(persons)
)