Hi mattn,

2016/7/14 Thu 15:43:11 UTC+9 mattn wrote:
> On Thursday, July 14, 2016 at 12:49:58 PM UTC+9, ZyX wrote:
> > 2016-07-13 19:17 GMT+03:00 mattn <mattn...@gmail.com>:
> > > 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.

Currently "{args -> expr}" has its own scope.

  :echo {-> execute("let a = 1")}()
  :echo a
  E121: Undefined variable: a
  E15: Invalid expression: a


BTW, because of its own scope, the following code doesn't work:

  let list = [ /* some data */ ]
  let l:threshold = 10
  filter(list, {idx, val -> val > l:threshold})

As ZyX pointed out, function() can be used (but redundant):

  filter(list, function({th, idx, val -> val > th}, [l:threshold]))

Closure might be useful for this, but it would be the next step.


Regards,
Ken Takata

-- 
-- 
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 vim_dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui