Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    for item in items.iter() {
      if predicate(item) {
        items.push(modify(item));
      }
    }
This is not a good idea in Python either. You are better off creating a new list in which you accumulate all items, both modified and unmodified.


I don’t think I’ve ever seen code of this shape in general, ever. Lists are homogeneous in their semantics, aka items stand for the same semantic thing. This is in contrast to tuples. Both lists and tuples exist in both Rust and Python as well was a bunch of other languages.

After you’re done with the above iteration, what are you going to do with the items list? It’ll contain a mix of semantically different things (all original entries as well as filtered and modified entries).

For example, say items originally is a list of file names. We want to only process image files (the predicate), and want to normalize paths beforehand (modify). Makes sense. But it requires a separate results list. Processed results cannot go at the end of the original list.

(The idiomatic way would be to process outright and not needlessly allocate and populate new lists, but let’s put that aside)


I've seen it in Python. Not in Rust, because I haven't worked with Rust.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: