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

Sequential threading/anaphoric cond-like macro idea

Open alphapapa opened this issue 6 years ago • 7 comments

Note: It's no longer proposed that these macros be named -cond->> and --cond->. See this comment.

Hey, it's me again, another idea. :)

(defmacro -cond->> (test &rest forms)
  (declare (indent defun))
  `(cond ,@(cl-loop for form in forms
                    for this-test = (pcase (car form)
                                      ('t t)
                                      (_ (append test (list (car form)))))
                    collect (list this-test
                                  (cadr form)))))

Used like:

(-cond->> (<= difference)
  (14400 ;; Within past 4 hours
   100)
  (86400 ;; Within last day
   80)
  (259200 ;; Within last 3 days
   60)
  (604800 ;; Within last week
   40)
  (2419200 ;; Within last 4 weeks
   20)
  (7776000 ;; Within last 90 days
   10)
  (t ;; More than 90 days
   0))

Which expands to:

(cond ((<= difference 14400)
       100)
      ((<= difference 86400)
       80)
      ((<= difference 259200)
       60)
      ((<= difference 604800)
       40)
      ((<= difference 2419200)
       20)
      ((<= difference 7776000)
       10)
      (t 0))

Saves a bit of repetition. Could also have a -cond-> form that uses each cond test as the second argument in the test form rather than the last, like the difference between ->> and ->.

alphapapa avatar Sep 06 '17 04:09 alphapapa