rolling span and groupBy for lists

Harendra Kumar harendra.kumar at gmail.com
Mon Feb 5 04:37:55 UTC 2018


I was mainly asking if it makes sense to include these functions in
base/Data.List. Since the base package is maintained and ships along with
ghc, and the issues are also raised at ghc trac I thought this is the right
list. I am copying to libraries at haskell.org as well.

-harendra

On 5 February 2018 at 09:53, David Feuer <david at well-typed.com> wrote:

> This is the wrong list. You probably meant to email haskell-cafe or
> perhaps libraries at haskell.org.
>
>
>
> David Feuer
> Well-Typed, LLP
>
> -------- Original message --------
> From: Harendra Kumar <harendra.kumar at gmail.com>
> Date: 2/4/18 10:50 PM (GMT-05:00)
> To: ghc-devs at haskell.org
> Subject: rolling span and groupBy for lists
>
> Hi,
>
> For a small problem, I was looking for a groupBy like function that groups
> based on a predicate on successive elements but I could not find one. I
> wrote these little functions for that purpose:
>
> -- | Like span, but with a predicate that compares two successive elements.
> The
> -- span ends when the two successive elements do not satisfy the predicate.
> rollingSpan :: (a -> a -> Bool) -> [a] -> ([a], [a])
> rollingSpan _ xs@[] = (xs, xs)
> rollingSpan _ xs@[_] = (xs, [])
> rollingSpan p (x1:xs@(x2:_))
>     | p x1 x2 =
>         let (ys, zs) = rollingSpan p xs
>         in (x1 : ys, zs)
>     | otherwise = ([x1], xs)
>
> -- | Like 'groupBy' but with a predicate that compares two successive
> elements.
> -- A group ends when two successive elements do not satisfy the predicate.
> rollingGroupBy :: (a -> a -> Bool) -> [a] -> [[a]]
> rollingGroupBy _ [] = []
> rollingGroupBy cmp xs =
>     let (ys, zs) = rollingSpan cmp xs
>     in ys : rollingGroupBy cmp zs
>
> Are there any existing functions that serve this purpose or is there any
> simpler way to achieve such functionality? If not, where is the right place
> for these, if any. Can they be included in Data.List in base?
>
> Thanks,
> Harendra
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20180205/64851c8d/attachment.html>


More information about the ghc-devs mailing list