mirror of
https://github.com/neovim/neovim.git
synced 2025-09-16 16:28:17 +00:00
vim-patch:partial:9.1.0027: Vim is missing a foreach() func (#27037)
Problem: Vim is missing a foreach() func Solution: Implement foreach({expr1}, {expr2}) function, which applies {expr2} for each item in {expr1} without changing it (Ernie Rael) closes: vim/vim#12166e79e207760
Partial port as this doesn't handle non-materialized range() lists. vim-patch:c92b8bed1fa6 runtime(help): delete duplicate help tag E741 (vim/vim#13861)c92b8bed1f
Co-authored-by: Ernie Rael <errael@raelity.com>
This commit is contained in:
@@ -2923,6 +2923,48 @@ M.funcs = {
|
||||
returns = 'string',
|
||||
signature = 'foldtextresult({lnum})',
|
||||
},
|
||||
foreach = {
|
||||
args = 2,
|
||||
base = 1,
|
||||
desc = [=[
|
||||
{expr1} must be a |List|, |String|, |Blob| or |Dictionary|.
|
||||
For each item in {expr1} execute {expr2}. {expr1} is not
|
||||
modified; its values may be, as with |:lockvar| 1. |E741|
|
||||
See |map()| and |filter()| to modify {expr1}.
|
||||
|
||||
{expr2} must be a |string| or |Funcref|.
|
||||
|
||||
If {expr2} is a |string|, inside {expr2} |v:val| has the value
|
||||
of the current item. For a |Dictionary| |v:key| has the key
|
||||
of the current item and for a |List| |v:key| has the index of
|
||||
the current item. For a |Blob| |v:key| has the index of the
|
||||
current byte. For a |String| |v:key| has the index of the
|
||||
current character.
|
||||
Examples: >
|
||||
call foreach(mylist, 'let used[v:val] = v:true')
|
||||
<This records the items that are in the {expr1} list.
|
||||
|
||||
Note that {expr2} is the result of expression and is then used
|
||||
as a command. Often it is good to use a |literal-string| to
|
||||
avoid having to double backslashes.
|
||||
|
||||
If {expr2} 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.
|
||||
With a lambda you don't get an error if it only accepts one
|
||||
argument.
|
||||
If the function returns a value, it is ignored.
|
||||
|
||||
Returns {expr1} in all cases.
|
||||
When an error is encountered while executing {expr2} no
|
||||
further items in {expr1} are processed.
|
||||
When {expr2} is a Funcref errors inside a function are ignored,
|
||||
unless it was defined with the "abort" flag.
|
||||
]=],
|
||||
name = 'foreach',
|
||||
params = { { 'expr1', 'any' }, { 'expr2', 'any' } },
|
||||
signature = 'foreach({expr1}, {expr2})',
|
||||
},
|
||||
foreground = {
|
||||
args = 0,
|
||||
params = {},
|
||||
|
Reference in New Issue
Block a user