Clojure macro to collect strings in constant - macros

I've got a Clojure file with a lot of string constants. I want to collect these strings in a collection by wrapping them in a macro. After several tries I succeeded, but my solution looks fairly hideous.
(ns Memorable)
(def MEMORY (atom []))
(defmacro memorize [s] (swap! MEMORY conj s) s)
(prn (str (memorize "hello") " brave new " (memorize "world"))) ; test
(defmacro make-memories-constant [] `(def MEMORIES ~(deref MEMORY)))
(make-memories-constant)
Are there more elegant solutions for this problem?

As far as code cleanup is concerned, I'd remove the make-memories-constant macro -- you can just do
(def MEMORIES #MEMORY)
or even
(def MEMORY #MEMORY)
so as not to clutter your namespace with another Var. As long as you put this after all calls to memorize, this will save a snapshot of MEMORY with all memorized strings inside.
I'd say this is actually pretty clean, with the actual code executing at runtime no different from what it would have been without any memorize-ing...
Another approach would be to prepare a sort of "memorisation framework" as a separate namespace exporting a macros called setup-memorization, say, which could look something like this (just a rough sketch which won't work without some polishing... updated with a (not so thoroughly) tested version -- this actually works!... still, please tweak it to your needs):
(ns memorization-framework)
(defmacro setup-memorization []
(let [MEMORY (gensym "MEMORY")
MEMORIES (gensym "MEMORIES")]
`(do (def ~MEMORY (atom []))
(defmacro ~'memorize [s#] (swap! ~MEMORY conj s#) s#)
(defmacro ~'get-memory [] #~MEMORY)
(defmacro ~'defmemories [~MEMORIES]
`(do (def ~~MEMORIES #~~MEMORY)
(ns-unmap ~~*ns* '~'~'get-memory)
(ns-unmap ~~*ns* '~'~'memorize)
(ns-unmap ~~*ns* '~'~MEMORY)
~~MEMORIES)))))
Then you'd use the memorization-framework namespace, do (setup-memorization) towards the top of your namespace to set things up, call memorize like you do in your example code to memorise things and finally use end-memorization defmemories to store the string collection somewhere in a Var and remove the temporary Var used to store the atom used for construction-time storage along with the ability to make further calls to memorize from this namespace. (The end-memorization macro is meant to just return the collection to wherever you call it from when called with no arguments or define a new Var to store it if given a single argument, which must be a symbol to be used to name the Var. Update: I've only tested the defmemories version, so I'm leaving it here and removing the "in-place return" variant.)
An example interaction at the REPL while in namespace foo (note I defined setup-memorization in the user namespace):
foo> (user/setup-memorization)
#'foo/defmemories
foo> (get-memory)
[]
foo> (memorize "foo")
"foo"
foo> (memorize "bar")
"bar"
foo> (get-memory)
["foo" "bar"]
foo> (defmemories quux)
["foo" "bar"]
foo> quux
["foo" "bar"]
foo> (get-memory)
; Evaluation aborted.
foo> (memorize)
; Evaluation aborted.
Those "Evaluation aborted." messages indicate that calls to get-memory and memorize throw exceptions after defmemories has been called. This is by design, as mentioned above. Also, get-memory is here mostly to ease testing / debugging.
If you're only going to use all this once, the setup-memorization approach might be overkill, but I guess it'll remove some boilerplate for you if you use it more.

Related

Nested quotion and expanding

Lets say i have fn begin: (begin "hello"). The result of `(begin "hello") will be (my-ns/begin "hello"). It's good. But now I do this: (def x '(begin "hello")). How can I expand x with backquote to get (my-ns/begin "hello"), not just (begin "hello")?
In the first example you used ` which is a reader macro named "syntax-quote", In the second example you used ' Which is a reader macro named "quote". Syntax-quote offers a couple of features beyond what quote offers:
unquoting with ~, ~#, etc
namespace expansion of symbols for use in writing hygenic macros.
Plain old quote does neither of these. So if you want namespace expansion in both examples use ` in both places. If you want to get an un-namespaced symbol in a syntax-quooted list you can use both of them together `(println ~'symbol-name) which will evaluate to simply (println symbol-name). (in this case symbol-name needs to be defined in the namespace where this is called (this process in known as "symbol capture")). The syntax-quote evaluates first to a call to quote, when in turn evaluates to the symbol.
If you are looking to expand a symbol that already exists for some reason, or you want to expand it in several different namespaces that both define it, you can use the ns-resolve function:
(ns-resolve *ns* 'begin)
so in your example you can map this over the list to print it with the NS qualified symbols:
user> (map #(if (symbol? %) (ns-resolve *ns* %) %) x)
(#'user/begin "hello")
Though this is not exactly the desired output because it refers to the var in that namespace rather than the symbol that resolves to this var. Things get a little dicey here because ` is a reader macro and we can't build calls to it in other macros. For instance I can think of no way to write:
(defmacro expand-x [thing]
`(syntax-quote-here ~thing)
Because as far as I know syntax-quote doesn't have a name (again because it is a reader macro) so there is nothing to use in place of syntax-quote-here in the above example. Though never fear, in a language with eval nothing is impossible, things just get arbitrarily ugly:
user> (eval (read-string (str "`" x)))
(user/begin "hello")
PS: don't actually use this last example or gremlins will inhabit your code for all time

IllegalStateException: Attempting to call unbound fn in macro

I'm trying to write a macro that calls some functions. The functions should only be used by the macro, so I put them inside of a letfn wrapping the macro. Pseudocode:
(letfn [(fn-a [] ...)
(fn-b [] ...)
(fn-c [] (fn-b))]
(defmacro my-macro [stuff]
`(let [whatever# (fn-a)]
(fn-c))))
The calls to fn-a and fn-c work, but when fn-c tries to call fn-b I get IllegalStateException: Attempting to call unbound fn: #'name.space/fn-b. Why is that?
If I put fn-b and fn-c in their own defns, everything works. But I don't want to do that because it isn't clean.
Edit: Just to test, I tried putting the function bindings in the inner let but ran into the same exception.
I don't think this can work at all this way.
The call to fn-c for example gets expanded to your.namespace/fn-c, so your code seems to call other functions that just happen to have the same names.
But you dont have a your.namespace/fn-b, which raises the exception.
To refer to a unqualified symbol you need to quote and unquote it: ~'fn-a
But this won't work either because the local functions aren't defined at the point of expansion, you can only use them for the macro itself.
You either have to define the functions in a namespace and qualify them in the macro or include them in the macroexpansion, which would define them again at each use.
I'm not sure if this is exactly what you're after but if I do:
(letfn [(fn-a [] (println 1))
(fn-b [] (println 2))
(fn-c [] (fn-b))]
(defmacro my-macro [stuff]
`(let [whatever# ~(fn-b)]
~(fn-c))))
then it works - it just needs the tilde to unquote the function calls.
Both of the following work:
(defn fn-x [] (println 1))
(defn fn-y [] (fn-x))
(defmacro my-macro2 [stuff]
`(let [whatever# (fn-x)]
(fn-y)))
AND
(defn fn-x [] (println 1))
(defn fn-y [] (fn-x))
(defmacro my-macro2 [stuff]
`(let [whatever# ~(fn-x)]
~(fn-y)))
In the first case, the functions are being evaluated and their results incorporated into the macro at compile time whereas in the second they are being evaluated at runtime. With the letfn (which is a macro itself) the results are not available at compile time (presumably because they are being compiled after your macro is compiled) so the functions can only be used at runtime.

Can I get rid of these eval?

I'd like to build a list of operations to execute in a Redis pipeline. I was using accession since it's much simpler than carmine, but now I need connection pooling (missing from accession) and thus I'm looking at carmine again (it seems the go-to and most complete clojure library for redis)
I managed to get this working:
; require [taoensso.carmine :as redis]
(defn execute1 [request] (redis/wcar {} (eval request)))
(defmacro execute [body] `(redis/wcar {} ~#(eval body)))
(def get1 `(redis/get 1))
(execute1 get1)
(execute [get1])
but given that I'll build vectors of thousands of elements, I'm a bit worried about the possible performance hit of eval (besides the fact that I've been taught to always avoid eval if possible). I reckon that defmacro instead is/can be evaluated at macro-expansion time, which might be earlier (during AOT compilation?) and without using eval.
Is there anything that I can do? should I move to a different library?
(I had a look at carmine's source code: my pain is only due to a small convenience for the author: a *context* used to avoid passing around a couple extra argument: getting rid of it should be simple enough, but I'm not invested enough in it... I might as well decide to move to another data store in the future)
edit: I've been asked to write an example of what I think is the boilerplate that I'd prefer to avoid writing in my actual code, so: (the following is untested, it's just a POC)
(defn hset [id key val]
#(redis/hset id key val))
(defn hsetnx [id key val]
#(redis/hsetnx id key val))
(defn hincrby [id key increment]
#(redis/hincrby id key increment))
(defn hgetall [id key]
#(redis/hgetall id key))
(defn sadd [id el]
#(redis/sadd id el))
(defn scard [id]
#(redis/scard id))
(defn smembers [id]
#(redis/smembers id))
(defmacro execute [forms]
`(redis/wcar {} ~#(map apply forms)))
; end boilerplate
(defn munge-element [[a b c]]
(conj
(mapcat #(hincrby a :whatever %) b)
(sadd c b)
(hsetnx a c))
(defn flush-queue! [queue_]
(execute queue_)
[])
(defn receive [item]
(if (< (count #queue) 2000)
(swap! queue conj (munge-element item))
(swap! queue flush-queue!)))
Obviously, I could write something like this, but if this was truly the inteneded way to use carmine, these curried functions would be provided along (or instead of) the normal ones. Also a lot of lines could be shaved off by constructing up the defs with syntax quoting, but this is accidental complexity, not inherent the original problem.
The following piece of code does not differ very much from yours:
(defmacro execute [& forms]
`(redis/wcar {} ~#forms))
(defn get1 []
(redis/get 1))
(execute (get1) (get1) ...)
This is basically how carmine should be used (following this suggestion in the README). If it doesn't suit your needs, could you clarify why?
After the question was edited to present more clearly what should be accomplished I think I have a solution for you. You're trying to create a list of statements to be executed at some specific time in the future. To achieve this you're wrapping each statement inside a function and I agree, that's a lot of boilerplate.
But unnecessary. You can get the same result by having a macro that automatically creates thunks for you:
(defmacro statements [& forms]
`(vector
~#(for [f forms]
`(fn [] ~f))))
Now, whatever you pass to this macro will result in a vector of zero-parameter functions that can be evaluated e.g. using:
(defn execute-statements [fns]
(redis/wcar {} (mapv #(%) fns))
And your example turns into something along the lines of:
(defn munge-element [[a b c]]
(statements
(mapcat #(redis/hincrby a :whatever %) b)
(redis/sadd c b)
(redis/hsetnx a c))
(defn flush-queue! [queue_]
(mapv execute-statements queue_)
[])
Edit: This executes every batch in its own pipeline. If you want to do it in a single one, use concat instead of conj to build up your queue (in receive) and (execute-statements queue_) instead of (mapv execute-statements queue_).
Note: IIRC correctly, this:
(redis/wcar {} a [b c]])
returns the same result as this:
(redis/wcar {} a b c)
I.e., carmine collects the results somewhere and always returns a vector for all of them. Even if not, I think you can still avoid your dreaded boilerplate by just tweaking the stuff presented here a little bit.

Use a clojure macro to automatically create getters and setters inside a reify call

I am trying to implement a huge Java interface with numerous (~50) getter and setter methods (some with irregular names). I thought it would be nice to use a macro to reduce the amount of code. So instead of
(def data (atom {:x nil}))
(reify HugeInterface
(getX [this] (:x #data))
(setX [this v] (swap! data assoc :x v)))
I want to be able to write
(def data (atom {:x nil}))
(reify HugeInterface
(set-and-get getX setX :x))
Is this set-and-get macro (or something similar) possible? I haven't been able to make it work.
(Updated with a second approach -- see below the second horizontal rule -- as well as some explanatory remarks re: the first one.)
I wonder if this might be a step in the right direction:
(defmacro reify-from-maps [iface implicits-map emit-map & ms]
`(reify ~iface
~#(apply concat
(for [[mname & args :as m] ms]
(if-let [emit ((keyword mname) emit-map)]
(apply emit implicits-map args)
[m])))))
(def emit-atom-g&ss
{:set-and-get (fn [implicits-map gname sname k]
[`(~gname [~'this] (~k #~(:atom-name implicits-map)))
`(~sname [~'this ~'v]
(swap! ~(:atom-name implicits-map) assoc ~k ~'v))])})
(defmacro atom-bean [iface a & ms]
`(reify-from-maps ~iface {:atom-name ~a} ~emit-atom-g&ss ~#ms))
NB. that the atom-bean macro passes the actual compile-time value of emit-atom-g&ss on to reify-from-maps. Once a particular atom-bean form is compiled, any subsequent changes to emit-atom-g&ss have no effect on the behaviour of the created object.
An example macroexpansion from the REPL (with some line breaks and indentation added for clarity):
user> (-> '(atom-bean HugeInterface data
(set-and-get setX getX :x))
macroexpand-1
macroexpand-1)
(clojure.core/reify HugeInterface
(setX [this] (:x (clojure.core/deref data)))
(getX [this v] (clojure.core/swap! data clojure.core/assoc :x v)))
Two macroexpand-1s are necessary, because atom-bean is a macro which expands to a further macro call. macroexpand would not be particularly useful, as it would expand this all the way to a call to reify*, the implementation detail behind reify.
The idea here is that you can supply an emit-map like emit-atom-g&ss above, keyed by keywords whose names (in symbolic form) will trigger magic method generation in reify-from-maps calls. The magic is performed by the functions stored as functions in the given emit-map; the arguments to the functions are a map of "implicits" (basically any and all information which should be accessible to all method definitions in a reify-from-maps form, like the name of the atom in this particular case) followed by whichever arguments were given to the "magic method specifier" in the reify-from-maps form. As mentioned above, reify-from-maps needs to see an actual keyword -> function map, not its symbolic name; so, it's only really usable with literal maps, inside other macros or with help of eval.
Normal method definitions can still be included and will be treated as in a regular reify form, provided keys matching their names do not occur in the emit-map. The emit functions must return seqables (e.g. vectors) of method definitions in the format expected by reify: in this way, the case with multiple method definitions returned for one "magic method specifier" is relatively simple. If the iface argument were replaced with ifaces and ~iface with ~#ifaces in reify-from-maps' body, multiple interfaces could be specified for implementation.
Here's another approach, possibly easier to reason about:
(defn compile-atom-bean-converter [ifaces get-set-map]
(eval
(let [asym (gensym)]
`(fn [~asym]
(reify ~#ifaces
~#(apply concat
(for [[k [g s]] get-set-map]
[`(~g [~'this] (~k #~asym))
`(~s [~'this ~'v]
(swap! ~asym assoc ~k ~'v))])))))))
This calls on the compiler at runtime, which is somewhat expensive, but only needs to be done once per set of interfaces to be implemented. The result is a function which takes an atom as an argument and reifies a wrapper around the atom implementing the given interfaces with getters and setters as specified in the get-set-map argument. (Written this way, this is less flexible than the previous approach, but most of the code above could be reused here.)
Here's a sample interface and a getter/setter map:
(definterface IFunky
(getFoo [])
(^void setFoo [v])
(getFunkyBar [])
(^void setWeirdBar [v]))
(def gsm
'{:foo [getFoo setFoo]
:bar [getFunkyBar setWeirdBar]})
And some REPL interactions:
user> (def data {:foo 1 :bar 2})
#'user/data
user> (def atom-bean-converter (compile-atom-bean-converter '[IFunky] gsm))
#'user/atom-bean-converter
user> (def atom-bean (atom-bean-converter data))
#'user/atom-bean
user> (.setFoo data-bean 3)
nil
user> (.getFoo atom-bean)
3
user> (.getFunkyBar data-bean)
2
user> (.setWeirdBar data-bean 5)
nil
user> (.getFunkyBar data-bean)
5
The point is reify being a macro itself which is expanded before your own set-and-get macro - so the set-and-get approach doesn't work. So, instead of an inner macro inside reify, you need a macro on the "outside" that generates the reify, too.
Since the trick is to expand the body before reify sees it, a more general solution could be something along these lines:
(defmacro reify+ [& body]
`(reify ~#(map macroexpand-1 body)))
You can also try to force your macro to expand first:
(ns qqq (:use clojure.walk))
(defmacro expand-first [the-set & code] `(do ~#(prewalk #(if (and (list? %) (contains? the-set (first %))) (macroexpand-all %) %) code)))
(defmacro setter [setterf kw] `(~setterf [~'this ~'v] (swap! ~'data assoc ~kw ~'v)))
(defmacro getter [getterf kw] `(~getterf [~'this] (~kw #~'data)))
(expand-first #{setter getter}
(reify HugeInterface
(getter getX :x)
(setter setX :x)))

Avoiding symbol capture when using macros to generate functions (or other macros)

I'm a bit confused as to exactly when symbol capture will occur with clojure macros. Suppose that I have a macro which defines a function from keywords. In this trivial example,
(defmacro foo [keywd1 keywd2] `(defn ~(symbol (name keywd1))
[~(symbol (name keywd2))] (* 2 ~(symbol (name keywd2)))))
I call (foo :bar :baz), and this gets expanded into (defn bar [baz] (* 2 baz)).
So now the question -- can this lead to symbol capture? If so, under what circumstances?
I know that it's preferred to use gensym (e.g. bar#) to prevent symbol capture, but in some cases (not many, but still) I'd like to have a pretty macro-expansion, without the auto-generated symbols.
Bonus question: does the answer change if we are considering a macro that creates macros?
In your example symbol capture does not happen, because provide the variable parts as parameters. So the developer can choose the names himself.
Symbol capture happens when your macro introduces new locals which are not specified by the user. Consider the following (really silly and nonsensical just to show the point) example:
(defmacro foo
[name & body]
`(defn ~name
[~'bar]
(println ~'bar)
~#body))
In this case bar is captured. Now suppose the user has some code like this.
(def bar 5)
(foo baz (* 2 bar))
(baz 7)
This would not give what the would expect. Because the global bar, the user refering to gets shadowed by the local bar introduced by the macro. As you already said: in this case one should use bar# to introduce the local.
So capture is always indicated by ~'. Macro writing macros don't really change that. Just add one more level: ~~'.