Thread
- To: mathgroup at smc.vnet.net
- Subject: [mg53238] Thread
- From: Carlo Teubner <"see signature."@example.com>
- Date: Sun, 2 Jan 2005 04:12:47 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
I'm trying to write a variant of Thread which takes into account
options. That is, I want
ThreadOptions[ f[{1,2}, Opt1 -> {a,b}, Opt2 -> {x,y}] ]
to give
{f[1, Opt1 -> a, Opt2 -> x], f[2, Opt1 -> b, Opt2 -> y]}.
A first approximation I've come up with is
ThreadOptions[f_[args___, opts___?OptionQ]] :=
Thread[f[args, Inner[Rule, Sequence@@Thread[{opts}, Rule], List]]]
Now I get
{f[1, {Opt1 -> a, Opt2 -> x}], f[2, {Opt1 -> b, Opt2 -> y}]}
which is close. I need to get rid of the {}. The following works:
ThreadOptions[f_[args___, opts___?OptionQ]] :=
Thread[f[args, Inner[Rule, Sequence@@Thread[{opts}, Rule], seq]]] /.
seq -> Sequence
However, there is a problem: now f gets evaluated before replacing the
seq with Sequence objects. To get around this, I tried using Unevaluated:
ThreadOptions[f_[args___, opts___?OptionQ]] :=
Unevaluated[Thread[f[args, Inner[Rule, Sequence@@Thread[{opts},
Rule], seq]]] /. seq -> Sequence
But it doesn't work; I get:
Thread::tdlen : Objects of unequal length in f[{1, 2}, {Opt1 -> a,
Opt2 -> x, Opt1 -> b, Opt2 -> y}] cannot be combined.
f[{1, 2}, {Opt1 -> a, Opt2 -> x, Opt1 -> b, Opt2 -> y}]
I assume what's happened is that ReplaceAll has been applied to the
Unevaluated expression, which has not been evaluated.
Is there any way at all of Thread'ing a function, but then not
evaluating it? Are there other ways of creating a ThreadOptions function?
Thanks for any help.
Carlo
--
C Teubner t offline de
dot at minus dot
...oops: "on" not "off" :)