purescript/purescript-arrays

deleteAt should return Array

user471 opened this issue ยท 5 comments

I think it should be the same as delete and return Array instead of Maybe.
If I care about existence of the index I can use length for checking.

You could also use javascript if you wanted to deal with that kind of nonsense ๐Ÿ˜‰

Any operation that involves an index is prone to human error, so making it hard to accidentally get it wrong is a valuable feature. The workaround is easy too: fromMaybe xs (deleteAt i xs). That makes it clear that you don't care if the deleteAt did nothing.

You could argue the same for delete, but then it's one of those "set-like" functions that I'm not entirely convinced should belong in arrays at all.

I guess so, I wouldn't mind if that returned Maybe too. There's a minor philosophical difference, a key is associated with a particular value whereas an index is just an Int with no particular meaning, so is perhaps more prone to accidental usages.

Could this deleteAt :: Int -> Array -> Array exist anywhere? Maybe in Unsafe?
Personally I only need this version and don't see how the version with Maybe could be useful.

I agree with @garyb - the 'workaround' is easy enough that I don't mind that the other version isn't in this library. Re the map argument, I think arrays are kind of different from maps because if you're passing a specific index to delete at, you probably ought to have some idea of the length of the array, whereas this doesn't apply for maps. Close this?