How to use out-str-str with collections?

I can use with-out-str to get a string value from (doc func) .

 => (with-out-str (doc first)) "-------------------------\nclojure.core/first\n([coll])\n Returns the first item in the collection. Calls seq on its\n argument. If coll is nil, returns nil.\n" 

However, if I try to do the same with a set of functions, I can only return an empty string for each:

 => (map #(with-out-str (doc %)) [first rest]) ("" "") 

Where am I wrong here?

+4
source share
1 answer

Unfortunately, doc is a macro, and therefore it is not a first-class citizen in clojure because you cannot use it as a function of a higher order.

 user> (doc doc) ------------------------- clojure.repl/doc ([name]) Macro Prints documentation for a var or special form given its name 

what you see is a documentation search result for % twice.

 user> (doc %) nil user> (with-out-str (doc %)) "" 

because the doc call completed execution during macro expansion before the call starts on the map (at runtime). However, you can get the doc string directly from metadata in var containing functions

 user> (map #(:doc (meta (resolve %))) '[first rest]) ("Returns the first item in the collection. Calls seq on its\n argument. If coll is nil, returns nil." "Returns a possibly empty seq of the items after the first. Calls seq on its\n argument.") 
+6
source

Source: https://habr.com/ru/post/1500362/


All Articles