Wednesday, January 6, 2021

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

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]])

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.

No comments: