MathGroup Archive 1998

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

Search the Archive

Re: variation on Split [comp.soft-sys.math.mathematica]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg12683] Re: variation on Split [comp.soft-sys.math.mathematica]
  • From: "P.J. Hinton" <paulh>
  • Date: Mon, 1 Jun 1998 23:16:54 -0400
  • Organization: "Wolfram Research, Inc."
  • Sender: owner-wri-mathgroup at wolfram.com

> ------ From "Barthelet, Luc" <lucb at ea.com>
> 
> 
> Split[{a,b,c,d,d,e,f,g,g,h,h,h}] return
> {{a},{b},{c},{d,d},{e},{f},{g,g},{h,h,h}}
> 
> What do you have to do to get:
> {{a,b,c},{d,d},{e,f},{g,g},{h,h,h}}

How about a wrapper function for Split that postprocesses the output?

In[1]:= groupedSplit[lst_List]:= 
  Module[ {splt = Split[lst] }, 
    splt //. ({u___,x_List,{y_}, v___}/;
            Apply[UnsameQ ,x]&& Length[y] == 0) :> {u,Join[x,{y}],v} ]

In[2]:= groupedSplit[{a,b,c,d,d,e,f,g,g,h,h,h}]

Out[2]= {{a, b, c}, {d, d}, {e, f}, {g, g}, {h, h, h}}

--
P.J. Hinton	
Mathematica Programming Group		paulh at wolfram.com Wolfram Research,
Inc.			http://www.wolfram.com/~paulh/



  • Prev by Date: How to get PostScript file in text mode.
  • Next by Date: Re: Mathematica's nesting.
  • Previous by thread: Re: How to get PostScript file in text mode.
  • Next by thread: Re: Differential geometry