is there a preferred way to keep a list number of items under a certain limit ?
In my project there is a GenServer module receiving events and stacking them in a list where only the X last one must be kept for analysis.
Currently I have this implementation :
each time an element is pushed in this list, the size of the list is checked and List.delete_at(list, -1) is called in case the limit has been reached.
It’s simple but I’m feeling it could be improved, specially the part where the size of the list is checked at each new event…
I do’t think the queue fits my need since the data structure I’m looking for should easily enable to read multiple items and it does not make sense to loop over the values of a queue. So I’m going to stick to the list associated with an integer referencing the length of the list in combination with Enum.take, thanks @zachallaun.
@krasenyp thanks for sharing that knowledge, it’s always useful on the long term to learn about data structures