Ken Takata wrote:
> 2016/7/12 Tue 4:59:40 UTC+9 Bram Moolenaar wrote:
> > Ken Takata wrote:
> >
> > > > [...]
> > > >
> > > > > > I do think that the backslash (or any other special character that
> > > > > > might
> > > > > > be available) makes it ugly.
> > > > > >
> > > > > > Although we can find the end of the expression without the "}",
> > > > > > mistakes
> > > > > > will be very hard to figure out. Also reading back a lambda that's
> > > > > > passed as an agument to a function with many arguments will make
> > > > > > the {}
> > > > > > part easy to spot. I have done this in Zimbu and I'm happy with
> > > > > > how it
> > > > > > looks.
> > > > > >
> > > > > > I don't think it's hard to implement: When finding a "{" look
> > > > > > forward,
> > > > > > skipping over [a-zA-Z0-9 ], if "->" is found it's a lambda.
> > > > >
> > > > > Here is an experimental patch:
> > > > >
> > > > > https://bitbucket.org/k_takata/vim-ktakata-mq/src/90ea467da862bc2eaf59454f75b8faea34604a1e/lambda2.patch?fileviewer=file-view-default
> > > > >
> > > > > * Support only one expression.
> > > > > * The "a:" prefix cannot be omitted.
> > > > > * No closures.
> > > > > * Inefficient "{" parser.
> > > > >
> > > > > Both mattn's patch and ZyX's code are partly used.
> > > > > Nested lambdas are also supported like ZyX's example:
> > > > >
> > > > > :echo {a -> {tmp -> [a:tmp, a:tmp + 2, a:tmp + 5]}(a:a * 5)}(2)
> > > > > [10, 12, 15]
> > > > >
> > > > > Hmm, it's hard to read.
> > > >
> > > > Thanks for making the patch. I think at a high level it looks good.
> > > >
> > > > Like I mentioned, I don't think we should require "a:arg", just "arg".
> > > > Perhaps in the function, instead of using a dict for local variables,
> > > > use the dict for arguments. After all, there are no local variables.
> > > >
> > > > Seeing the documentation, and perhaps a few tests, may trigger some fine
> > > > tuning. So I hope you can do that.
> > >
> > > I have updated the experimental patch:
> > > https://bitbucket.org/k_takata/vim-ktakata-mq/src/6d421eab8842ba93c115d57c42fd9eabfa328851/lambda2.patch?fileviewer=file-view-default
> > >
> > > Changes from the previous patches are:
> > >
> > > * Supports both "a:arg" and "arg". Named arguments are also added to the
> > > "l:"
> > > dictionary.
> > > * Document update.
> >
> > Thanks for the update.
> >
> > > Maybe the following patch is also needed:
> > > https://bitbucket.org/k_takata/vim-ktakata-mq/src/6d421eab8842ba93c115d57c42fd9eabfa328851/func-refcount.patch?fileviewer=file-view-default
> > >
> > > This fix the reference counters for callbacks.
> >
> > I'll include that now. The vim_free() can also be moved into the
> > function. And there is some more repeated statements that can be moved
> > into a function.
> >
> > > Lambda examples:
> > > :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]
> > >
> > > :echo {a -> {tmp -> [tmp, tmp + 2, tmp + 5]}(a * 5)}(2)
> > > [10, 12, 15]
> > >
> > > :let timer = timer_start(500,
> > > \ {-> execute("echo 'Handler called'", "")},
> > > \ {'repeat': 3})
> > > Handler called
> > > Handler called
> > > Handler called
> >
> > I like it.
> >
> > The trick with execute() is interesting, so a lambda can execute
> > commands. But with some limitations, esp. about returning a string.
> >
> > So how about tests?
>
> Added.
> https://bitbucket.org/k_takata/vim-ktakata-mq/src/02b4123c6ec5c56dfd908a166cc33fb378eaf785/lambda2.patch?fileviewer=file-view-default
Thanks! Unless there is a better proposal soon I'll include this.
> When I was adding the tests, I found that a test caused SEGV.
> I use copy_tv() to copy the "a:" arguments to the "l:" dictionary to fix this.
> I'm not sure this is the best solution.
That's probably the right way. The only other way I can think of is
using the same dictionary for a: and l:.
--
press CTRL-ALT-DEL for more information
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
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.