1
0
forked from aniani/vim

patch 7.4.2044

Problem:    filter() and map() either require a string or defining a function.
Solution:   Support lambda, a short way to define a function that evaluates an
            expression. (Yasuhiro Matsumoto, Ken Takata)
This commit is contained in:
Bram Moolenaar
2016-07-15 21:25:08 +02:00
parent 93431df9eb
commit 069c1e7fa9
7 changed files with 428 additions and 80 deletions

View File

@@ -1,4 +1,4 @@
*eval.txt* For Vim version 7.4. Last change: 2016 Jul 09
*eval.txt* For Vim version 7.4. Last change: 2016 Jul 15
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -140,9 +140,10 @@ You will not get an error if you try to change the type of a variable.
1.2 Function references ~
*Funcref* *E695* *E718*
A Funcref variable is obtained with the |function()| function. It can be used
in an expression in the place of a function name, before the parenthesis
around the arguments, to invoke the function it refers to. Example: >
A Funcref variable is obtained with the |function()| function or created with
the lambda expression |expr-lambda|. It can be used in an expression in the
place of a function name, before the parenthesis around the arguments, to
invoke the function it refers to. Example: >
:let Fn = function("MyFunc")
:echo Fn()
@@ -694,6 +695,7 @@ Expression syntax summary, from least to most significant:
@r contents of register 'r'
function(expr1, ...) function call
func{ti}on(expr1, ...) function call with curly braces
{args -> expr1} lambda expression
".." indicates that the operations in this level can be concatenated.
@@ -1207,6 +1209,42 @@ function(expr1, ...) function call
See below |functions|.
lambda expression *expr-lambda* *lambda*
-----------------
{args -> expr1} lambda expression
A lambda expression creates a new unnamed function which returns the result of
evaluating |expr1|. Lambda expressions are differ from |user-functions| in
the following ways:
1. The body of the lambda expression is an |expr1| and not a sequence of |Ex|
commands.
2. The prefix "a:" is optional for arguments. E.g.: >
:let F = {arg1, arg2 -> arg1 - arg2}
:echo F(5, 2)
< 3
The arguments are optional. Example: >
:let F = {-> 'error function'}
:echo F()
< error function
Examples for using a lambda expression with |sort()|, |map()| and |filter()|: >
:echo map([1, 2, 3], {idx, val -> val + 1})
< [2, 3, 4] >
:echo sort([3,7,2,1,4], {a, b -> a - b})
< [1, 2, 3, 4, 7]
The lambda expression is also useful for Channel, Job and timer: >
:let timer = timer_start(500,
\ {-> execute("echo 'Handler called'", "")},
\ {'repeat': 3})
< Handler called
Handler called
Handler called
Note how execute() is used to execute an Ex command. That's ugly though.
==============================================================================
3. Internal variable *internal-variables* *E461*
@@ -3278,7 +3316,8 @@ execute({command} [, {silent}]) *execute()*
"silent" `:silent` used
"silent!" `:silent!` used
The default is 'silent'. Note that with "silent!", unlike
`:redir`, error messages are dropped.
`:redir`, error messages are dropped. When using an external
command the screen may be messed up, use `system()` instead.
*E930*
It is not possible to use `:redir` anywhere in {command}.
@@ -7202,7 +7241,8 @@ system({expr} [, {input}]) *system()* *E677*
in a way |writefile()| does with {binary} set to "b" (i.e.
with a newline between each list item with newlines inside
list items converted to NULs).
Pipes are not used.
Pipes are not used, the 'shelltemp' option is not used.
When prepended by |:silent| the shell will not be set to
cooked mode. This is meant to be used for commands that do
@@ -8204,10 +8244,10 @@ can be 0). "a:000" is set to a |List| that contains these arguments. Note
that "a:1" is the same as "a:000[0]".
*E742*
The a: scope and the variables in it cannot be changed, they are fixed.
However, if a |List| or |Dictionary| is used, you can change their contents.
Thus you can pass a |List| to a function and have the function add an item to
it. If you want to make sure the function cannot change a |List| or
|Dictionary| use |:lockvar|.
However, if a composite type is used, such as |List| or |Dictionary| , you can
change their contents. Thus you can pass a |List| to a function and have the
function add an item to it. If you want to make sure the function cannot
change a |List| or |Dictionary| use |:lockvar|.
When not using "...", the number of arguments in a function call must be equal
to the number of named arguments. When using "...", the number of arguments
@@ -8219,9 +8259,8 @@ until the matching |:endfunction|. It is allowed to define another function
inside a function body.
*local-variables*
Inside a function variables can be used. These are local variables, which
will disappear when the function returns. Global variables need to be
accessed with "g:".
Inside a function local variables can be used. These will disappear when the
function returns. Global variables need to be accessed with "g:".
Example: >
:function Table(title, ...)