version sorting?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

version sorting?

Javran Cheng
Hi Cafe,

I'm wondering if there's any existing packages that do "version sorting": as an example, for a list like:

["foo-10.20", "foo-1.2", "foo-2.100", "foo-1.12"]

I'd like the result to be:

["foo-1.2", "foo-1.12", "foo-2.100", "foo-10.20"]

I could think of an implementation that turns String elements of a list into [Either Int String] by grouping consecutive chunks using span and isDigit and then do a sortOn:

versionSort :: [String] -> [String]
versionSort = sortOn brkND
  where
    tr f g (x,y) = f x : g y
    -- ND for non-digits
    brkND = tr Right brkD . span (not . isDigit)
    brkD = tr (Left . read @Int) brkND . span isDigit

(side question: does the helper function tr I defined above have a commonly known name?)
just wondering if there are more sophisticated solutions available.

Best,
Javran

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Tom Ellis-5
On Fri, Dec 07, 2018 at 05:47:44PM -0800, Javran Cheng wrote:
> I'm wondering if there's any existing packages that do "version sorting":
> as an example, for a list like:

There's this

    https://hackage.haskell.org/package/versions-3.5.0/docs/Data-Versions.html

and Versioning has an Ord instance.  I don't think it deals with the package
name though.  It should be easy to combine something simple with versioning'
though

    https://hackage.haskell.org/package/versions-3.5.0/docs/Data-Versions.html#v:versioning-39-
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Francesco Ariis
In reply to this post by Javran Cheng
Hello Jarvan,

On Fri, Dec 07, 2018 at 05:47:44PM -0800, Javran Cheng wrote:
> I'm wondering if there's any existing packages that do "version sorting":
> as an example, for a list like:

    https://hackage.haskell.org/package/natural-sort

    λ> :m +Algorithms.NaturalSort
    λ> :m +Data.List
    λ> sortBy (Algorithms.NaturalSort.compare) ["foo-10.20", "foo-1.2", "foo-2.100", "foo-1.12"]
    ["foo-1.2","foo-1.12","foo-2.100","foo-10.20"]


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

MarLinn
In reply to this post by Javran Cheng
versionSort :: [String] -> [String]
versionSort = sortOn brkND
  where
    tr f g (x,y) = f x : g y
    -- ND for non-digits
    brkND = tr Right brkD . span (not . isDigit)
    brkD = tr (Left . read @Int) brkND . span isDigit

(side question: does the helper function tr I defined above have a commonly known name?)

I don't think so, but that's just because you made the cut at a weird point. It's very close to (***) from Control.Arrow, Data.Tuple.Extra, Data.Profunctor, and other libraries, also known as bimap in Data.Bifunctor and in the lens package.

Rewriting your functions to tease it out even more (not tested):

    versionSort :: [String] -> [String]
    versionSort = sortOn gatherNonDigits
      where

        gatherAll predicate gather continue = uncurry (:) . (gather *** continue) . span predicate

        -- 'NonDigits' for non-digits
        gatherNonDigits = gatherAll (not . isDigit) wrapNonDigits gatherDigits
        gatherDigits    = gatherAll        isDigit  wrapDigits    gatherNonDigits

        wrapNonDigits   = Right
        wrapDigits      = Left . read @Int



_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Iustin Pop-3
In reply to this post by Francesco Ariis
On 2018-12-08 11:38:15, Francesco Ariis wrote:

> Hello Jarvan,
>
> On Fri, Dec 07, 2018 at 05:47:44PM -0800, Javran Cheng wrote:
> > I'm wondering if there's any existing packages that do "version sorting":
> > as an example, for a list like:
>
>     https://hackage.haskell.org/package/natural-sort
>
>     λ> :m +Algorithms.NaturalSort
>     λ> :m +Data.List
>     λ> sortBy (Algorithms.NaturalSort.compare) ["foo-10.20", "foo-1.2", "foo-2.100", "foo-1.12"]
>     ["foo-1.2","foo-1.12","foo-2.100","foo-10.20"]

Does it really sort 1.2 before 1.12? (or is it just a copy-paste error)

curious,
iustin
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Francesco Ariis
On Sat, Dec 08, 2018 at 02:31:25PM +0100, Iustin Pop wrote:
> Does it really sort 1.2 before 1.12? (or is it just a copy-paste error)

It does, hence the name of the library!

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Iustin Pop-3
On 2018-12-08 15:08:20, Francesco Ariis wrote:
> On Sat, Dec 08, 2018 at 02:31:25PM +0100, Iustin Pop wrote:
> > Does it really sort 1.2 before 1.12? (or is it just a copy-paste error)
>
> It does, hence the name of the library!

Apologies - given the ".12", I implicitly translated ".2" as ".20". Just
a slow day :)

iustin
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Francesco Ariis
On Sat, Dec 08, 2018 at 03:09:43PM +0100, Iustin Pop wrote:
> Apologies - given the ".12", I implicitly translated ".2" as ".20". Just
> a slow day :)

No worries, I have bitten by "0.x/0.x0" more times than I am willing to
admit :P
-F
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

Javran Cheng
Thanks for the suggestion! natural-sort looks like what I want - I've just taken a quick look and the algorithm is almost the same if I'm not mistaken.

also thanks MarLinn - it's just that as I wrote I realized I've been implemented "tr" over and over again so there might be a common name for it. I do recognize there's a (***) out there
and hoogling (a, [a]) -> [a] doesn't yield any function of interest., didn't bother to go any further to see the rest of it is just a curried (:).
I'd still prefer "tr" though as for me if you think it as a rewrite rule, it's more readable than having a chain of function composition.

Cheers!

On Sat, Dec 8, 2018 at 6:34 AM Francesco Ariis <[hidden email]> wrote:
On Sat, Dec 08, 2018 at 03:09:43PM +0100, Iustin Pop wrote:
> Apologies - given the ".12", I implicitly translated ".2" as ".20". Just
> a slow day :)

No worries, I have bitten by "0.x/0.x0" more times than I am willing to
admit :P
-F
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
Javran (Fang) Cheng

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: version sorting?

David Fox-12
There is an implementation of the debian version sorting algorithm here: http://hackage.haskell.org/package/debian-3.93.2/docs/Debian-Version.html

On Sat, Dec 8, 2018 at 11:40 AM Javran Cheng <[hidden email]> wrote:
Thanks for the suggestion! natural-sort looks like what I want - I've just taken a quick look and the algorithm is almost the same if I'm not mistaken.

also thanks MarLinn - it's just that as I wrote I realized I've been implemented "tr" over and over again so there might be a common name for it. I do recognize there's a (***) out there
and hoogling (a, [a]) -> [a] doesn't yield any function of interest., didn't bother to go any further to see the rest of it is just a curried (:).
I'd still prefer "tr" though as for me if you think it as a rewrite rule, it's more readable than having a chain of function composition.

Cheers!

On Sat, Dec 8, 2018 at 6:34 AM Francesco Ariis <[hidden email]> wrote:
On Sat, Dec 08, 2018 at 03:09:43PM +0100, Iustin Pop wrote:
> Apologies - given the ".12", I implicitly translated ".2" as ".20". Just
> a slow day :)

No worries, I have bitten by "0.x/0.x0" more times than I am willing to
admit :P
-F
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
Javran (Fang) Cheng
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.