2016-07-14 9:43 GMT+03:00 mattn <[email protected]>:
> On Thursday, July 14, 2016 at 12:49:58 PM UTC+9, ZyX wrote:
>> 2016-07-13 19:17 GMT+03:00 mattn <[email protected]>:
>> > On Wednesday, July 13, 2016 at 4:11:59 AM UTC+9, Bram Moolenaar wrote:
>> >
>> > Well, I wonder this lambda will be useful. At the first, we hoped to call
>> > statements in lambda. But the implementation you will include into vim
>> > can't do because it only allow expressions. It's similar to python's
>> > lambda. python's one doesn't allow statements. So usecase are limited to
>> > use. I don't have strong opinion but I'm thinking that this is an new
>> > expresssion or language for the lambda. It will demand to learn the new
>> > expression for the users.
>>
>> Vim has `execute()`. Python-3 has `exec()` function (Python-2 has it
>> as a statement). Lambdas do not usually allow statements because they
>> are to be used in contexts which requires return value (e.g. in Python
>> this is sorted()/list.sort(), defaultdict(), re.sub[n] (BTW, it is
>> good idea to have `substitute(s, pattern, funcref, flags)` to work
>> like `substitute(s, pattern, '\=funcref(submatch(0), submatch(1), …)',
>> flags)`)). Lambdas are also used as a replacement for
>> `functools.partial` (python)/`function(fref, args, self)` (VimL) in
>> cases when they do not apply (e.g. when one needs to fix not the
>> first, but second or other arguments), but this requires closures.
>
> execute() doesn't have scope. So:
>
> call execute("let a = 1")
> echo 1
>
> This define new variable in global scope. I want anonymous function. If
> execute() works with the scope, for example "let a = 1" mean "let l:a = 1",
> It's so great.
`execute()` uses whatever scope it is called in, just as :execute
does. Your test is invalid, check execute() *inside a function*, there
is no reason why `execute()` should yield anything different in your
test. With Python’s `exec` you would see exactly the same results.
>
>> If you need lambda with statement then most likely you are going to
>> create unreadable code. Good lambdas are *small*, all examples you
>> have shown in documentation are either
>>
>> 1. More readable with new lambda style. It is absolutely not needed to
>> write `return` and `a:` in `sort()` or `map()` callbacks.
>> 2. Not practical. I still want to see an example of timer callback
>> which is used for more practical applications then testing timers
>> (like your echo) and yet is more readable with your variant of lambda
>> among variants a) :function, b) new lambda style (+ execute()!) and c)
>> your lambda style.
>
> The arrow operator which bram suggested is new, I think.
“I have (seen) a plugin that uses timers/jobs/etc with the following
code [link] and think that if there were lambdas implemented according
to my suggestion it would be more readable if rewritten as [code].”
Current suggestion has three points behind it:
1. sort() and map() callbacks will be more readable now. Both are fine
if lambdas are expression-only. I would say that substitute() could
also accept funcref in place of \=.
2. Other languages which have lambdas (e.g. Python) accept expressions
as a lambda body.
3. I know languages which have anonymous function allowing statements
and requiring `return` (e.g. C++). But
a) here they are usually not called “lambdas” (though specifically
C++ named its feature “lambda function”): these may be “anonymous
functions” (where it is a simple extension to function definition
syntax, e.g. lua with `function foo(a) return a end` vs `foo =
(function(a) return a end)`, JavaScript is similar; you cannot really
call such an extension “lambda function”), “arrow functions” (ES6,
though it also has anonymous functions), “closures” (Rust), etc,
b) I know exactly no languages where lambdas allow statements
*and* require continuation characters to span through multiple lines
and
c) some of such languages (Perl, Ruby, Rust (though it requires
omitting semicolon)) follow convention “result of last statement in
the block is the return value of the function”.
Though I should say that wikipedia states that “lambda function” and
“anonymous function” are essentially the same thing and 3.a mostly
expresses why I have certain expectations when I hear this term. 3.b
is a requirement if you really want commands in VimL lambdas. 3.c is
not applicable for VimL.
>
>> 3. Needs closures and not lambdas. I would not really say that your
>> example with multiline lambda used for counter generator is better
>> then with :function. Though it actually needs no closures in the
>> current state:
>>
>> function s:counter(x)
>> let d = {'x': a:x}
>> function d.counter()
>> let self.x += 1
>> return self.x
>> endfunction
>> return d.counter
>> endfunction
>>
>> And there is no new language, expressions are already used in a number
>> of places.
>
> --
> --
> You received this message from the "vim_dev" 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_dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
--
--
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.