• sort - C, python, C++, glibc Was: Baby X is bor nagain

    From Michael S@21:1/5 to David Brown on Wed Jun 19 13:17:58 2024
    On Wed, 19 Jun 2024 07:44:44 +0200
    David Brown <david.brown@hesbynett.no> wrote:

    On 18/06/2024 18:39, Malcolm McLean wrote:


    My main experience of Python was that we had some resource files
    which were icons, in matching light and dark themes. The light
    theme had suffix _L followed by extension, and the dark themes had
    _D. And they needed to be sorted alphabetically, except that _L
    should be placed before _D.
    And it didn't take long to get Python to sort the list
    alphabetically, but there seemed no way in to the sort comparision
    function itself. And I had to give up.


    Python "sort" is a bit like C "qsort" (desperately trying to relate
    this to the group topicality) in that you can define your own
    comparison function, and use that for "sort". For simple comparison functions, people often use lambdas, for more complicated ones it's
    clearer to define a function with a name.



    Off topic:
    Indeed, Python sort has option for specifying comparison function, but
    I would not call it very similar to comparison function of C qsort
    since in Python comparison is not applied directly to the record.
    Instead, it applied to the keys that are derived from record.
    Besides, my impression is that in Python sorting by user-supplied
    comparison function is less idiomatic than doing all heavy lifting in user-supplied key function.
    For the case, presented by Malcolm, I'd certainly do it all in key(),
    without custom cmp_to_key(). May be, it's a little less efficient, but significantly easier to comprehend.

    Back to topic:
    C qsort() sucks. They forgot to provide an option for 3rd parameter
    (context) in comparison callback.
    Back to O.T.:
    Python's sort bypasses the problem by allowing lambda as a key()
    function, so it could have visibility of variables of the caller. IMHO,
    it still sucks.
    C++ way, where comparison can be functor, sucks far less. I find it
    less than obvious, but at least all functionality one can ever want is available.

    Back to topic: why C standard committee still didn't add something like
    gnu qsort_r() to the standard?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to Michael S on Wed Jun 19 12:53:21 2024
    On 19/06/2024 12:17, Michael S wrote:
    On Wed, 19 Jun 2024 07:44:44 +0200
    David Brown <david.brown@hesbynett.no> wrote:

    On 18/06/2024 18:39, Malcolm McLean wrote:


    My main experience of Python was that we had some resource files
    which were icons, in matching light and dark themes. The light
    theme had suffix _L followed by extension, and the dark themes had
    _D. And they needed to be sorted alphabetically, except that _L
    should be placed before _D.
    And it didn't take long to get Python to sort the list
    alphabetically, but there seemed no way in to the sort comparision
    function itself. And I had to give up.


    Python "sort" is a bit like C "qsort" (desperately trying to relate
    this to the group topicality) in that you can define your own
    comparison function, and use that for "sort". For simple comparison
    functions, people often use lambdas, for more complicated ones it's
    clearer to define a function with a name.



    Off topic:
    Indeed, Python sort has option for specifying comparison function, but
    I would not call it very similar to comparison function of C qsort
    since in Python comparison is not applied directly to the record.

    Yes, it seems that the comparison function support in sort() was in
    Python 2 but was dropped for Python 3.

    Instead, it applied to the keys that are derived from record.
    Besides, my impression is that in Python sorting by user-supplied
    comparison function is less idiomatic than doing all heavy lifting in user-supplied key function.

    A key function can be applied once per item, while a comparison function
    is called once per comparison - thus key functions will be (or at least
    /can/ be) more efficient.

    For the case, presented by Malcolm, I'd certainly do it all in key(),
    without custom cmp_to_key(). May be, it's a little less efficient, but significantly easier to comprehend.

    Back to topic:
    C qsort() sucks. They forgot to provide an option for 3rd parameter
    (context) in comparison callback.

    It is also not guaranteed to be stable (which is important in some
    contexts), and it is a misnomer - it is rarely a quicksort.

    Back to O.T.:
    Python's sort bypasses the problem by allowing lambda as a key()
    function, so it could have visibility of variables of the caller. IMHO,
    it still sucks.
    C++ way, where comparison can be functor, sucks far less. I find it
    less than obvious, but at least all functionality one can ever want is available.

    The C++ way is also massively more efficient than C in cases where the comparison function is simple. And it is typesafe.


    Back to topic: why C standard committee still didn't add something like
    gnu qsort_r() to the standard?


    That is a little more flexible, but it's still ugly!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)