Skip to content

Instantly share code, notes, and snippets.

@carcigenicate
Created December 9, 2017 18:53
Show Gist options
  • Save carcigenicate/f1fbfd7c0a866f3153595c2ef398be22 to your computer and use it in GitHub Desktop.
Save carcigenicate/f1fbfd7c0a866f3153595c2ef398be22 to your computer and use it in GitHub Desktop.
(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")
@sjg970
Copy link

sjg970 commented Dec 9, 2017

(defmacro select
[vara _ coll _ wherearg _ orderarg]
`(filter
((fn
[[word func arg]]
(~func (~word ~coll) ~arg)
) ~wherearg )
) ~coll
)

@carcigenicate
Copy link
Author

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