purescript/purescript-lists

Use Alternative constraint instead of Maybe

safareli opened this issue · 2 comments

I just came across total-alternative by @guaraqe.
I think it's pretty interesting way to express partiality of functions.
If we incorporate this technique then, for example type of head will change like this:

-head :: List ~> Maybe
-head Nil = Nothing
-head (x : _) = Just x
+head :: forall m. (Alternative m) => List ~> m
+head Nil = empty
+head (x : _) = pure x

Client will be free to use any alternative for the job. One could even use Array instead of Maybe, not sure but it might also have performance benefits (I think creating Arrays would be cheaper then Maybe objects).

Some foldable functions could also be changed this way

-minimumBy :: forall a f. Foldable f => (a -> a -> Ordering) -> f a -> Maybe a
+minimumBy :: forall a f g. (Alternative g, Foldable f) => (a -> a -> Ordering) -> f a -> g a

https://github.com/guaraqe/total-alternative/blob/master/src/Data/List/Alternative.hs
https://github.com/guaraqe/total-alternative/blob/master/src/Data/Foldable/Alternative.hs

garyb commented

Previous discussion: purescript/purescript-arrays#51

I think this can be closed for the same reason the arrays one was closed (it’s a breaking change for questionable benefit and so we are very unlikely to do it at this point).