dash.el icon indicating copy to clipboard operation
dash.el copied to clipboard

Destructive versions of threading macros

Open Luis-Henriquez-Perez opened this issue 6 years ago • 5 comments

Very often I want to update the value of a variable using a threading macro.

(setq cute-var (->> cute-var  (form1) (form2) ...))

So often in fact that the setq cute-var var is beginning to feel tedious and unnecessary. I suspect I'm not the only one. Perhaps creating destructive versions of these macros would be helpful. What do you think?

(->>* cute-var (form1) (form2))

Luis-Henriquez-Perez avatar May 12 '19 00:05 Luis-Henriquez-Perez

Here are some of my implementations. They seem trivial. If you're interested in this idea and want me to make a formal pull request let me know and I'll do so.

(defmacro ->* (x &rest forms)
  `(setq ,x (-> ,x ,@forms)))

(defmacro ->>* (x &rest forms)
  `(setq ,x (->> ,x ,@forms)))

(defmacro -->* (x &rest forms)
  `(setq ,x (--> ,x ,@forms)))

Luis-Henriquez-Perez avatar May 12 '19 18:05 Luis-Henriquez-Perez

Why not use cl-callf?

cireu avatar Aug 11 '19 08:08 cireu

I tried

(let (var)
  (cl-callf '->> var 1 (* 2) (+ 1))
  var)

and I get Debugger entered--Lisp error: (invalid-function '->>)

Fuco1 avatar Aug 13 '19 15:08 Fuco1

On Tue, 13 Aug 2019 23:20:30 +0800, Matus Goljer wrote:

[1 <text/plain; UTF-8 (7bit)>] [2 <text/html; UTF-8 (7bit)>] I tried

(let (var) (cl-callf '->> var 1 (* 2) (+ 1)) var)

and I get Debugger entered--Lisp error: (invalid-function '->>)

― You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

(cl-callf '->> var (* 2) (+ 1)) is wrong usage, you should use

(let ((var 1)) (cl-callf ->> var (* 2) (+ 1)))

cireu avatar Aug 13 '19 15:08 cireu

I see, yes that works. I like it.

Fuco1 avatar Aug 13 '19 15:08 Fuco1