 
 
 
 
 
 
Re: Programming: List Structure Manipulation
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg971] Re: [mg872] Programming: List Structure Manipulation
- From: Allan Hayes <hay%haystack at christensen.cybernetics.net>
- Date: Thu, 4 May 1995 05:14:26 -0400
Xah Y Lee <xyl10060 at fhda.edu> [mg872] Programming: List Structure  
Manipulation
Writes
> Can anyone show me better ways to write the following function?
>
> fun[listA_, styleList_]:=
>   Table[
>      Append[
>          Flatten@{ First@RotateLeft[styleList,t-1] },  listA[[t]]
>      ],
>      {t, Length@listA}
>  ]
>
> Examples:
>
> fun[Range[0,5],  {a}]         returns
>
>{{a, 0}, {a, 1}, {a, 2}, {a, 3}, {a, 4}, {a, 5}}
>
>
>--
>fun[Range[0,5],  {a,b,c}]         returns
>
>{{a, 0}, {b, 1}, {c, 2}, {a, 3}, {b, 4}, {c, 5}}
>
>--
>fun[Range[0,10],  {{a1, a2}, b}]         returns
>
>{{a1, a2, 0}, {b, 1}, {a1, a2, 2}, {b, 3}, {a1, a2, 4}, {b, 5},  
{a1, >a2, 6}, {b, 7}, {a1, a2, 8}, {b, 9}, {a1, a2, 10}}
>
>--
>fun[Range[0,10],  {{a1, a2}, {{b}}, {c1,c2} }]         returns
>
>
>{{a1, a2, 0}, {b, 1}, {c1, c2, 2}, {a1, a2, 3}, {b, 4}, {c1, c2, 5}, 
>   {a1, a2, 6}, {b, 7}, {c1, c2, 8}, {a1, a2, 9}, {b, 10}}
How about
In[1]:=
fun2[listA_, styleList_]:=
   With[{ls = Length[styleList], lA = Length[listA]},
      Flatten/@
   	Transpose[
   	   {	
	      Take[Join@@
	         Table[styleList, {Ceiling[(lA)/ls]}],
		 lA
	      ],
	      listA
	    }
	 ]
   ]
  Append is a very inefficient operation.
  Better to set up a list structure and then reshape it as a whole
 Allan Hayes
 hay at haystack.demon.co.uk

