Loading ...
Sorry, an error occurred while loading the content.

{func} of sort()

Expand Messages
  • mattn
    Hi all. filter() or map() is given {string} expression to compare the items. But sort() is given function name or funcref for that. I hope to give expression
    Message 1 of 23 , May 31, 2013
    • 0 Attachment
      Hi all.

      filter() or map() is given {string} expression to compare the items. But sort() is given function name or funcref for that.
      I hope to give expression to {func}.

      I guess this is useful to call sort().

      :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
      [{'foo': 2}, {'foo': 3}, {'foo': 4}]

      Below is a patch. Please check and include.
      This patch still allow to give string for function name.

      https://gist.github.com/mattn/5684747

      Thanks.
      - Yasuhiro Matsumoto

      --
      --
      You received this message from the "vim_dev" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_dev" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Ingo Karkat
      ... Is this really such a frequent use case? I m reluctant to introduce two more special variables (v:lhs, v:rhs; especially with these very ambiguous (think
      Message 2 of 23 , May 31, 2013
      • 0 Attachment
        On 31-May-2013 15:14 +0200, mattn wrote:

        > Hi all.
        >
        > filter() or map() is given {string} expression to compare the items. But sort() is given function name or funcref for that.
        > I hope to give expression to {func}.
        >
        > I guess this is useful to call sort().
        >
        > :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
        > [{'foo': 2}, {'foo': 3}, {'foo': 4}]
        >
        > Below is a patch. Please check and include.
        > This patch still allow to give string for function name.
        >
        > https://gist.github.com/mattn/5684747

        Is this really such a frequent use case? I'm reluctant to introduce two
        more special variables (v:lhs, v:rhs; especially with these very
        ambiguous (think mappings) names! Why not v:i1 / v:i2?) just for this.

        Rather, I'd love to see an easier way to define anonymous Funcrefs
        inline, as in this imaginary syntax:

        echo sort([{"foo":3},{"foo":2},{"foo":4}], function(i1, i2)
        return a:i1.foo - a:i2.foo
        endfunction)

        That would be even more useful for the mentioned map() and filter(),
        where the double escaping of the expression is really ugly. To
        summarize, I'd rather see this addressed by allowing more uses for
        Funcrefs rather than introducing more occasions for expressions.

        -- regards, ingo

        --
        --
        You received this message from the "vim_dev" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_dev" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • ZyX
        ... I would prefer a patch for adding support for funcrefs in map/filter instead. And it is also bad idea to only overload strings: if you need expressions
        Message 3 of 23 , May 31, 2013
        • 0 Attachment
          пятница, 31 мая 2013 г., 17:14:17 UTC+4 пользователь mattn написал:
          > Hi all.
          >
          > filter() or map() is given {string} expression to compare the items. But sort() is given function name or funcref for that.
          > I hope to give expression to {func}.
          >
          > I guess this is useful to call sort().
          >
          > :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
          > [{'foo': 2}, {'foo': 3}, {'foo': 4}]
          >
          > Below is a patch. Please check and include.
          > This patch still allow to give string for function name.
          >
          > https://gist.github.com/mattn/5684747
          >
          > Thanks.
          > - Yasuhiro Matsumoto

          I would prefer a patch for adding support for funcrefs in map/filter instead.

          And it is also bad idea to only overload strings: if you need expressions here use special value (like "expr") in place of {dict}, add second optional argument or whatever, but **do not silently allow invalid function names taking them as expression**. There already is a lot of places where vim shows strange behavior with otherwise invalid input, don't add yet another one.

          --
          --
          You received this message from the "vim_dev" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_dev" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Yasuhiro MATSUMOTO
          Well, I do not want to define the function each time for sort. This is the opinion of the user. ... -- - Yasuhiro Matsumoto -- -- You received this message
          Message 4 of 23 , May 31, 2013
          • 0 Attachment
            Well, I do not want to define the function each time for sort. This is
            the opinion of the user.

            On 6/1/13, ZyX <zyx.vim@...> wrote:
            > пятница, 31 мая 2013 г., 17:14:17 UTC+4 пользователь mattn написал:
            >> Hi all.
            >>
            >> filter() or map() is given {string} expression to compare the items. But
            >> sort() is given function name or funcref for that.
            >> I hope to give expression to {func}.
            >>
            >> I guess this is useful to call sort().
            >>
            >> :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
            >> [{'foo': 2}, {'foo': 3}, {'foo': 4}]
            >>
            >> Below is a patch. Please check and include.
            >> This patch still allow to give string for function name.
            >>
            >> https://gist.github.com/mattn/5684747
            >>
            >> Thanks.
            >> - Yasuhiro Matsumoto
            >
            > I would prefer a patch for adding support for funcrefs in map/filter
            > instead.
            >
            > And it is also bad idea to only overload strings: if you need expressions
            > here use special value (like "expr") in place of {dict}, add second optional
            > argument or whatever, but **do not silently allow invalid function names
            > taking them as expression**. There already is a lot of places where vim
            > shows strange behavior with otherwise invalid input, don't add yet another
            > one.
            >
            > --
            > --
            > You received this message from the "vim_dev" maillist.
            > Do not top-post! Type your reply below the text you are replying to.
            > For more information, visit http://www.vim.org/maillist.php
            >
            > ---
            > You received this message because you are subscribed to the Google Groups
            > "vim_dev" group.
            > To unsubscribe from this group and stop receiving emails from it, send an
            > email to vim_dev+unsubscribe@....
            > For more options, visit https://groups.google.com/groups/opt_out.
            >
            >
            >


            --
            - Yasuhiro Matsumoto

            --
            --
            You received this message from the "vim_dev" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_dev" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Yasuhiro MATSUMOTO
            In addional, my patch don t remove feature that passing func name . ... -- - Yasuhiro Matsumoto -- -- You received this message from the vim_dev maillist.
            Message 5 of 23 , May 31, 2013
            • 0 Attachment
              In addional, my patch don't remove feature that passing 'func name'.

              On 6/1/13, ZyX <zyx.vim@...> wrote:
              > пятница, 31 мая 2013 г., 17:14:17 UTC+4 пользователь mattn написал:
              >> Hi all.
              >>
              >> filter() or map() is given {string} expression to compare the items. But
              >> sort() is given function name or funcref for that.
              >> I hope to give expression to {func}.
              >>
              >> I guess this is useful to call sort().
              >>
              >> :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
              >> [{'foo': 2}, {'foo': 3}, {'foo': 4}]
              >>
              >> Below is a patch. Please check and include.
              >> This patch still allow to give string for function name.
              >>
              >> https://gist.github.com/mattn/5684747
              >>
              >> Thanks.
              >> - Yasuhiro Matsumoto
              >
              > I would prefer a patch for adding support for funcrefs in map/filter
              > instead.
              >
              > And it is also bad idea to only overload strings: if you need expressions
              > here use special value (like "expr") in place of {dict}, add second optional
              > argument or whatever, but **do not silently allow invalid function names
              > taking them as expression**. There already is a lot of places where vim
              > shows strange behavior with otherwise invalid input, don't add yet another
              > one.
              >
              > --
              > --
              > You received this message from the "vim_dev" maillist.
              > Do not top-post! Type your reply below the text you are replying to.
              > For more information, visit http://www.vim.org/maillist.php
              >
              > ---
              > You received this message because you are subscribed to the Google Groups
              > "vim_dev" group.
              > To unsubscribe from this group and stop receiving emails from it, send an
              > email to vim_dev+unsubscribe@....
              > For more options, visit https://groups.google.com/groups/opt_out.
              >
              >
              >


              --
              - Yasuhiro Matsumoto

              --
              --
              You received this message from the "vim_dev" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_dev" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            • Marc Weber
              Not having read the implemeentation details: using expressions you can have closures , using functions this is harder and requires much more typing. Whether
              Message 6 of 23 , May 31, 2013
              • 0 Attachment
                Not having read the implemeentation details:
                using expressions you can have "closures", using functions this is harder and
                requires much more typing. Whether this is important for sorting, I
                don't know.

                let sort_opts = {'ignore_spaces': 1}

                sort(foo, 'sort_stuff(v:1, v:2, sort_opts)')

                try the same with function references. There are ways to make it work,
                but they are more complicated. Does it matter in real world use cases?

                Marc Weber

                --
                --
                You received this message from the "vim_dev" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php

                ---
                You received this message because you are subscribed to the Google Groups "vim_dev" group.
                To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                For more options, visit https://groups.google.com/groups/opt_out.
              • Bram Moolenaar
                ... Using strings here was just a quick way of making it work. What we would really need is some kind of lambda function. So the argument would be one of
                Message 7 of 23 , May 31, 2013
                • 0 Attachment
                  ZyX wrote:

                  > пятница, 31 мая 2013 г., 17:14:17 UTC+4 пользователь mattn написал:
                  > > Hi all.
                  > >
                  > > filter() or map() is given {string} expression to compare the items. But sort() is given function name or funcref for that.
                  > > I hope to give expression to {func}.
                  > >
                  > > I guess this is useful to call sort().
                  > >
                  > > :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
                  > > [{'foo': 2}, {'foo': 3}, {'foo': 4}]
                  > >
                  > > Below is a patch. Please check and include.
                  > > This patch still allow to give string for function name.
                  > >
                  > > https://gist.github.com/mattn/5684747
                  > >
                  > > Thanks.
                  > > - Yasuhiro Matsumoto
                  >
                  > I would prefer a patch for adding support for funcrefs in map/filter instead.
                  >
                  > And it is also bad idea to only overload strings: if you need
                  > expressions here use special value (like "expr") in place of {dict},
                  > add second optional argument or whatever, but **do not silently allow
                  > invalid function names taking them as expression**. There already is a
                  > lot of places where vim shows strange behavior with otherwise invalid
                  > input, don't add yet another one.

                  Using strings here was just a quick way of making it work. What we
                  would really need is some kind of lambda function. So the argument
                  would be one of three types:
                  expression - mainly for backwards compatibility, but also allows
                  building the functionality from pieces.
                  function name or reference - for bigger functions
                  lamba function - for small functions

                  That could be used in several places.

                  Implementing a lambda function is not trivial though.

                  --
                  Due knot trussed yore spell chequer two fined awl miss steaks.

                  /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
                  /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                  \\\ an exciting new programming language -- http://www.Zimbu.org ///
                  \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

                  --
                  --
                  You received this message from the "vim_dev" maillist.
                  Do not top-post! Type your reply below the text you are replying to.
                  For more information, visit http://www.vim.org/maillist.php

                  ---
                  You received this message because you are subscribed to the Google Groups "vim_dev" group.
                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                  For more options, visit https://groups.google.com/groups/opt_out.
                • ZyX
                  ... It would be better if it removed it. As I said, do make the difference between sort(list, Func ) and sort(list, {expr} ) defined explicitely: sort(list,
                  Message 8 of 23 , May 31, 2013
                  • 0 Attachment
                    пятница, 31 мая 2013 г., 20:39:15 UTC+4 пользователь mattn написал:
                    > In addional, my patch don't remove feature that passing 'func name'.

                    It would be better if it removed it. As I said, do make the difference between sort(list, 'Func') and sort(list, '{expr}') defined explicitely: sort(list, '{expr}', 'expr'); sort(list, '{expr}', 0); sort(list, '{expr}', 0, 1) or whatever but definitely *not* sort(list, '{expr}').

                    I did not argue against the feature: I only said that I would like map() and filter() accept function references more then your extension for sort(). I do argue against exact implementation.

                    --
                    --
                    You received this message from the "vim_dev" maillist.
                    Do not top-post! Type your reply below the text you are replying to.
                    For more information, visit http://www.vim.org/maillist.php

                    ---
                    You received this message because you are subscribed to the Google Groups "vim_dev" group.
                    To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                    For more options, visit https://groups.google.com/groups/opt_out.
                  • ZyX
                    ... It is not very good idea: lambdas are better implemented as the same type as function references; basically the idea is the same as used in python: have
                    Message 9 of 23 , May 31, 2013
                    • 0 Attachment
                      > Using strings here was just a quick way of making it work. What we
                      > would really need is some kind of lambda function. So the argument
                      > would be one of three types:
                      > expression - mainly for backwards compatibility, but also allows
                      > building the functionality from pieces.
                      > function name or reference - for bigger functions
                      > lamba function - for small functions
                      >
                      > That could be used in several places.
                      >
                      > Implementing a lambda function is not trivial though.

                      It is not very good idea: lambdas are better implemented as the same type as function references; basically the idea is the same as used in python: have structure with ob_type (at least with functions returning string used for string() and functions setting typval_T *tv given arguments and self (as tp_call in python)), ob_refcnt (for garbage collecting) and void*arg. Then you can have whatever you want, be it lambdas, proxies for python callable objects, anonymous or user-defined functions.

                      We already want two different candidates for such change: I want python callables called without pyeval() proxy; you want lambdas. With such change these can live together and still count as one funcref type; without it would be 3 types.

                      I can write a patch for this, but not until RFC for python interfaces is finished and implemented.

                      --
                      --
                      You received this message from the "vim_dev" maillist.
                      Do not top-post! Type your reply below the text you are replying to.
                      For more information, visit http://www.vim.org/maillist.php

                      ---
                      You received this message because you are subscribed to the Google Groups "vim_dev" group.
                      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                      For more options, visit https://groups.google.com/groups/opt_out.
                    • Ingo Karkat
                      ... I think you ve once sketched this here: https://groups.google.com/group/vim_dev/msg/d767d19228f5a886?hl=en ... Another benefit would be (as I remember)
                      Message 10 of 23 , May 31, 2013
                      • 0 Attachment
                        On 31-May-2013 21:27 +0200, ZyX wrote:

                        >> Using strings here was just a quick way of making it work. What we
                        >> would really need is some kind of lambda function. So the argument
                        >> would be one of three types:
                        >> expression - mainly for backwards compatibility, but also allows
                        >> building the functionality from pieces.
                        >> function name or reference - for bigger functions
                        >> lamba function - for small functions
                        >>
                        >> That could be used in several places.
                        >>
                        >> Implementing a lambda function is not trivial though.
                        >
                        > It is not very good idea: lambdas are better implemented as the same
                        > type as function references; basically the idea is the same as used in
                        > python: have structure with ob_type (at least with functions returning
                        > string used for string() and functions setting typval_T *tv given
                        > arguments and self (as tp_call in python)), ob_refcnt (for garbage
                        > collecting) and void*arg. Then you can have whatever you want, be it
                        > lambdas, proxies for python callable objects, anonymous or
                        > user-defined functions.

                        I think you've once sketched this here:
                        https://groups.google.com/group/vim_dev/msg/d767d19228f5a886?hl=en

                        > We already want two different candidates for such change: I want
                        > python callables called without pyeval() proxy; you want lambdas. With
                        > such change these can live together and still count as one funcref
                        > type; without it would be 3 types.

                        Another benefit would be (as I remember) that it would be possible to
                        pass around Funcrefs to script-local functions and be able to just
                        invoke them in any scope without the current workarounds of expanding <SID>.

                        > I can write a patch for this, but not until RFC for python interfaces
                        > is finished and implemented.

                        Thank you so much; your Python RFC looks like a really nice (yet large)
                        addition!

                        -- regards, ingo

                        --
                        --
                        You received this message from the "vim_dev" maillist.
                        Do not top-post! Type your reply below the text you are replying to.
                        For more information, visit http://www.vim.org/maillist.php

                        ---
                        You received this message because you are subscribed to the Google Groups "vim_dev" group.
                        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                        For more options, visit https://groups.google.com/groups/opt_out.
                      • ZyX
                        ... Not this. Workaround with expanding was merged AFAIR and it does not matter much whether it is a workaround or not as it works (well, it will matter
                        Message 11 of 23 , May 31, 2013
                        • 0 Attachment
                          > Another benefit would be (as I remember) that it would be possible to
                          > pass around Funcrefs to script-local functions and be able to just
                          > invoke them in any scope without the current workarounds of expanding <SID>.

                          Not this. Workaround with expanding <SID> was merged AFAIR and it does not matter much whether it is a workaround or not as it works (well, it will matter when refactoring, but not from the view point of VimL users).

                          It has another huge benefit that you once you got obtained new-style funcref you can no longer fear that somebody will undefine or redefine the referenced function. Though if this matters for compatibility reasons I can have both new-style funcrefs and keep this problem: just have void* point to function name and keep functions used previously in fv_func_def (in this thread I named it ob_type) structure.

                          Next benefit is unified garbage-collecting.

                          --
                          --
                          You received this message from the "vim_dev" maillist.
                          Do not top-post! Type your reply below the text you are replying to.
                          For more information, visit http://www.vim.org/maillist.php

                          ---
                          You received this message because you are subscribed to the Google Groups "vim_dev" group.
                          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                          For more options, visit https://groups.google.com/groups/opt_out.
                        • Andy Wokula
                          ... What I use: (copy) sort {list} by comparing strings obtained with {selexpr} func! MapSort(list, selexpr) {list} list to sort {selexpr} (string)
                          Message 12 of 23 , Nov 3, 2013
                          • 0 Attachment
                            Am 31.05.2013 15:14, schrieb mattn:
                            > :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")

                            What I use:

                            " (copy) sort {list} by comparing strings obtained with {selexpr}
                            func! MapSort(list, selexpr)
                            " {list} list to sort
                            " {selexpr} (string) expression to get a string from list item {v:val}
                            let strings = map(copy(a:list), a:selexpr)
                            call map(strings, 'v:val. " ". v:key')
                            call sort(strings)
                            call map(strings, '0 + matchstr(v:val, ''\d\+$'')')
                            return map(strings, 'a:list[v:val]')
                            endfunc

                            let s:alpha = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'

                            " turns a number into a string for sorting purpose; returns the string
                            func! NumStr(number)
                            return s:alpha[strlen(a:number)]. a:number
                            endfunc

                            ---------------

                            :echo MapSort([{"foo":3},{"foo":2},{"foo":4}], "NumStr(v:val.foo)")
                            [{'foo': 2}, {'foo': 3}, {'foo': 4}]


                            --
                            Andy

                            --
                            --
                            You received this message from the "vim_dev" maillist.
                            Do not top-post! Type your reply below the text you are replying to.
                            For more information, visit http://www.vim.org/maillist.php

                            ---
                            You received this message because you are subscribed to the Google Groups "vim_dev" group.
                            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                            For more options, visit https://groups.google.com/groups/opt_out.
                          • mattn
                            When keys or values contains space letter, I wonder it works correctly. ... -- -- You received this message from the vim_dev maillist. Do not top-post! Type
                            Message 13 of 23 , Nov 5, 2013
                            • 0 Attachment
                              When keys or values contains space letter, I wonder it works correctly.

                              On Monday, November 4, 2013 2:39:26 AM UTC+9, Andy Wokula wrote:
                              > Am 31.05.2013 15:14, schrieb mattn:
                              >
                              > > :echo sort([{"foo":3},{"foo":2},{"foo":4}], "v:lhs.foo - v:rhs.foo")
                              >
                              >
                              >
                              > What I use:
                              >
                              >
                              >
                              > " (copy) sort {list} by comparing strings obtained with {selexpr}
                              >
                              > func! MapSort(list, selexpr)
                              >
                              > " {list} list to sort
                              >
                              > " {selexpr} (string) expression to get a string from list item {v:val}
                              >
                              > let strings = map(copy(a:list), a:selexpr)
                              >
                              > call map(strings, 'v:val. " ". v:key')
                              >
                              > call sort(strings)
                              >
                              > call map(strings, '0 + matchstr(v:val, ''\d\+$'')')
                              >
                              > return map(strings, 'a:list[v:val]')
                              >
                              > endfunc
                              >
                              >
                              >
                              > let s:alpha = 'ZABCDEFGHIJKLMNOPQRSTUVWXY'
                              >
                              >
                              >
                              > " turns a number into a string for sorting purpose; returns the string
                              >
                              > func! NumStr(number)
                              >
                              > return s:alpha[strlen(a:number)]. a:number
                              >
                              > endfunc
                              >
                              >
                              >
                              > ---------------
                              >
                              >
                              >
                              > :echo MapSort([{"foo":3},{"foo":2},{"foo":4}], "NumStr(v:val.foo)")
                              >
                              > [{'foo': 2}, {'foo': 3}, {'foo': 4}]
                              >
                              >
                              >
                              >
                              >
                              > --
                              >
                              > Andy

                              --
                              --
                              You received this message from the "vim_dev" maillist.
                              Do not top-post! Type your reply below the text you are replying to.
                              For more information, visit http://www.vim.org/maillist.php

                              ---
                              You received this message because you are subscribed to the Google Groups "vim_dev" group.
                              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                              For more options, visit https://groups.google.com/groups/opt_out.
                            • Andy Wokula
                              ... Keys? v:key is list index, it cannot contain spaces. Spaces in values shouldn t matter. Not sure what you mean. -- Andy -- -- You received this message
                              Message 14 of 23 , Nov 6, 2013
                              • 0 Attachment
                                Am 06.11.2013 02:16, schrieb mattn:
                                > When keys or values contains space letter, I wonder it works
                                > correctly.

                                Keys? v:key is list index, it cannot contain spaces.
                                Spaces in values shouldn't matter.
                                Not sure what you mean.

                                --
                                Andy

                                --
                                --
                                You received this message from the "vim_dev" maillist.
                                Do not top-post! Type your reply below the text you are replying to.
                                For more information, visit http://www.vim.org/maillist.php

                                ---
                                You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                For more options, visit https://groups.google.com/groups/opt_out.
                              • mattn
                                ... I wonder this won t works correctly. [{ a : b }, { a b : }] -- -- You received this message from the vim_dev maillist. Do not top-post! Type your
                                Message 15 of 23 , Aug 19, 2014
                                • 0 Attachment
                                  > Keys? v:key is list index, it cannot contain spaces.
                                  > Spaces in values shouldn't matter.
                                  > Not sure what you mean.

                                  I wonder this won't works correctly.

                                  [{"a": "b"}, {"a b": ""}]

                                  --
                                  --
                                  You received this message from the "vim_dev" maillist.
                                  Do not top-post! Type your reply below the text you are replying to.
                                  For more information, visit http://www.vim.org/maillist.php

                                  ---
                                  You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                  For more options, visit https://groups.google.com/d/optout.
                                • 'Andy Wokula' via vim_dev
                                  ... [{ a : b }, { a b : }] ... [ a , a b ] actually within MapSort(), the index is appended: [ a 0 , a b 1 ] (not so nice, but) shouldn t make a
                                  Message 16 of 23 , Aug 20, 2014
                                  • 0 Attachment
                                    Am 19.08.2014 um 11:17 schrieb mattn:
                                    >> Keys? v:key is list index, it cannot contain spaces.
                                    >> Spaces in values shouldn't matter.
                                    >> Not sure what you mean.
                                    >
                                    > I wonder this won't works correctly.
                                    >
                                    > [{"a": "b"}, {"a b": ""}]

                                    Guessing you want to sort by dict key:
                                    :echo MapSort([{"a": "b"}, {"a b": ""}], 'keys(v:val)[0]')
                                    [{'a': 'b'}, {'a b': ''}]

                                    the following list is given to the sort() function:
                                    :echo map([{"a": "b"}, {"a b": ""}], 'keys(v:val)[0]')
                                    ['a', 'a b']

                                    actually within MapSort(), the index is appended:
                                    ['a 0', 'a b 1']

                                    (not so nice, but) shouldn't make a difference for sort().

                                    Sorting by dict value:
                                    :echo MapSort([{"a": "b"}, {"a b": ""}], 'values(v:val)[0]')
                                    [{'a b': ''}, {'a': 'b'}]

                                    --
                                    Andy

                                    --
                                    --
                                    You received this message from the "vim_dev" maillist.
                                    Do not top-post! Type your reply below the text you are replying to.
                                    For more information, visit http://www.vim.org/maillist.php

                                    ---
                                    You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                    To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                    For more options, visit https://groups.google.com/d/optout.
                                  • mattn
                                    About making list sorted by string of one of value. echo 3
                                    Message 17 of 23 , Aug 20, 2014
                                    • 0 Attachment
                                      About making list sorted by string of one of value.

                                      echo "3" < "3 0"

                                      This is TRUE, then:

                                      echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}], 'v:val["foo"]["bar"]')

                                      We expecet it should be kept:

                                      [{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}]

                                      But results are:

                                      [{'foo': {'bar': '3 0'}}, {'foo': {'bar': '3'}}]

                                      We need to escape string to avoid to handle index as value.
                                      I don't want to need to provide this function in all plugins. Just want to get it in official sort function.

                                      --
                                      --
                                      You received this message from the "vim_dev" maillist.
                                      Do not top-post! Type your reply below the text you are replying to.
                                      For more information, visit http://www.vim.org/maillist.php

                                      ---
                                      You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                      To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                      For more options, visit https://groups.google.com/d/optout.
                                    • 'Andy Wokula' via vim_dev
                                      ... I don t think so. ... [ 3 0 1 , 3 0 ] I d expect [ 3 0 , 3 0 1 ]. ... 1 Bug in sort() function? -- Andy -- -- You received this message from the
                                      Message 18 of 23 , Aug 21, 2014
                                      • 0 Attachment
                                        Am 21.08.2014 um 03:50 schrieb mattn:
                                        > About making list sorted by string of one of value.
                                        >
                                        > echo "3" < "3 0"
                                        >
                                        > This is TRUE, then:
                                        >
                                        > echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}],
                                        > 'v:val["foo"]["bar"]')
                                        >
                                        > We expecet it should be kept:
                                        >
                                        > [{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}]
                                        >
                                        > But results are:
                                        >
                                        > [{'foo': {'bar': '3 0'}}, {'foo': {'bar': '3'}}]
                                        >
                                        > We need to escape string to avoid to handle index as value.

                                        I don't think so.

                                        > I don't want to need to provide this function in all plugins. Just
                                        > want to get it in official sort function.

                                        The reason is:
                                        :echo sort(['3 0', '3 0 1'])
                                        ['3 0 1', '3 0']

                                        I'd expect ['3 0', '3 0 1'].
                                        :echo '3 0' < '3 0 1'
                                        1

                                        Bug in sort() function?

                                        --
                                        Andy

                                        --
                                        --
                                        You received this message from the "vim_dev" maillist.
                                        Do not top-post! Type your reply below the text you are replying to.
                                        For more information, visit http://www.vim.org/maillist.php

                                        ---
                                        You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                        To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                        For more options, visit https://groups.google.com/d/optout.
                                      • mattn
                                        ... See :help sort() ... It need to do like below with MapSort() echo MapSort([{ foo :{ bar : 3 }}, { foo :{ bar : 3 0 }}], 0 + v:val[ foo ][ bar ] ) If
                                        Message 19 of 23 , Aug 21, 2014
                                        • 0 Attachment
                                          > > We need to escape string to avoid to handle index as value.
                                          > I don't think so.
                                          > > I don't want to need to provide this function in all plugins. Just
                                          > > want to get it in official sort function.
                                          > The reason is:
                                          > :echo sort(['3 0', '3 0 1'])
                                          > ['3 0 1', '3 0']
                                          > I'd expect ['3 0', '3 0 1'].
                                          > :echo '3 0' < '3 0 1'
                                          > 1
                                          > Bug in sort() function?

                                          See :help sort()

                                          > The sort is stable, items which compare equal (as number or as
                                          > string) will keep their relative position. E.g., when sorting
                                          > on numbers, text strings will sort next to each other, in the
                                          > same order as they were originally.

                                          It need to do like below with MapSort()

                                          echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}], '0 + v:val["foo"]["bar"]')

                                          If using my patch, it is possible easy to avoid this.

                                          :echo sort(["3 0", "3 0 1"], "a:lhs < a:rhs")

                                          And MapSort() can't look local variable in the expression. For example, this doesn't work.

                                          ---------------------------------------------------
                                          function! s:foo()
                                          let rank = {"queen": 12, "king": 13}
                                          return MapSort(["queen", "king"], 'rank[v:val]')
                                          endfunction
                                          echo s:foo()
                                          ---------------------------------------------------

                                          But:

                                          ---------------------------------------------------
                                          function! s:foo()
                                          let rank = {"queen": 12, "king": 13}
                                          return sort(["queen", "king"], 'rank[v:lhs] < rank[v:rhs]')
                                          endfunction
                                          echo s:foo()
                                          ---------------------------------------------------

                                          This should be worked.

                                          --
                                          --
                                          You received this message from the "vim_dev" maillist.
                                          Do not top-post! Type your reply below the text you are replying to.
                                          For more information, visit http://www.vim.org/maillist.php

                                          ---
                                          You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                          To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                          For more options, visit https://groups.google.com/d/optout.
                                        • John Little
                                          ... There is indeed a bug in sort(). The stability of the sort is not relevant. I haven t been following the thread too closely, but became interested on
                                          Message 20 of 23 , Aug 21, 2014
                                          • 0 Attachment
                                            On Friday, August 22, 2014 12:05:33 PM UTC+12, mattn wrote:
                                            > > Bug in sort() function?
                                            >
                                            > See :help sort()
                                            >
                                            > > The sort is stable, items which compare equal (as number or as...

                                            There is indeed a bug in sort(). The stability of the sort is not relevant.

                                            I haven't been following the thread too closely, but became interested on seeing Andy's report of

                                            :echo sort(['3 0', '3 0 1'])
                                            ['3 0 1', '3 0']

                                            I tried
                                            :echo sort(['foo bar','foo'])
                                            ['foo bar', 'foo']
                                            but
                                            :echo sort(['foobar','foo'])
                                            ['foo', 'foobar']

                                            The space makes a difference, because it sorts less than a single quote. Try
                                            :echo sort(['foo','foo&bar','foo(bar'])
                                            ['foo&bar', 'foo', 'foo(bar']

                                            Looking in eval.c, to compare list items tv2string is called to stringify each item. tv2string calls string_quote, which puts single quotes around each string (and doubles any single quotes in the string). This effectively adds a single quote to the end of each string, and if it's being compared to a string that has something comparing less than a quote, it sorts ahead.

                                            I don't know why string_quote is called; it clearly is not useful for this purpose, but may be needed for another.

                                            Regards, John Little

                                            --
                                            --
                                            You received this message from the "vim_dev" maillist.
                                            Do not top-post! Type your reply below the text you are replying to.
                                            For more information, visit http://www.vim.org/maillist.php

                                            ---
                                            You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                            To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                            For more options, visit https://groups.google.com/d/optout.
                                          • Bram Moolenaar
                                            ... Oof, I didn t realize the item compare function was changing strings before doing the actual compare. And it allocates a buffer for the result, thus
                                            Message 21 of 23 , Aug 22, 2014
                                            • 0 Attachment
                                              John Little wrote:

                                              > On Friday, August 22, 2014 12:05:33 PM UTC+12, mattn wrote:
                                              > > > Bug in sort() function?
                                              > >
                                              > > See :help sort()
                                              > >
                                              > > > The sort is stable, items which compare equal (as number or as...
                                              >
                                              > There is indeed a bug in sort(). The stability of the sort is not relevant.
                                              >
                                              > I haven't been following the thread too closely, but became interested
                                              > on seeing Andy's report of
                                              >
                                              > :echo sort(['3 0', '3 0 1'])
                                              > ['3 0 1', '3 0']
                                              >
                                              > I tried
                                              > :echo sort(['foo bar','foo'])
                                              > ['foo bar', 'foo']
                                              > but
                                              > :echo sort(['foobar','foo'])
                                              > ['foo', 'foobar']
                                              >
                                              > The space makes a difference, because it sorts less than a single quote. Try
                                              > :echo sort(['foo','foo&bar','foo(bar'])
                                              > ['foo&bar', 'foo', 'foo(bar']
                                              >
                                              > Looking in eval.c, to compare list items tv2string is called to
                                              > stringify each item. tv2string calls string_quote, which puts single
                                              > quotes around each string (and doubles any single quotes in the
                                              > string). This effectively adds a single quote to the end of each
                                              > string, and if it's being compared to a string that has something
                                              > comparing less than a quote, it sorts ahead.
                                              >
                                              > I don't know why string_quote is called; it clearly is not useful for
                                              > this purpose, but may be needed for another.

                                              Oof, I didn't realize the item compare function was changing strings
                                              before doing the actual compare. And it allocates a buffer for the
                                              result, thus that's expensive. Thanks for digging that up!

                                              Just removing the quoting means the sorting of strings vs numbers
                                              changes completely. That goes against the documentation. I can work
                                              around that, hopefully it doesn't cause trouble in a situation that the
                                              tests doesn't catch.

                                              --
                                              Don't believe everything you hear or anything you say.

                                              /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
                                              /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
                                              \\\ an exciting new programming language -- http://www.Zimbu.org ///
                                              \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

                                              --
                                              --
                                              You received this message from the "vim_dev" maillist.
                                              Do not top-post! Type your reply below the text you are replying to.
                                              For more information, visit http://www.vim.org/maillist.php

                                              ---
                                              You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                              To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                              For more options, visit https://groups.google.com/d/optout.
                                            • 'Andy Wokula' via vim_dev
                                              ... With MapSort(), you have to derive strings from you list items, as plain sort() can only compare strings. It s easy to turn positive integers into strings
                                              Message 22 of 23 , Oct 15, 2014
                                              • 0 Attachment
                                                Am 22.08.2014 um 02:05 schrieb mattn:
                                                > It need to do like below with MapSort()
                                                >
                                                > echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}], '0 + v:val["foo"]["bar"]')
                                                >
                                                > If using my patch, it is possible easy to avoid this.
                                                >
                                                > :echo sort(["3 0", "3 0 1"], "a:lhs < a:rhs")

                                                I suppose your examples should read as follows (?):
                                                :echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}], 'v:val.foo.bar')
                                                :echo sort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}], 'v:lhs.foo.bar < v:rhs.foo.bar')

                                                With MapSort(), you have to derive strings from you list items, as plain sort() can only compare strings.

                                                It's easy to turn positive integers into strings for comparison.
                                                (It's hard to turn floats into strings for comparison.)

                                                > And MapSort() can't look local variable in the expression. For example, this doesn't work.

                                                > ---------------------------------------------------
                                                > function! s:foo()
                                                > let rank = {"queen": 12, "king": 13}
                                                > return MapSort(["queen", "king"], 'rank[v:val]')
                                                > endfunction
                                                > echo s:foo()

                                                It can use local variables, with a little change to the func sig:
                                                MapSort(list, selexpr, ...)
                                                now you can place a:1, a:2, ... in your map expression:

                                                function! s:foo()
                                                let rank = {"queen": 9, "king": 10}
                                                return MapSort(["queen", "king"], 'NumStr(a:1[v:val])', rank)
                                                endfunction
                                                echo s:foo()

                                                (where NumStr() turns 9 into 'A9' and 10 into 'B10')

                                                --
                                                Andy

                                                --
                                                --
                                                You received this message from the "vim_dev" maillist.
                                                Do not top-post! Type your reply below the text you are replying to.
                                                For more information, visit http://www.vim.org/maillist.php

                                                ---
                                                You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                                To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                                For more options, visit https://groups.google.com/d/optout.
                                              • Yasuhiro MATSUMOTO
                                                I m not talking about how you can do it. And I don t hope to discuss about MapSort. We should discuss how sort function is improvable to do it without copying
                                                Message 23 of 23 , Oct 15, 2014
                                                • 0 Attachment
                                                  I'm not talking about how you can do it. And I don't hope to discuss
                                                  about MapSort. We should discuss how sort function is improvable to do
                                                  it without copying your MapSort every time.

                                                  On 10/16/14, Andy Wokula <anwoku@...> wrote:
                                                  > Am 22.08.2014 um 02:05 schrieb mattn:
                                                  >> It need to do like below with MapSort()
                                                  >>
                                                  >> echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}], '0 +
                                                  >> v:val["foo"]["bar"]')
                                                  >>
                                                  >> If using my patch, it is possible easy to avoid this.
                                                  >>
                                                  >> :echo sort(["3 0", "3 0 1"], "a:lhs < a:rhs")
                                                  >
                                                  > I suppose your examples should read as follows (?):
                                                  > :echo MapSort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}],
                                                  > 'v:val.foo.bar')
                                                  > :echo sort([{"foo":{"bar": "3"}}, {"foo":{"bar": "3 0"}}],
                                                  > 'v:lhs.foo.bar < v:rhs.foo.bar')
                                                  >
                                                  > With MapSort(), you have to derive strings from you list items, as plain
                                                  > sort() can only compare strings.
                                                  >
                                                  > It's easy to turn positive integers into strings for comparison.
                                                  > (It's hard to turn floats into strings for comparison.)
                                                  >
                                                  >> And MapSort() can't look local variable in the expression. For example,
                                                  >> this doesn't work.
                                                  >
                                                  >> ---------------------------------------------------
                                                  >> function! s:foo()
                                                  >> let rank = {"queen": 12, "king": 13}
                                                  >> return MapSort(["queen", "king"], 'rank[v:val]')
                                                  >> endfunction
                                                  >> echo s:foo()
                                                  >
                                                  > It can use local variables, with a little change to the func sig:
                                                  > MapSort(list, selexpr, ...)
                                                  > now you can place a:1, a:2, ... in your map expression:
                                                  >
                                                  > function! s:foo()
                                                  > let rank = {"queen": 9, "king": 10}
                                                  > return MapSort(["queen", "king"], 'NumStr(a:1[v:val])', rank)
                                                  > endfunction
                                                  > echo s:foo()
                                                  >
                                                  > (where NumStr() turns 9 into 'A9' and 10 into 'B10')
                                                  >
                                                  > --
                                                  > Andy
                                                  >


                                                  --
                                                  - Yasuhiro Matsumoto

                                                  --
                                                  --
                                                  You received this message from the "vim_dev" maillist.
                                                  Do not top-post! Type your reply below the text you are replying to.
                                                  For more information, visit http://www.vim.org/maillist.php

                                                  ---
                                                  You received this message because you are subscribed to the Google Groups "vim_dev" group.
                                                  To unsubscribe from this group and stop receiving emails from it, send an email to vim_dev+unsubscribe@....
                                                  For more options, visit https://groups.google.com/d/optout.
                                                Your message has been successfully submitted and would be delivered to recipients shortly.