digitalmars.D.learn - range of ranges into one range?
- Jonathan Marler (90/90) Jun 25 2017 I'm using the phobos "chain" function to iterate over a set of
- rikki cattermole (2/2) Jun 25 2017 Perhaps?
- Russel Winder via Digitalmars-d-learn (14/17) Jun 25 2017 I think this is yet another case where if Phobos had flatMap life would
- Jonathan Marler (2/4) Jun 26 2017 Thank you.
I'm using the phobos "chain" function to iterate over a set of string arrays. However, one of the variables is actually an array of structs that each contain a string array. So I use "map" to get a range of string arrays, but "chain" expects each variable to be a string array, not a range of string arrays. The solution I came up with was to find a way to convert a range of ranges into one range. I couldn't find this transformation in phobos. Does anyone know if it already exists? It's actually similar to chain itself but chains a range of ranges, not a tuple of ranges. I implemented something that works below, but would love to use something that already exists in phobos. import std.stdio, std.range, std.algorithm; struct MyStrings { string[] strings; } void main() { auto normalStringArray1 = ["a", "b", "c"]; auto normalStringArray2 = ["d", "e", "f"]; auto myStringArrays = [ MyStrings(["g", "h", "i"]), MyStrings(["j", "k", "l"]), ]; foreach(str; chain( normalStringArray1, normalStringArray2, splice(map!(a => a.strings)(myStringArrays)))) { writeln(str); } } auto splice(Range)(Range inputRange) { static struct SplicedRanges(K) { static assert(K.init.empty); Range inputRange; K current; this(Range inputRange) { this.inputRange = inputRange; if(!this.inputRange.empty) { current = this.inputRange.front; if(current.empty) { setCurrentToNext(); } } } private void setCurrentToNext() { while(!inputRange.empty) { inputRange.popFront; if(inputRange.empty) { break; } current = inputRange.front; if(!current.empty) { break; } } } property bool empty() { return current.empty; } property auto front() { return current.front; } void popFront() { if(!current.empty) { current.popFront; if(!current.empty) { return; } } setCurrentToNext(); } } return SplicedRanges!(typeof(inputRange.front))(inputRange); }
Jun 25 2017
On Mon, 2017-06-26 at 07:19 +0100, rikki cattermole via Digitalmars-d- learn wrote:Perhaps? =20I think this is yet another case where if Phobos had flatMap life would be a lot easier. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jun 25 2017
On Monday, 26 June 2017 at 06:19:07 UTC, rikki cattermole wrote:Perhaps?Thank you.
Jun 26 2017