In the very early stages of evaluating Nim for a side project - liking what I'm seeing!
But I'd be grateful for any tips about how to meet a key requirement before I invest the time in digging in too deeply.
The app is a trading backtester. So I need to maintain lists of price quotes and indicator values over specific lookback periods for calculating indicators and trading signals. These lists stay in memory for the lifetime of the backtest.
The lists would typically be 10 - 1000 items long when initialised - generally towards the shorter end.
For each new event, I'd be popping the oldest value off the tail, and pushing the newest value into the head.
When I last did this, in .Net, I used a deque library that had a toArray method that was pretty performant and gave me access to the interior values. This worked well.
But the Nim deque library only seems to offer an array -> deque proc. Unless I'm overlooking something, there's no deque->array. I don't understand enough about the collection internals to understand the reason for this.
As this is the central data structure in a performance-sensitive app, I'd need to figure out a good solution. Suggestions would be very welcome.
I'm a total newbie, as I say, so please be kind if this is a dumb question...
deque->array.
Obviously a simple and fast operation. Allocate a seq (Nims dynamic array), iterate over the deque and copy the values into the seq. When the number of items is not too large you may use a fixed size array instead of the seq. Or maybe you can use a seq from the beginning, shifting elements is a fast copy only. 1000 items is really tiny.
Maybe I didn’t understand your question but with Nim deques there is no need to use an array proc to access an element. You can do it directly. Here is an example of what can be done with Nim deques:
import deques
var d = [1, 3, 5, 7].toDeque
echo d
d.addFirst 0
echo d
d.addLast 8
echo d
echo d[3]
d[2] = 9
echo d
echo d.popFirst
echo d.popLast
echo d
d.shrink(1, 1)
echo d
Many thanks - I rather suspected it might be a naive question. I'm only give the docs a quick skim so far and haven't even downloaded yet.
I've never seen a deque with accessible internals before - guess I was just making assumptions.