www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Getting the source range

One thing I often find myself in need of is the ability to get 
back at the source range of a range iteration.

Here's a few examples:

// Read a file up until a line containing "END"
string beforeEND = 
readText(filename).splitter('\n').findBefore(["END"])[0].source;

// Find the largest group of characters
string aaac = "bbaaac".group().minPos!("a[1] > b[1]").source;

// Find first number that has different sign from preceding 
numbers
int three = [-1, -2, 
3].map!(sgn).uniq().drop(1).source.source.front;


As far as I'm aware, there's no easy way to do any of the above 
without source.

There's also examples where alternatives are possible, but having 
the source range available enables an alternate approach.

// Find the number whose square is 25 (could be done using 
predicated find)
int five = iota(10).map!("a*a").find(25).source.front;


Has anyone else ever needed something like this? Is it worth 
adding to Phobos?
Dec 05 2012