When there's a function that takes a list/array/string and an index, and the index is outside the bounds of the list, some functions will raise, some wrap, some do the thing at the start/end based on where the index is, and some do the same thing regardless of whether its out-of-bounds.
They should all behave the same on all platforms. What should that behaviour be?
My preference would be, in order:
- normalize to valid bound
- normalize with wrapping
- raise an error if out of bounds
- use option/result
- silently return the input for out of bounds
By normalize-to-valid-bound I mean:
let index =
if index < 0
then 0
else min (length, index)
By normalize-with-wrapping I mean:
let index =
if index < 0
then max (length + index), 0
else min (length, index)
When there's a function that takes a list/array/string and an index, and the index is outside the bounds of the list, some functions will raise, some wrap, some do the thing at the start/end based on where the index is, and some do the same thing regardless of whether its out-of-bounds.
They should all behave the same on all platforms. What should that behaviour be?
My preference would be, in order:
By normalize-to-valid-bound I mean:
By normalize-with-wrapping I mean: