Wednesday, January 6, 2021

Re: why doesn't min take more than 1 parameter?

Yes, 20. I agree with the problem. It's undoubtedly a holdover from when you had to use a:0, a:1, etc., and couldn't get a:000 (as a list), before proper lists were added.

I think the system could be extended to allow an arbitrary number of arguments for a:000 and still limit to up to 20 via the old mechanism: if you want to access the 21st on, use a:000 instead. 

Of course, if you have dozens and dozens of arguments, you could wrap them in a [] pair to convert it into a list of arbitrary length (that process isn't limited by the 20 function parameter limit). Programmatically speaking, if you're doing this dynamically, it seems to me as if creating a list of arbitrary length is better than creating an execute statement that takes a large number of individual parameters.

--

Salman

On Wed, Jan 6, 2021, 15:38 BPJ <bpj@melroch.se> wrote:
Den ons 6 jan. 2021 21:29Salman Halim <salmanhalim@gmail.com> skrev:
While I can't explain why things work the way they do, here is a custom function that should do what you want (called Min); it takes a variable number of arguments, flattens incoming lists into a single list and then calls the built-in min. You can see that it uses the type() function to figure out the type of the incoming argument (to answer another question you asked herein):

function! Min( ... )
  let collection = []

  for entry in a:000
    if ( type( entry ) == v:t_list )
      call extend( collection, flatten( entry ) )
    else
      let collection += [ entry ]
    endif
  endfor

  return min( collection )
endfunction

" Sample call
" echo Min(9, 22, [5, [3, 7]])

Isn't the problem that the number of varargs to a Vimscript function is limited, while a List can have many more elements (although there is probably a limit there too)?



On Wed, Jan 6, 2021 at 2:37 PM L A Walsh <vim@tlinx.org> wrote:
On 2021/01/02 08:01, Tim Chase wrote
> though a lot of vim stuff takes inspiration from
> Python where min() is a vararg function letting
> you do as the OP requests
>
>   min(arg1, arg2, arg3, arg4, …)
>
> so it's a reasonable sort of hope/expectation.
> It just doesn't happen to be a vim thing.
>   
---
    Is there any reason why vim couldn't have a simplified
version, "Min" that takes a variable number of args
rather than its current behavior of taking a fixed
number (1) of "list-args"?

I.e. it sounds like, by being declared "varargs",
that min could take a variable number of args (?).

    Is a vim function capable of determining the type
of the arg(s) passed to it?  Like if I passed
min(3,4,[1,2]), can vim determine that it got 3 arguments,
where it knows that they are two "plain" args, and
1 is a "List" such that it could automatically expand "List" args,
and "inline" its elements into 1 longer set of arguments
that 'min' can return the value of? 

    I.e., it seems like "min", rather than returning an "Error"
when presented with multiple-args, could simply return the
minimum of those args, _and_, if any of those args were a "List",
like [1,2], then merge those args into the set of multiple
args that was passed.  This would mean that ([1,2]) would
automatically expand its List into multiple arguments (1,2), and
then return the "min" value,  i.e. "doing the right thing", while
keeping compatibility with current behavior (except for returning
an error when unnecessary).

     Wouldn't that be possible for "min" and similar functions
that can tell what the user wants, and just do it? (rather than
returning an error that doesn't seem necessary or useful)
> -tim
>   
---
    Thanks for the explanations!  As you point out, vim, it
seems, could do the right thing while remaining functionally
compatible with older vim script.

-Linda

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/5FF6110E.7050102%40tlinx.org.


--
 
Salman

I, too, shall something make and glory in the making.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEdT5fcqbQc5v8o2h8RiPrwqSgm9ew5jKyBxNBGo%3DiBo9g%40mail.gmail.com.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CADAJKhBiO2KacvOiZYpGi6jW0hKfhcPawsKUdJe0diqRZ80UzQ%40mail.gmail.com.

--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CANuxnEeH0iDyQJPV2BKxcw3LSUX7dz8%2BiGtJ0Ci2TpztQhiO7Q%40mail.gmail.com.

No comments: