Proposal: add unfoldr1 to the somewhere in base
wren romano
winterkoninkje at gmail.com
Fri Aug 15 04:05:19 UTC 2014
On Fri, Aug 1, 2014 at 7:21 PM, David Feuer <david.feuer at gmail.com> wrote:
> Way back in 2001, Shin-Cheng Mu proposed an unfoldr1 combinator:
> http://code.haskell.org/~dons/haskell-1990-2000/msg06775.html
>
> I discussed this a bit with shachaf in #haskell, and he noted that a
> similar function, with a slightly different but isomorphic type,
> appears in Edward Kmett's semigroups package as the unfoldr for
> NonEmpty.
>
> I propose that we add this. It can be written
>
> unfoldr1 :: (b -> (a, Maybe b)) -> b -> [a]
> unfoldr1 f b = go b
> where
> go b = case f b of
> (a, may_b) -> a : maybe [] go may_b
>
> With the appropriate RULES, it can be wrapped up in build and fuse properly.
>
> I'd love to see this written as an unfoldr instead. Does anyone know
> if that's possible?
unfoldr1 :: (b -> (a, Maybe b)) -> b -> [a]
unfoldr1 f = unfoldr (fmap f) . Just
Note that unless unfoldr is inlined whenever its first argument is
supplied, the use of (Maybe b) as the seed type means you'll get a lot
more allocation and case analysis than in your direct definition.
--
Live well,
~wren
More information about the Libraries
mailing list