Re: Insulating data from code
- To: mathgroup at smc.vnet.net
- Subject: [mg66544] Re: Insulating data from code
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Fri, 19 May 2006 03:40:23 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e4ekai$9av$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 5/18/06, Ray Koopman <koopman at sfu.ca> wrote:
> Thanks, J-M, but there seems to be a problem:
>
> In[3]:= A = {a, b, c, d};
> B = {1, 2, {3, 4}};
>
> In[5]:= copyPartition[A,B]
> copyPartition2[A,B]
>
> Out[5]= {a,b,{c,d}}
> Out[6]= {{},{},{a,b}}
>
> Regards,
> Ray
Hi Ray,
That's right. I had not done enough testing before posting. Sorry about
that.
The following version of the function seems to work well as long as
the first list is flat.
In[1]:=
copyPartition3[A_List, B_List] /;
Length[A] >= Length[Flatten[B]] :=
Module[{start, end, lst},
lst = (If[AtomQ[#1], {#1}, #1] & ) /@ B;
start = FoldList[Plus, 1, Length /@ lst];
end = start - 1; start = Most[start];
end = Rest[end];
lst = ((A[[#1]] & ) /@ Range[First[#1],
Last[#1]] & ) /@ Transpose[{start, end}];
MapAt[#1[[1]] & , lst, Position[AtomQ /@ B, True]]]
In[2]:=
A = {a, b, c, d};
B = {1, 2, {3, 4}};
In[4]:=
copyPartition3[A, B]
Out[4]=
{a, b, {c, d}}
In[5]:=
copyPartition[A_List, B_List] /;
Length[A] >= Length[Flatten[B]] :=
Module[{i = 0}, Map[A[[++i]] & , B, {-1}]]
In[6]:=
copyPartition[A, B]
Out[6]=
{a, b, {c, d}}
In[7]:=
A = {a, b, c, d, {e, f}, g, h};
B = {1, {2}, {3, 4}, {5, {6, 7}}};
In[9]:=
copyPartition3[A, B]
Out[9]=
{a, {b}, {c, d}, {{e, f}, g}}
In[10]:=
copyPartition[A, B]
Out[10]=
{a, {b}, {c, d}, {{e, f}, {g, h}}}
Best regards,
Jean-Marc