Hi, -- | 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 _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
This is the wrong list. You probably meant to email haskell-cafe or perhaps [hidden email]. David Feuer Well-Typed, LLP -------- Original message -------- From: Harendra Kumar <[hidden email]> Date: 2/4/18 10:50 PM (GMT-05:00) To: [hidden email] Subject: rolling span and groupBy for lists 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 _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
In reply to this post by Harendra Kumar
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 [hidden email] as well.
-harendra On 5 February 2018 at 09:53, David Feuer <[hidden email]> wrote:
_______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
In reply to this post by Harendra Kumar
I have my own list library with a bunch of things like this. I think
it's what most people do, and some upload them to hackage, e.g. utility-ht or the split package, or data-ordlist. Specifically, I think rollingGroupBy is what I call splitWith: -- | Split @xs@ before places where @f@ matches. -- -- > split_with (==1) [1,2,1] -- > --> [[], [1, 2], [1]] split_with :: (a -> Bool) -> [a] -> NonNull [a] -- ^ output is non-null, and the contents are also, except the first one You can probably find something like this in 'split', or if not, that might be a good place to contribute it. I have a bunch of grouping functions too, which I use all the time, so if there's some kind of general list grouping package then maybe I could put them there. On the other hand, this sort of thing is pretty individual, so it doesn't seem so bad for each person to have their own local library. That way you know it fits your style. Ultimately I think that's why none of the split functions made it into Data.List, every person has a slightly different idea of what it should be. On Sun, Feb 4, 2018 at 7:50 PM, Harendra Kumar <[hidden email]> wrote: > 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 > > _______________________________________________ > ghc-devs mailing list > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs > ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
According to hayoo there seem to be 7 different implementations of this same function. Yours is 8th and mine is 9th and other people may have more not uploaded or maybe the ones that hayoo is not able to find. Does that make a case for including this in some standard place?
-harendra On 5 February 2018 at 12:22, Evan Laforge <[hidden email]> wrote: I have my own list library with a bunch of things like this. I think _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
Why do I suddenly catch a whiff of https://xkcd.com/927/ ? On Mon, Feb 5, 2018 at 1:13 PM, Harendra Kumar <[hidden email]> wrote:
brandon s allbery kf8nh sine nomine associates unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
Yes, I did too :-) But there is a key difference in this case, all these definitions are mathematically equivalent with identical semantics instead of being some fuzzy subjective standards.
-harendra On 5 February 2018 at 23:46, Brandon Allbery <[hidden email]> wrote:
_______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
In reply to this post by Evan Laforge
We have two groups of "leaders", with partially opposing goals. This is a disaster looking for an excuse to happen. On Mon, Feb 5, 2018 at 2:29 PM, Harendra Kumar <[hidden email]> wrote:
brandon s allbery kf8nh sine nomine associates unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net _______________________________________________ ghc-devs mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs |
Free forum by Nabble | Edit this page |