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

--case-> macro?

Open alphapapa opened this issue 7 years ago • 4 comments

I'm still an elisp noob, so this is probably a bad idea and I just don't realize it yet. :) (I did search the issue tracker but I didn't find anything quite like this.)

What do you think about this macro?

(defmacro --case-> (value &rest cond-forms)
  `(let ((it ,value))
     (dolist (form ',cond-forms it)
       (when (eval (car form))
         (setq it (eval (cadr form)))))))

Used like this:

(--case-> (list 1 2 3)
          (t (nreverse it))
          (t (--map (* 2 it) it))) ; => (6 4 2)

I think it could be useful when handling option arguments to a function, where each option has the potential to mutate the result.

I thought that --> with when would work, like this:

(--> (list 1 2 3)
     (if t (nreverse it) it)
     (it t (--map (* 2 it) it) it))

But instead I get an error:

Debugger entered--Lisp error: (void-variable it)
  (mapcar (function (lambda (it) (* 2 it))) it)
  (progn (mapcar (function (lambda (it) (* 2 it))) it))
  (if t (progn (mapcar (function (lambda (it) (* 2 it))) it)))
  eval((if t (progn (mapcar (function (lambda (it) (* 2 it))) it))) nil)

And I don't understand the macro expansion, which leaves out everything except the last form:

(if t
    (progn
      (--map
       (* 2 it)
       it)))

But even if that did work, having to write (if COND FORM IT) instead of (COND FORM) is much more verbose, so it seems like (--case-> ... could still be useful.

There's also this one, which handles multiple forms:

(defmacro --case-multi-> (value &rest cond-forms)
  `(let ((it ,value))
     (dolist (form ',cond-forms it)
       (when (eval (car form))
         (dolist (f (cdr form))
           (setq it (eval f)))))))

Used like:

(--case-multi-> (list 1 2 3)
                (t (nreverse it))
                (t (--map (* 2 it) it)
                   (--map (+ 1 it) it))) ; => (7 5 3)

Thanks for your work on Dash.

alphapapa avatar Oct 15 '16 12:10 alphapapa