Clojure's threading macros -> and ->> to be quite a win.  It breaks
down when the expression to be chained together are not consistent in
nesting the threaded expression second or last.  An idiomatic way to
gain the necessary flexibility seems to be via let:

(let [x (line-seq x)
      x (sort x)
      ...]
  x)

I've never been very happy with that solution. The same variable
appears multiple times in the same let. Maybe that just confuses my
Scheme sensibilities. (I know there are previously been discussions
about a variant of -> which allows the threading position to be marked
in some way, though these never really went anywhere. I also rejected
the alternative of using an anaphoric macro which always uses 'it or
'$ or some such as the name to thread through. That didn't seem very
Clojuresque.)

I came up with this macro, but I'm unsure what to call it:

(defmacro thread-let [[varname init-expression :as binding] & expressions]
  {:pre [(symbol? varname)
         (not (namespace varname))
         (vector? binding)
         (= 2 (count binding))]}
  `(let [~@(interleave (repeat varname) (cons init-expression expressions))]
     ~varname))

usage example:

(thread-let [x (initial-value)]
    (foo x 3)
    (bar 1 2 x))

which is equivalent to:

(let [x (initial-value)
      x (foo x 3)
      x (bar 1 2 x)]
  x)

What should I name this thing? I'm concerned that "thread" is
confusing due to its dual meaning. let seems in line with clojure
conventions.

(thread-let [x ...] ...)
(thread-with [x ...] ...)
(thread-through [x ...] ...)
(let-> [x ...] ...)

thoughts?

// Ben

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to