Open gampleman opened 5 months ago
My vote is for both "List
" and "return List a
instead of Maybe (List a)
"
I'd advocate for providing both signatures:
getMany : Array Int -> Array a -> Array a
getManyList : List Int -> Array a -> List a
Like @miniBill , I'd tend to not wrap into a Maybe
since:
Nothing
doesn't provide the list of erroneous indicesArray.set
Having the two functions allows the developper to choose what fits most to its use case.
getMany : Array Int -> Array a -> Maybe (Array a)
Like
Array.get
, but retrieves a number of elements all at once.Motivating use case
A list (or array) of indices is a fairly natural way to abstractly store a particular ordering of an array. Once you're done processing, you want to have a way to materialise the ordering.
Rationale
The way one would implement this is relatively straightforward, but mostly marred by excessive
Maybe
handling:or some such. Not terrible, but easy to get the ordering wrong (which maybe I have, I didn't check).
Design alternatives
Maybe
if an index wasn't found. Instead the missing index would simply be ignored. It would be relatively easy to detect that case, since thenArray.length return /= Array.length indices
.