February 16, 2011 Re: Isn't using find with retro awkward? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jam | On 2/16/11, jam <gr0v3er+d@gmail.com> wrote:
>
> import std.stdio,std.algorithm,std.range,std.container;
>
> void main()
> {
> auto a = [5,1,2,3,4,5,1];
> auto index = countUntil(retro(a),5);
> auto R = retro(take(retro(a),index+1));
> writeln(R);
> R[0] = 6;
> writeln(a);
> }
>
> but this is just getting nutty.
>
>
Nutty, but it's great how much lines you can save when composing ranges. retro and take are both lazy afaik, so this can't be that bad?
|
February 16, 2011 Re: Isn't using find with retro awkward? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Thu, 17 Feb 2011 00:05:15 +0100, Andrej Mitrovic wrote:
> On 2/16/11, jam <gr0v3er+d@gmail.com> wrote:
>>
>> import std.stdio,std.algorithm,std.range,std.container;
>>
>> void main()
>> {
>> auto a = [5,1,2,3,4,5,1];
>> auto index = countUntil(retro(a),5);
>> auto R = retro(take(retro(a),index+1)); writeln(R);
>> R[0] = 6;
>> writeln(a);
>> }
>>
>> but this is just getting nutty.
>>
>>
>>
> Nutty, but it's great how much lines you can save when composing ranges. retro and take are both lazy afaik, so this can't be that bad?
Well take is for sure, but I'm not sure about retro(I didn't see anything in range.d or the online docs that indicate it is). You could save some work by just reversing the container once at the start though.
|
February 17, 2011 Re: Isn't using find with retro awkward? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jam | On Wednesday, February 16, 2011 15:19:01 jam wrote:
> On Thu, 17 Feb 2011 00:05:15 +0100, Andrej Mitrovic wrote:
> > On 2/16/11, jam <gr0v3er+d@gmail.com> wrote:
> >> import std.stdio,std.algorithm,std.range,std.container;
> >>
> >> void main()
> >> {
> >>
> >> auto a = [5,1,2,3,4,5,1];
> >> auto index = countUntil(retro(a),5);
> >> auto R = retro(take(retro(a),index+1)); writeln(R);
> >> R[0] = 6;
> >> writeln(a);
> >>
> >> }
> >>
> >> but this is just getting nutty.
> >
> > Nutty, but it's great how much lines you can save when composing ranges. retro and take are both lazy afaik, so this can't be that bad?
>
> Well take is for sure, but I'm not sure about retro(I didn't see anything in range.d or the online docs that indicate it is). You could save some work by just reversing the container once at the start though.
All retro does is forward front and popFront to back and popBack and back and popBack to front and popFront. So, the only overhead is the extra function call, which is probably inlined anyway.
- Jonathan M Davis
|
February 17, 2011 Re: Isn't using find with retro awkward? | ||||
---|---|---|---|---|
| ||||
Posted in reply to jam | On Wed, 16 Feb 2011 17:58:12 -0500, jam <gr0v3er+d@gmail.com> wrote:
> On Wed, 16 Feb 2011 22:00:13 +0100, Andrej Mitrovic wrote:
>
>> On 2/16/11, jam <gr0v3er+d@gmail.com> wrote:
>>> void main()
>>> {
>>> auto a = [5,1,2,3,4,5,1];
>>> auto index = countUntil(retro(a),5);
>>> writeln(a[a.length-1-index .. a.length]);
>>> }
>>>
>>>
>> That works for random-access ranges.
>> But I was under the impression that bidirectional ranges don't
>> necessarily have a length property?
>
> Doh. That is exactly correct. I guess the following would work for
> bidirectional ranges:
>
> import std.stdio,std.algorithm,std.range,std.container;
>
> void main()
> {
> auto a = [5,1,2,3,4,5,1];
> auto index = countUntil(retro(a),5);
> auto R = retro(take(retro(a),index+1));
> writeln(R);
> R[0] = 6;
> writeln(a);
> }
>
> but this is just getting nutty.
>
This doesn't work, you can't retro take because take is not a bidirectional range. It might work in the case of arrays, but it won't work for something like a string.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation