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

Proposal `-<<` (`distribute`)

Open emacksnotes opened this issue 10 months ago • 4 comments

Proposal to introduce -<< (distribute)

-<< looks like a fork in the pipeline, with the same arg getting fed in to multiple forms.

(defmacro -<< (x forms)
  `(list ,@(->> forms
                (--map `(->> ,x ,it)))))

With the above definition, the form

(-<< '(H S L)
     ((--map (intern (format "INDEX-OF-%s-IN-HSLUV" it)))
      (funcall (-compose (-cut -iota <> 0) #'length))))

expands to

(list
 (->> '(H S L) (--map (intern (format "INDEX-OF-%s-IN-HSLUV" it))))
 (->> '(H S L) (funcall (-compose (-cut -iota <> 0) #'length))))

and evaluates to

((INDEX-OF-H-IN-HSLUV INDEX-OF-S-IN-HSLUV INDEX-OF-L-IN-HSLUV)
 (0 1 2))

Here is a non-trivial example,

(->> (-<< '(H S L)
          ((--map (intern (format "INDEX-OF-%s-IN-HSLUV" it)))
           (funcall (-compose (-cut -iota <> 0) #'length))))
     (apply #'-zip-lists))

evaluates to

((INDEX-OF-H-IN-HSLUV 0) (INDEX-OF-S-IN-HSLUV 1)
 (INDEX-OF-L-IN-HSLUV 2))

Two << in -<< signifies that two >>-s are used in it macro expansion. So, -<< distribute at the last position.

emacksnotes avatar Aug 15 '23 04:08 emacksnotes