Aug-26-2021, 06:40 PM
It turns out that treating the data in your programs as immutable values can be hugely beneficial in terms of how to think about programs - Rich Hickey has an excellent talk on this, titled The Value of Values. It's a shame that we still teach people how to program quite imperatively; most languages these days have a lot of useful abstractions that let you program more declaratively. The problem here can be expressed as: pair up each item with its index (
enumerate
), keep the pairs with the indices that you want (filter
) and then from each pair, select only the items (map
). The list comprehension shown above neatly expresses that. Kevlin Henney has a great talk on programming declaratively, titled Declarative Thinking, Declarative Practice and the Toolz library provides a set of functions to help programming that way, beyond what's in the standard library.