Skip to content

A better foldr1 for Foldable1 #77

@andrewthad

Description

@andrewthad

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions