DEV Community

loading...

Discussion on: De-throning the List: Summary

Collapse
herteby profile image
Simon Herteby

Excited about not having to think about Lists vs Arrays in the future!

Some of the first things I did when learning Elm was hacking together functions for getting items and deleting items by index from a List, because I hadn't looked into the Array package ๐Ÿ˜…Having Arrays as the default would have made the experience much smoother.

Btw, why is there no "delete element at index" function built in? Is it perhaps because it can "fail silently"? Ie:
I have an Array of length 3
I do some math wrong and delete index 5 instead of 2
I get the same Array of length 3 back, with no compiler error.

Collapse
robinheghan profile image
Robin Heggelund Hansen Author

Mostly because itโ€™s inefficient, but that might be fixed in the future

Collapse
herteby profile image
Simon Herteby

Ah I see. Surely it'll be more efficient than people writing their own though. Here's mine ๐Ÿ˜…

remove : Int -> List a -> List a
remove index list =
    list
        |> List.indexedMap Tuple.pair
        |> List.filter (\( i, _ ) -> i /= index)
        |> List.map Tuple.second