Clojure Macro using filter returns an object reference. Do not know how to interpret this reference

血红的双手。 提交于 2020-01-05 08:39:09

问题


I am defining this macro

seminar.core=> (defmacro select
          #_=>     [vara _ coll _ wherearg _ orderarg]
          #_=>        `(filter ~wherearg))
#'seminar.core/select

And then defining a table

(def persons '({:id 1 :name "olle"} {:id 2 :name "anna"} {:id 3 :name 

"isak"} {:id 4 :name "beatrice"}))

When I try to run my macro, so that I get the columns from the table where the id is greater than 2 (i.e {:id 3 :name "isak"} {:id 4 :name "beatrice"})

    seminar.core=> (select [:id :name] from persons where [> :id 2] orderby :name)

I receive the following message and do not know quite how to interpret it

#object[clojure.core$filter$fn__4808 0x18e53c53 "clojure.core$filter$fn__4808@18e53c53"]

Update

I added a second argument to filter

seminar.core=> (defmacro select
          #_=>     [vara _ coll _ wherearg _ orderarg]
          #_=>        `(filter ~wherearg ~coll))

and receive IllegalArgumentException Key must be integer clojure.lang.APersistentVector.invoke (APersistentVector.java:292) as my return value now. I do not know how to interpret this error


回答1:


When you use macroexpand-1 function to see the expanded form of macro it may give you a clue:

(macroexpand-1 '(select [:id :name] from persons where (> :id 2) orderby :name))
;;=> (clojure.core/filter [> :id 2] persons)

The form [> :id 2] isn't a valid function definition in Clojure. You have to pass proper function to filter, e.g. using anonymous function:

(select [:id :name] from persons where #(> (:id %) 2) orderby :name)
;;=> ({:id 3, :name "isak"} {:id 4, :name "beatrice"})


来源:https://stackoverflow.com/questions/47730081/clojure-macro-using-filter-returns-an-object-reference-do-not-know-how-to-inter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!