purescript/purescript-arrays

Alternative head, init, tail, and index.

beckyconning opened this issue · 6 comments

e.g. head xs <|> x

head :: forall m. (Alternative m) => Array a -> m a

For using with Maybe.

head a <|> Nothing

@paf31 is there a good reason not do this? It seems like a pretty nice idea to me.

I always feel uneasy about replacing Maybe with Alternative, although I don't really know why. I'm happy to replace List with Foldable for example.

I wrote out a bunch of text as a reply, but it just served to confirm that my concerns are irrational 😄 I guess it boils down to whether we think something like perhaps :: forall f a. (Alternative) => Maybe a -> f a is a "good function". Since head etc. here is just the result of fusing the existing function with perhaps, the obvious reason in favour is that it improves laziness when needed.

👍

Given that typeclass resolution can sometimes lead to surprises (ie, getting a different type to the one you expected), I wonder if it would be better to say that people should opt in to this, which they can do with that perhaps function? What other Alternative types might you want for head, incidentally? Eff and Aff are obvious ones, but maybe that's not such a good idea, because empty will give a terrible error message. I think it would be better to use fromJust there, even.

I haven't managed to come up with a concrete example of how a typeclass resolution might lead to a surprise here, admittedly.

I'd rather add a function (Alternative m) => Maybe a -> m a, or more generally choose :: (Foldable f, Alternative m) => f a -> m a. Then choose <<< head is the more general function you describe above.