commercialhaskell / rio

A standard library for Haskell
Other
836 stars 54 forks source link

fold*1 variants for NonEmpty #240

Open twhitehead opened 2 years ago

twhitehead commented 2 years ago

Would seem logical if RIO.NonEmpty included the NonEmpty specialized fold*1 variants in addition to scan*1.

Don't know for sure, but I expect they weren't included due to the fact they normally come via Data.Foldable and not Data.List.NonEmpty.

snoyberg commented 2 years ago

I don't have an inherent objection, but I'd be concerned about exposing partial versions of these functions. In particular, if we expose the fully generalized versions from Foldable we will accidentally expose a partial function when applied to other data structures. I'm not sure what the best way forward is.

twhitehead commented 2 years ago

Just expose specialized versions would, if nothing else, be consistent with the scan* exports. That is, currently we have

scanl :: Foldable f => (b -> a -> b) -> b -> f a -> NonEmpty b
scanl1 :: (a -> a -> a) -> NonEmpty a -> NonEmpty a

so it would seem logical to do

foldl' :: Foldable f => (b -> a -> b) -> b -> f a -> b
foldl1' :: (a -> a -> a) -> NonEmpty a -> a
snoyberg commented 2 years ago

Sorry I missed the original description. Yes, that seems reasonable, PR welcome.