MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Thread

  • To: mathgroup at smc.vnet.net
  • Subject: [mg53249] Re: Thread
  • From: Peter Pein <petsie at arcor.de>
  • Date: Mon, 3 Jan 2005 04:29:26 -0500 (EST)
  • References: <cr8eem$r86$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Carlo Teubner wrote:

> 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
> 
> 
Is this:
In[1]:=
ThreadOptions[f_[args___, opts___?OptionQ]] :=
   Module[{myf},
     Thread[myf[args,
           Inner[Rule, Sequence @@ Thread[{opts}, Rule], seq]]] /.
   {seq -> Sequence, myf -> f}]
In[2]:=
ThreadOptions[f[{1, 2}, Opt1 -> {a, b}, Opt2 -> {x, y}]]
Out[2]=
{f[1, Opt1 -> a, Opt2 -> x], f[2, Opt1 -> b, Opt2 -> y]}

what you want?

-- 
Peter Pein
Berlin


  • Prev by Date: Re: Partial evaulation of function terms
  • Next by Date: Re: Thread
  • Previous by thread: Re: Thread
  • Next by thread: Re: Thread