Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Swaping Variables with cl-lib

Common-Lisp in Emacs post series

On today's episode, we again find ourselves admiring the cl package and focus on a very little-known feature of Emacs, psetq. Short for “parallel-setq”, it takes the same syntax as setq, but saves all values before assigning.

For instance, if one needs to swap the variables old and new, assuming you've required cl-lib, you can simply do the following.

(cl-psetq old new
          new old)

And, of course, it has also been generalized to psetf for assigning generalized places.

Update <2014-09-09 Tue>

Truth be told, psetq is more useful when your demands are more sophisticated. When all you need is to swap two variables (or places), the nice and quick solution is cl-rotatef.

(cl-rotatef old new)

Tags: lisp, emacs,

comments powered by Disqus