digitalmars.D.learn - Need help how to get started with D ranges
- Uranuz (32/32) Mar 24 2014 I see that ranges is primitive to organzie universal approach to
- John Colvin (2/34) Mar 24 2014 Have you read this: http://ddili.org/ders/d.en/ranges.html ?
- Uranuz (12/13) Mar 24 2014 Yes I have read it. It's difficult to formulate the question in
- Uranuz (19/19) Mar 24 2014 I have another question. For example I have some range with input
- monarch_dodra (5/18) Mar 24 2014 If you want to be able to *slice*, then you need an
I see that ranges is primitive to organzie universal approach to
write some algorithms. But I'm using algorithms from the library
but I still can't start with writing my own algorithms based on
ranges. For example I have the following function written without
ranges. I want to improve it and make it working with different
types of strings (char[], string, wchar[], etc...). So could
someone give me an example how to rewrite this function in
`range`-style?
//Parses HTML form data
dstring[dstring] parseFormData2(dstring queryStr)
{ size_t LexStart = 0;
dstring curKey;
dstring curValue;
for( size_t i = 0; i < queryStr.length; ++i )
{ if( queryStr[i] == '=' )
{ curKey = queryStr[LexStart..i].idup;
curValue = null;
LexStart = i+1;
}
if( (queryStr[i] == '&') || (i+1 == queryStr.length) )
{ curValue = queryStr[ LexStart .. (i+1 == queryStr.length) ?
++i : i ].idup;
if( curKey.length > 0)
{ result[curKey] = curValue;
//result[curKey] ~= curValue;
}
curKey = null;
LexStart = i+1;
}
}
return result;
}
Mar 24 2014
On Monday, 24 March 2014 at 12:13:43 UTC, Uranuz wrote:
I see that ranges is primitive to organzie universal approach
to write some algorithms. But I'm using algorithms from the
library but I still can't start with writing my own algorithms
based on ranges. For example I have the following function
written without ranges. I want to improve it and make it
working with different types of strings (char[], string,
wchar[], etc...). So could someone give me an example how to
rewrite this function in `range`-style?
//Parses HTML form data
dstring[dstring] parseFormData2(dstring queryStr)
{ size_t LexStart = 0;
dstring curKey;
dstring curValue;
for( size_t i = 0; i < queryStr.length; ++i )
{ if( queryStr[i] == '=' )
{ curKey = queryStr[LexStart..i].idup;
curValue = null;
LexStart = i+1;
}
if( (queryStr[i] == '&') || (i+1 == queryStr.length) )
{ curValue = queryStr[ LexStart .. (i+1 == queryStr.length) ?
++i : i ].idup;
if( curKey.length > 0)
{ result[curKey] = curValue;
//result[curKey] ~= curValue;
}
curKey = null;
LexStart = i+1;
}
}
return result;
}
Have you read this: http://ddili.org/ders/d.en/ranges.html ?
Mar 24 2014
Have you read this: http://ddili.org/ders/d.en/ranges.html ?Yes I have read it. It's difficult to formulate the question in English bu I'l try. In this example I searching for special symbols '&' and '='. So when symbol found I use *save* method of range to remember start of *name* or *value* string of URL encoded param. Then I trying to find next special symbol. And when I found it I need to take a slice and insert in result AA. Should input range be RandomAccess for it? And if it's not random access range should should I append this substring by symbol to some temp variable and then (when delimiter found) insert into Associative Array. So result is that I will implement two different behaviours for InputRange and for RandomAccessRange. Am I right or not?
Mar 24 2014
I have another question. For example I have some range with input data (for example some array). I using method popFront() to iterate to next element. Then with property front I check element if it has some value. Then I *save* it. But how could I get position of this derived range in original range without overhead. As I see it is only possible via additional variable that will keep this position. In this case I can't understand what is the benefit of range over iterators or simply iterating using some integral index. I should admit that I haven't used iterators a lot in C++ so I don't know all of it's possibilities/ advantages. It's why I asking. In my algorithms for parsing some strings I often save positions of beginings of some tokens in order to take a slice and put it into some buffer. But for opearting with them in terms of ranges I need to have RandomAccessRange, because (as far as I understand) only it have ability to take a slice. But with input I will need to operate (save parsed data) element-wise. And this will realocate and slow execution. What is preferable startegy opearating with ranges that will be flexible and productive?
Mar 24 2014
On Monday, 24 March 2014 at 14:12:58 UTC, Uranuz wrote:If you want to be able to *slice*, then you need an `RandomAccessRange` with `hasSlicing`. a RandomAccessRange is a special kind of ForwardRange, which itself is an Input Range.Have you read this: http://ddili.org/ders/d.en/ranges.html ?Yes I have read it. It's difficult to formulate the question in English bu I'l try. In this example I searching for special symbols '&' and '='. So when symbol found I use *save* method of range to remember start of *name* or *value* string of URL encoded param. Then I trying to find next special symbol. And when I found it I need to take a slice and insert in result AA. Should input range be RandomAccess for it? And if it's not random access range should should I append this substring by symbol to some temp variable and then (when delimiter found) insert into Associative Array. So result is that I will implement two different behaviours for InputRange and for RandomAccessRange. Am I right or not?
Mar 24 2014









"Uranuz" <neuranuz gmail.com> 