On Sat, Feb 5, 2011 at 00:19, Michael Ossareh <ossa...@gmail.com> wrote:
> On Fri, Feb 4, 2011 at 12:05, B Smith-Mannschott <bsmith.o...@gmail.com>
> wrote:
>>
>> 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))
>>
>
> Hah, you have been working on one of my frustrations! Thanks!
> I like (thread-with sym & forms)

I considered that, but decided against it because vector is the
conventional syntax for variable bindings in Clojure (let, fn,
defrecord, ...) Once I'd decided to do that, it became clear that the
first of the forms would become the initialization expression for the
binding.

On the other hand, reusing the vector syntax is not entirely fitting
because it's limited to binding a single symbol without destructuring,
so maybe it's not such a bad idea:

(defmacro thread-with [varname & expressions]
  {:pre [(symbol? varname) (not (namespace varname))]}
  `(let [~@(interleave (repeat varname) (drop-last expressions))]
     ~(last expressions)))

;; e.g.
;;   (thread-with x 2 (+ 1 x) (/ x 2))
;;   => 3/2

Having now broken the notational convention that always puts variable
bindings in a vector once, we might as well break it again by
borrowing let1 from common lisp:

(defmacro let1 [bindable-form init-expression & body]
  `(let [~bindable-form ~init-expression]
     ~@body))

;; e.g.
;;   (let1 x 1 (inc x))
;;   => 2
;; but also supports destructuring
;;   (let1 [x y] [1 2] [y x])
;;   => [2 1]
;; potentially confusing to read?

Though that's neither here nor there.

// 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