Finding an item using a lambda function in a List using the index() function

1,325 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Aug 12, 2022, 10:41:48 AM8/12/22
to vim_dev
Hi all,

The index() function returns the lowest index in a List where an item
has a value equal to the supplied expression. This function cannot be
used to find an item with a specific key/value pair in a List of Dicts
(or to use a custom search in a List). You need to resort to using a
for loop to find the index.

What do you think about extending the index() function to accept a
lambda function (similar to map(), filter() and reduce())? This lambda
function will be called with two arguments: the index of the item and the
value of the item. If this function returns true, then the search will stop.

Currently the index() function accepts four arguments (the first two
arguments are mandatory and the last two arguments are optional). To
support using a lambda function, a fifth optional argument 'eval' needs
to be added. If this is true, then the second 'expr' argument will be
evaluated. It would have been simpler to add new arguments if the third
argument is a Dict.

Regards,
Yegappan

Ernie Rael

unread,
Aug 12, 2022, 11:54:24 AM8/12/22
to vim...@googlegroups.com
How about if the 3rd argument is a dict, then there are only/exactly
three arguments
and the dict specifies optional parameters? The old definition is
unchanged with 2+2.

-erniee

>
> Regards,
> Yegappan
>

Bram Moolenaar

unread,
Aug 12, 2022, 1:06:05 PM8/12/22
to vim...@googlegroups.com, Ernie Rael
Instead making the index() arguments quite complicated, it's probably
better to add a new function that takes two arguments: the list and a
dict with various (optional) arguments. Or, to make it more like map()
and filter(), the second argument would be the lambda/funcref and the
third argument a dict with optional arguments. That's probably best,
since the only optional argument that makes sense is the start index.

Not sure what to call it, indexfunc() ?

--
ARTHUR: Who are you?
TALL KNIGHT: We are the Knights Who Say "Ni"!
BEDEVERE: No! Not the Knights Who Say "Ni"!
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Maxim Kim

unread,
Aug 12, 2022, 1:10:59 PM8/12/22
to vim...@googlegroups.com

> Not sure what to call it, indexfunc() ?


- indexof()
- indexby()

Yegappan Lakshmanan

unread,
Aug 12, 2022, 1:25:09 PM8/12/22
to vim_dev, Ernie Rael
Hi Bram,
Sounds good. Based on the suggestion from Maxim Kim, I will go with the
name indexof().

- Yegappan
Reply all
Reply to author
Forward
0 new messages