Getting the source range
Peter Alexander
peter.alexander.au at gmail.com
Wed Dec 5 16:30:48 PST 2012
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?
More information about the Digitalmars-d
mailing list