-
Notifications
You must be signed in to change notification settings - Fork 44
Closed
Description
There is not a foldr1
function for Foldable
, but I have something in mind that's more general. Consider the following:
semifoldr :: Semifoldable f => (a -> b -> b) -> (a -> b) -> f a -> b
This function behaves as follows:
semifoldr f g [a,b,c,d] = f a (f b (f c (g d)))
Trivially, we can recover foldr1
with:
foldr1 f = semifoldr f id
Going the other way, recovering semifoldr
from foldr1
is possibly, but it incurs a hefty performance penalty. The really starts to matter when dealing with a data structure like a non-empty set. I originally stumbled across this variant of foldr1
when working with non-empty monomorphic foldable collections, where the type of foldr1
the base
uses is unworkable.
recursion-ninja and josephcsible
Metadata
Metadata
Assignees
Labels
No labels