#Alternative to list.at in this code

1 messages · Page 1 of 1 (latest)

tidal wraith
#

Hi, I've made some working code in gleam but list.at is deprecated for:

Gleam lists are immutable linked lists, so indexing into them is a slow
operation that must traverse the list.

In functional programming it is very rare to use indexing, so if you are
using indexing then a different algorithm or a different data structure is
likely more appropriate.

So I'm curious what alternative approaches to this could look like? Here's the code: https://gist.github.com/u9g/3a2bef2dbccdd3ca67219de97f040008

One approach I thought of is that I could hold the iterator in the state and just cycle it, and on ShouldUpdate I just step it, but I'm curious if there are any other possibilities here?

Gist

GitHub Gist: instantly share code, notes, and snippets.

quiet inlet
#

It's hard to give specific advice without more context but in general the deprecation is meant as a warning/note to think about your data structures and if a list is really the right choice for the problem. in this case it looks like you only ever access values from items at specific indices. would it make sense to use a dict instead?

tidal wraith
tidal wraith
#

yeah I ended up with this, although iterator.index puts the index on the wrong side so I have to swap them, which may cloud the meaning, I'm nopt sure

#

hm, I really wish I could do this with an iterator, but my iterator idea doesn't actually work since we can't step back on an iterator

quiet inlet
#

Yeah an iterator probably wouldn't work well here since you aren't processing each item in sequence and you are already starting with a list anyway