MathGroup Archive 2006

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

Search the Archive

Re: Insulating data from code

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66529] Re: Insulating data from code
  • From: "J Siehler" <jsiehler at gmail.com>
  • Date: Fri, 19 May 2006 03:39:22 -0400 (EDT)
  • References: <e4ekai$9av$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ray Koopman wrote:
> >From time to time I've wanted to partition the first level of one
> list, say A, the same way that another list, say B, is partitioned.
> One way to do this is
>
> copyPartition[A_List, B_List] /; Length@A >= Length@Flatten@B :=
>                               Module[{i = 0}, Map[A[[++i]]&,B,{-1}]]

Here's a solution, but I'm skeptical that the problem you describe
actually arises.  Module should give you a 'local' i that doesn't
duplicate an other symbol in use.

This function will actually copy structure at deeper levels, as you
see, but if you really want to restrict it to the first level of B,
change the {-1} in the partition to a {2}.

In[1]:=
a=Prime/@Range[30]
b={{x,x,x},{x,x,x,x,x},{x,x},{x,x,x}}
c={{x,x,x},{x,{x,x},x},{{x,x},{x,x},{x,x,x}}}

Out[1]=
{2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,\
103,107,109,113}

Out[2]=
{{x,x,x},{x,x,x,x,x},{x,x},{x,x,x}}

Out[3]=
{{x,x,x},{x,{x,x},x},{{x,x},{x,x},{x,x,x}}}

In[4]:=
partitionedAs[a_,b_]:=ReplacePart[b,a,Position[b,_,{-1},Heads\[Rule]False],
    {#}&/@Range@Length@Flatten@b]

In[5]:=
a~partitionedAs~b

Out[5]=
{{2,3,5},{7,11,13,17,19},{23,29},{31,37,41}}

In[6]:=
a~partitionedAs~c

Out[6]=
{{2,3,5},{7,{11,13},17},{{19,23},{29,31},{37,41,43}}}


--

Here's the restricted version:

In[9]:=
partitionedAs[a_,b_]:=ReplacePart[b,a,Position[b,_,{2},Heads\[Rule]False],
    {#}&/@Range@Length@Flatten@b]

In[11]:=
a~partitionedAs~b
a~partitionedAs~c

Out[11]=
{{2,3,5},{7,11,13,17,19},{23,29},{31,37,41}}

Out[12]=
{{2,3,5},{7,11,13},{17,19,23}}



  • Prev by Date: (Newbie question): New types of numbers
  • Next by Date: Re: fixing contours
  • Previous by thread: Re: Insulating data from code
  • Next by thread: Re: Insulating data from code