
On Fri, Jan 08, 2021 at 09:44:31PM +0200, Oleg Grenrus wrote:
Note also
-- | @since 4.9.0.0 instance Foldable NonEmpty where foldr f z ~(a :| as) = f a (List.foldr f z as) foldl f z (a :| as) = List.foldl f (f z a) as foldl1 f (a :| as) = List.foldl f a as
-- GHC isn't clever enough to transform the default definition -- into anything like this, so we'd end up shuffling a bunch of -- Maybes around. foldr1 f (p :| ps) = foldr go id ps p where go x r prev = f prev (r x)
-- We used to say -- -- length (_ :| as) = 1 + length as -- -- but the default definition is better, counting from 1. -- -- The default definition also works great for null and foldl'. -- As usual for cons lists, foldr' is basically hopeless.
foldMap f ~(a :| as) = f a `mappend` foldMap f as fold ~(m :| ms) = m `mappend` fold ms toList ~(a :| as) = a : as
Plenty of irrefutable patterns.
Do any of these make "mfix" more usable for NonEmpty? Or are they just superfluous? With just one constructor, is there any downside to an irrefutable pattern? -- Viktor.