The specificity of bound-fn: capturing specific dynamic vars

13 views
Skip to first unread message

Garth Sheldon-Coulson

unread,
Nov 23, 2009, 3:46:46 PM11/23/09
to clo...@googlegroups.com
Meikel and All,

On the general topic of the recent posts on the "Clojure Scoping Rules" thread:

bound-fn is very nice, but I've found that capturing *all* dynamic vars is often overkill.

In fact, I would argue (subject to persuasion otherwise) that capturing all dynamic vars should be the very rare exception rather than the rule. In most cases, it seems to me that a developer should capture just the dynamic vars that he or she has defined in that particular app, allowing other dynamic vars to reset in new threads as they otherwise would. Another library might expect this resetting of its dynamic vars, or the user might expect it of Clojure's default bindings.

Have you thought about a version of bound-fn in which the user can specify which specific dynamic bindings to capture?

(bound-fn-with [*binding1* *binding2* ...] [args ... ] <body>)

It would just be shorthand for the following code.

(let [binding1 *binding1* binding2 *binding2* ... ]
  (fn [args ...]
    (binding [*binding1* binding1 *binding2* binding2 ... ]
      <body>)))

I use that code enough that I'm getting tired of writing it out. (No, I don't think I'm overusing dynamic vars. Even when I use them very judiciously that piece of code turns up enough to warrant, I think, its own macro.)

Analogously there could be a bound-lazy-seq-with (or whatever name) for defining lazy seqs bound over dynamic vars.

Garth

On Mon, Nov 23, 2009 at 3:21 PM, Graham Fawcett <graham....@gmail.com> wrote:
On Sat, Nov 21, 2009 at 4:37 PM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi,
>
> Am 21.11.2009 um 05:22 schrieb Mark Engelberg:
>
>> Which reminds me, every once in a while I see people talking about
>> this here, and brainstorming up some alternatives to binding that
>> might interact better with lazy data structures.  Has there been any
>> real progress on this, or has every proposed solution been equally
>> problematic?
>
> I wrote up a little blog post on the problem and the possible solutions.
> Feedback welcome.

Very nice. A generalized version might be more useful to your readers:
take an input seq, and return an output seq which is evaluated
stepwise in the binding environment.

Best,
Graham

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Meikel Brandmeyer

unread,
Nov 25, 2009, 4:30:55 PM11/25/09
to clo...@googlegroups.com
Hi,

Am 23.11.2009 um 21:46 schrieb Garth Sheldon-Coulson:

> bound-fn is very nice, but I've found that capturing *all* dynamic
> vars is often overkill.

As I said in said thread: I don't think, that this is a big problem.
The number Vars having actually a non-root binding should be rather
small in the general case. So the saving is not thaaaat large. (For
the case file: I asked exactly this in the assembla ticket, but got no
answer.)

> In fact, I would argue (subject to persuasion otherwise) that
> capturing all dynamic vars should be the very rare exception rather
> than the rule. In most cases, it seems to me that a developer should
> capture just the dynamic vars that he or she has defined in that
> particular app, allowing other dynamic vars to reset in new threads
> as they otherwise would. Another library might expect this resetting
> of its dynamic vars, or the user might expect it of Clojure's
> default bindings.

Hmm... I see your point, but I'm not sure. I think, the environment
should be consisted. If I don't use the other Vars, then there is no
problem. If I use them, I'd expect them to not change under the
code... Maybe I prefer the current solution.

> Analogously there could be a bound-lazy-seq-with (or whatever name)
> for defining lazy seqs bound over dynamic vars.

bound-seq might be a nice complement to bound-fn. Rich?

Sincerely
Meikel

Reply all
Reply to author
Forward
0 new messages