From 0972d7a12468d6914a70e453af85c307b167c55b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 26 Feb 2023 23:13:12 +0800 Subject: vim-patch:9.0.0196: finding value in list may require a for loop Problem: Finding value in list may require a for loop. Solution: Add indexof(). (Yegappan Lakshmanan, closes vim/vim#10903) https://github.com/vim/vim/commit/b218655d5a485f5b193fb18d7240837d42b89812 Co-authored-by: Yegappan Lakshmanan --- runtime/doc/builtin.txt | 48 +++++++++++++++++++++++++++++++++++++++++++++++- runtime/doc/usr_41.txt | 6 ++++-- 2 files changed, 51 insertions(+), 3 deletions(-) (limited to 'runtime/doc') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 5f279ed09c..1168d7ff1d 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -255,6 +255,8 @@ iconv({expr}, {from}, {to}) String convert encoding of {expr} indent({lnum}) Number indent of line {lnum} index({object}, {expr} [, {start} [, {ic}]]) Number index in {object} where {expr} appears +indexof({object}, {expr} [, {opts}]]) + Number index in {object} where {expr} is true input({prompt} [, {text} [, {completion}]]) String get input from the user inputlist({textlist}) Number let the user pick from a choice list @@ -4084,19 +4086,25 @@ indent({lnum}) The result is a Number, which is indent of line {lnum} in the GetLnum()->indent() index({object}, {expr} [, {start} [, {ic}]]) *index()* + Find {expr} in {object} and return its index. See + |filterof()| for using a lambda to select the item. + If {object} is a |List| return the lowest index where the item has a value equal to {expr}. There is no automatic conversion, so the String "4" is different from the Number 4. And the Number 4 is different from the Float 4.0. The value - of 'ignorecase' is not used here, case always matters. + of 'ignorecase' is not used here, case matters as indicated by + the {ic} argument. If {object} is a |Blob| return the lowest index where the byte value is equal to {expr}. If {start} is given then start looking at the item with index {start} (may be negative for an item relative to the end). + When {ic} is given and it is |TRUE|, ignore case. Otherwise case must match. + -1 is returned when {expr} is not found in {object}. Example: > :let idx = index(words, "the") @@ -4105,6 +4113,44 @@ index({object}, {expr} [, {start} [, {ic}]]) *index()* < Can also be used as a |method|: > GetObject()->index(what) +indexof({object}, {expr} [, {opt}]) *indexof()* + {object} must be a |List| or a |Blob|. + If {object} is a |List|, evaluate {expr} for each item in the + List until the expression returns v:true and return the index + of this item. + + If {object} is a |Blob| evaluate {expr} for each byte in the + Blob until the expression returns v:true and return the index + of this byte. + + {expr} must be a |string| or |Funcref|. + + If {expr} is a |string|: If {object} is a |List|, inside + {expr} |v:key| has the index of the current List item and + |v:val| has the value of the item. If {object} is a |Blob|, + inside {expr} |v:key| has the index of the current byte and + |v:val| has the byte value. + + If {expr} is a |Funcref| it must take two arguments: + 1. the key or the index of the current item. + 2. the value of the current item. + The function must return |TRUE| if the item is found and the + search should stop. + + The optional argument {opt} is a Dict and supports the + following items: + start start evaluating {expr} at the item with index + {start} (may be negative for an item relative + to the end). + Returns -1 when {expr} evaluates to v:false for all the items. + Example: > + :let l = [#{n: 10}, #{n: 20}, #{n: 30]] + :let idx = indexof(l, "v:val.n == 20") + :let idx = indexof(l, {i, v -> v.n == 30}) + +< Can also be used as a |method|: > + mylist->indexof(expr) + input({prompt} [, {text} [, {completion}]]) *input()* input({opts}) The result is a String, which is whatever the user typed on diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index 27c5927cf9..77b1795a57 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -657,14 +657,16 @@ List manipulation: *list-functions* map() change each List item reduce() reduce a List to a value sort() sort a List - reverse() reverse the order of a List + reverse() reverse the order of a List or Blob uniq() remove copies of repeated adjacent items split() split a String into a List join() join List items into a String range() return a List with a sequence of numbers string() String representation of a List call() call a function with List as arguments - index() index of a value in a List + index() index of a value in a List or Blob + indexof() index in a List or Blob where an expression + evaluates to true max() maximum value in a List min() minimum value in a List count() count number of times a value appears in a List -- cgit From 13da3d469ae10201de00ae89277c53c40342f4df Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 26 Feb 2023 23:50:35 +0800 Subject: vim-patch:partial:9.0.0202: code and help for indexof() is not ideal Problem: Code and help for indexof() is not ideal. Solution: Refactor the code, improve the help. (Yegappan Lakshmanan, closes vim/vim#10908) https://github.com/vim/vim/commit/3fbf6cd355de2212e9227f57d545592aae3f688f Skip CHECK_LIST_MATERIALIZE and set_vim_var_type(). Use tv_list_uidx() instead of lv_idx. Co-authored-by: Yegappan Lakshmanan --- runtime/doc/builtin.txt | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'runtime/doc') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 1168d7ff1d..35f1b7cd6f 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -4087,7 +4087,7 @@ indent({lnum}) The result is a Number, which is indent of line {lnum} in the index({object}, {expr} [, {start} [, {ic}]]) *index()* Find {expr} in {object} and return its index. See - |filterof()| for using a lambda to select the item. + |indexof()| for using a lambda to select the item. If {object} is a |List| return the lowest index where the item has a value equal to {expr}. There is no automatic @@ -4113,15 +4113,17 @@ index({object}, {expr} [, {start} [, {ic}]]) *index()* < Can also be used as a |method|: > GetObject()->index(what) -indexof({object}, {expr} [, {opt}]) *indexof()* - {object} must be a |List| or a |Blob|. +indexof({object}, {expr} [, {opts}]) *indexof()* + Returns the index of an item in {object} where {expr} is + v:true. {object} must be a |List| or a |Blob|. + If {object} is a |List|, evaluate {expr} for each item in the - List until the expression returns v:true and return the index - of this item. + List until the expression is v:true and return the index of + this item. If {object} is a |Blob| evaluate {expr} for each byte in the - Blob until the expression returns v:true and return the index - of this byte. + Blob until the expression is v:true and return the index of + this byte. {expr} must be a |string| or |Funcref|. @@ -4137,16 +4139,17 @@ indexof({object}, {expr} [, {opt}]) *indexof()* The function must return |TRUE| if the item is found and the search should stop. - The optional argument {opt} is a Dict and supports the + The optional argument {opts} is a Dict and supports the following items: - start start evaluating {expr} at the item with index - {start} (may be negative for an item relative - to the end). + startidx start evaluating {expr} at the item with this + index; may be negative for an item relative to + the end Returns -1 when {expr} evaluates to v:false for all the items. Example: > - :let l = [#{n: 10}, #{n: 20}, #{n: 30]] - :let idx = indexof(l, "v:val.n == 20") - :let idx = indexof(l, {i, v -> v.n == 30}) + :let l = [#{n: 10}, #{n: 20}, #{n: 30}] + :echo indexof(l, "v:val.n == 20") + :echo indexof(l, {i, v -> v.n == 30}) + :echo indexof(l, "v:val.n == 20", #{startidx: 1}) < Can also be used as a |method|: > mylist->indexof(expr) -- cgit