February 10, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Dunlap | On Monday, 10 February 2014 at 03:14:31 UTC, Jonathan Dunlap wrote: > (disclaimer: I'm new around here) > Is it possible to cycle backwards? If not, what's the best approach? > > Example of some ideal "takeBack" function: > data = cycle([1,2,3][]) > take(data, 4) is [1,2,3,1][] > takeBack(data, 4) would be [1,3,2,1][] > > Thoughts? Probably what you're looking for: http://dlang.org/phobos/std_range.html#.retro |
February 10, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Dunlap | On Monday, 10 February 2014 at 03:14:31 UTC, Jonathan Dunlap wrote: > (disclaimer: I'm new around here) > Is it possible to cycle backwards? If not, what's the best approach? import std.algorithm; import std.array; import std.range; import std.stdio; void main(string[] args) { auto data = [1,2,3]; assert(data.cycle.take(5).array == [1,2,3,1,2]); assert(data.retro.cycle.take(5).array == [3,2,1,3,2]); } |
February 10, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | On Mon, 2014-02-10 at 09:16 +0000, Gary Willoughby wrote: > On Monday, 10 February 2014 at 03:14:31 UTC, Jonathan Dunlap wrote: > > (disclaimer: I'm new around here) > > Is it possible to cycle backwards? If not, what's the best > > approach? > > import std.algorithm; > import std.array; > import std.range; > import std.stdio; > > void main(string[] args) > { > auto data = [1,2,3]; > > assert(data.cycle.take(5).array == [1,2,3,1,2]); > assert(data.retro.cycle.take(5).array == [3,2,1,3,2]); > } This is why people should be using D instead of C++! This really needs to get onto the D website somewhere. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder@ekiga.net 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 |
February 10, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russel Winder | Russel Winder:
>This really needs to get onto the D website somewhere.
retro+cycle is very simple code, you can also combine them:
alias retroCycle = compose!(cycle, retro);
Ranges and algorithms can be combined together in so many ways :-) For an imperative/OO programmer writing code based on lazy ranges and higher order functions is a new kind of programming that should be learnt patiently, but it's not hard and it doesn't contain many low-level pitfalls :-)
Bye,
bearophile
|
February 11, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | Wow! This is GREAT stuff. My use-case is slightly more complex, and I'm not sure how to best apply this knowledge. The retro reverses the array which is problematic in itself as well as losing the starting index location. I have an array that I'd like to elegantly "rotate". Best way I can show this is by example of an imaginary rotate function:
auto data = [1,2,3];
assert( data.cycle.rotate(2) == [3,1,2] );
assert( data.cycle.rotate(-2) == [2,3,1] );
Perhaps what I'm doing is too complex requires me making my own iterator or something. In my quest of writing readable efficient code, I'm wondering what's the best route here. Thanks :)
On Monday, 10 February 2014 at 09:16:31 UTC, Gary Willoughby wrote:
> void main(string[] args)
> {
> auto data = [1,2,3];
>
> assert(data.cycle.take(5).array == [1,2,3,1,2]);
> assert(data.retro.cycle.take(5).array == [3,2,1,3,2]);
> }
|
February 11, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Dunlap | > auto data = [1,2,3]; > assert( data.cycle.rotate(2) == [3,1,2] ); > assert( data.cycle.rotate(-2) == [2,3,1] ); It's not of immediate help, but it might trigger other answers. Matlab offers this for multi-dimensional arrays: http://www.mathworks.nl/help/matlab/ref/circshift.html |
February 11, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Dunlap | On Tuesday, 11 February 2014 at 03:10:02 UTC, Jonathan Dunlap wrote:
> Wow! This is GREAT stuff. My use-case is slightly more complex, and I'm not sure how to best apply this knowledge. The retro reverses the array which is problematic in itself as well as losing the starting index location. I have an array that I'd like to elegantly "rotate". Best way I can show this is by example of an imaginary rotate function:
>
> auto data = [1,2,3];
> assert( data.cycle.rotate(2) == [3,1,2] );
> assert( data.cycle.rotate(-2) == [2,3,1] );
>
> Perhaps what I'm doing is too complex requires me making my own iterator or something. In my quest of writing readable efficient code, I'm wondering what's the best route here. Thanks :)
>
> On Monday, 10 February 2014 at 09:16:31 UTC, Gary Willoughby wrote:
>> void main(string[] args)
>> {
>> auto data = [1,2,3];
>>
>> assert(data.cycle.take(5).array == [1,2,3,1,2]);
>> assert(data.retro.cycle.take(5).array == [3,2,1,3,2]);
>> }
data.cycle.rotate(-2) == data.cycle(data.length + (-2 % data.length))
I guess you can implement your rotate function with this in mind.
|
February 11, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrea Fontana | On Tuesday, 11 February 2014 at 09:10:16 UTC, Andrea Fontana wrote:
> On Tuesday, 11 February 2014 at 03:10:02 UTC, Jonathan Dunlap wrote:
>> Wow! This is GREAT stuff. My use-case is slightly more complex, and I'm not sure how to best apply this knowledge. The retro reverses the array which is problematic in itself as well as losing the starting index location. I have an array that I'd like to elegantly "rotate". Best way I can show this is by example of an imaginary rotate function:
>>
>> auto data = [1,2,3];
>> assert( data.cycle.rotate(2) == [3,1,2] );
>> assert( data.cycle.rotate(-2) == [2,3,1] );
>>
>> Perhaps what I'm doing is too complex requires me making my own iterator or something. In my quest of writing readable efficient code, I'm wondering what's the best route here. Thanks :)
>>
>> On Monday, 10 February 2014 at 09:16:31 UTC, Gary Willoughby wrote:
>>> void main(string[] args)
>>> {
>>> auto data = [1,2,3];
>>>
>>> assert(data.cycle.take(5).array == [1,2,3,1,2]);
>>> assert(data.retro.cycle.take(5).array == [3,2,1,3,2]);
>>> }
>
> data.cycle.rotate(-2) == data.cycle(data.length + (-2 % data.length))
> I guess you can implement your rotate function with this in mind.
I missed a .rotate after data.cycle, of course.
|
February 11, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Dunlap | On Tuesday, 11 February 2014 at 03:10:02 UTC, Jonathan Dunlap wrote: > Wow! This is GREAT stuff. My use-case is slightly more complex, and I'm not sure how to best apply this knowledge. The retro reverses the array which is problematic in itself as well as losing the starting index location. I have an array that I'd like to elegantly "rotate". Best way I can show this is by example of an imaginary rotate function: > > auto data = [1,2,3]; > assert( data.cycle.rotate(2) == [3,1,2] ); > assert( data.cycle.rotate(-2) == [2,3,1] ); > > Perhaps what I'm doing is too complex requires me making my own iterator or something. In my quest of writing readable efficient code, I'm wondering what's the best route here. Thanks :) Perhaps something like this? http://dpaste.dzfl.pl/d4b82b0b5cba |
February 11, 2014 Re: Circular Buffer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | On Tuesday, 11 February 2014 at 16:26:06 UTC, Rene Zwanenburg wrote: > On Tuesday, 11 February 2014 at 03:10:02 UTC, Jonathan Dunlap wrote: >> Wow! This is GREAT stuff. My use-case is slightly more complex, and I'm not sure how to best apply this knowledge. The retro reverses the array which is problematic in itself as well as losing the starting index location. I have an array that I'd like to elegantly "rotate". Best way I can show this is by example of an imaginary rotate function: >> >> auto data = [1,2,3]; >> assert( data.cycle.rotate(2) == [3,1,2] ); >> assert( data.cycle.rotate(-2) == [2,3,1] ); >> >> Perhaps what I'm doing is too complex requires me making my own iterator or something. In my quest of writing readable efficient code, I'm wondering what's the best route here. Thanks :) > > Perhaps something like this? > http://dpaste.dzfl.pl/d4b82b0b5cba Wait, we can avoid creating that closure and eliminate the map. This should be a bit faster and not use the GC: http://dpaste.dzfl.pl/78c65eacfeb1 |
Copyright © 1999-2021 by the D Language Foundation