You see that for a "continuous span", you can always partition the list into 3 parts as {initial, continuous span, final} by properly choosing the arguments to IPR. This means that you simply need to MapAt your function f onto the second element of the partitioned list and then flatten it.

Thanks! This works great! Just out of curiosity, where is Internal`PartitionRagged documented? Is this a separate package? I can't seem to find PartitionRagged, Internal, or Internal`PartitionRagged in the Mathematica 8 Documentation Center.
–
AndrewJul 13 '13 at 20:28

1

@Andrew Internal` functions are not documented on purpose, but are used extensively in the Mathematica source code and are generally stable enough to be used in one's code and you can expect it to be around in future versions (don't quote me on this though). You learn about the usage of such functions from trial and error or from posts here and on MathGroup
–
rm -rf♦Jul 13 '13 at 21:58

Generic element-grouping function

Several years ago, I wrote a simplistic HTML parser, for which I wrote a generalization of the function you request, that works on different levels of expression, and groups elements at specified start and end positions in specified heads.

Implementation

Here is the code (I made no effort to improve it, so it may not be optimal or the most elegant):

This code was instrumental for the kind of HTML parser I wanted - it was a breadth-first parser that could also (partially) parse certain types of malformed HTML documents. Speed of parsing was important, and the above functions were reasonably fast for my purposes.

Examples

Combining integers in a nested list based on a list of positions

Create a test expression (nested list here, but in general this can be a general expression)

Simple Mathematica FullForm parser

Here will be a less trivial example: parse a string of Mathematica code, given that it represents the FullForm (which makes the task of parsing vastly simpler). Here is one possible implementation for such a parser:

We can now parse this. The idea is to combine together all elements in between the closest pair of opening and closing square brackets of the same depth. This is done in the line with Fold, in the parse function.

At each pass (single iteration in Fold), we process all brackets at the same depth in an expression. The important thing here is that after the first pass, the resulting expression becomes nested, and is no longer a simple list of tokens. Therefore, here we do need the full power of groupElementsNested function to repeatedly combine elements deeper and deeper inside expression being built.

Finally, we can use the parsedToCode function to convert the above result (where all heads are still strings) into Mathematica code:

WRI never put it in Mathematica, and probably never will. It's in the Presentations Application, which I sell.
–
David ParkJul 14 '13 at 21:14

1

Please don't take this the wrong way (I certainly don't object; in fact you can read my argument here), but it doesn't sit well with some users if many references are made to commercial packages such as yours. I think it would go a long way to addressing such objections, while still not undermining your product (since there is a lot more to Presentations` than this), if you would perhaps post the definition of this simple function so that your code can be compared more fairly with the other answers.
–
Oleksandr R.Jul 15 '13 at 4:03

It was the simplest answer to the question posted. My answer was a direct answer to the poster's subsequent question. It is extremely inexpensive compared to its size and capabilities. If you are employed at all I'll wager that you make 1 or 2 orders of magnitude more than I from your technical work. One of the reasons I don't appear much on this group is that if I ever mention Presentations I get a lot of flack. So I do take offense.
–
David ParkJul 15 '13 at 15:00

I actually bought a copy of Presentations and can agree that it is a good package and excellent value for money--thank you for your hard work. I know that people react negatively to it and I do not agree with this at all, so I was trying to make a suggestion that could improve how your answers are received in future given prevailing attitudes. Moreover, I only suggested that because you already posted the definition of MapLevelParts on MathGroup anyway. I am on your side on this issue and certainly did not mean for my comment to come across as pedantic or demanding. If so, I apologize.
–
Oleksandr R.Jul 17 '13 at 11:01

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.