Arc Forumnew | comments | leaders | submitlogin
4 points by rntz 5255 days ago | link | parent

I've been toying with clojure recently, and there are two macros, -> and ->>, which essentially "thread" expressions through a series of forms, like so:

  (-> x (f a) (g b))
  ; macroexpands to
  (g (f x a) b)
  
  (->> x (f a) (g b))
  ; macroexpands to
  (g b (f a x))
It occurs to me that (magic-)scope is very similar to a reversed-argument form of ->>; let's call it '<<-:

  (<<-
    (w/uniq (foo bar))
    (let xyzzy (+ plugh quux))
    `(,xyzzy (,foo ,bar)))
which is equivalent to (but for this use case, more readable than):

  (->> `(xyzzy (,foo ,bar)) (let xyzzy (+ plugh quux)) (w/uniq (foo bar)))
both of which macroexpand to:

  (w/uniq (foo bar)
    (let xyzzy (+ plugh quux)
      `(,xyzzy (,foo ,bar))))
This <<- form has the advantage of not depending on any sig/binding-function-list* magic, at the expense of a few sets of parentheses. And, as I mentioned, it has a nice symmetry with ->/->>, both of which are useful macros in their own right.