Skip to content

Instantly share code, notes, and snippets.

@carcigenicate
Last active December 9, 2017 18:53
Show Gist options
  • Save carcigenicate/d6218f35db46bcad7b68e7071e836237 to your computer and use it in GitHub Desktop.
Save carcigenicate/d6218f35db46bcad7b68e7071e836237 to your computer and use it in GitHub Desktop.
; k for keyword, f for function
(defmacro where [[k f arg]]
`(fn [a#] ; Expand to a function
; that applies k to the map, then applies f to that result, and the passed arg
(~f (~k a#) ~arg)))
(filter (where [:id < 2])
[{:id 0} {:id 1} {:id 2} {:id 3} {:id 4} {:id 5}])
; Returns ({:id 0} {:id 1})
-----
(where [:id < 2])
Expands into
(fn [a] (< (:id a) 2))
@sjg970
Copy link

sjg970 commented Dec 9, 2017

(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)
~wherearg) ; the wherearg parameter (:id < 2) is passed into this internally created function
~coll) ; the second argument to filter is coll(persons)

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