Re: [bug] lambda expressions

2016-09-26 Fir de Conversatie Bram Moolenaar

Ken Takata wrote:

> > How about this:
> > 
> > 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.
> > The function must return |TRUE| if the item should be kept.
> > Example that keeps the odd items of a list: >
> > func Odd(idx, val)
> >   return a:idx % 2 == 1
> > endfunc
> > call filter(mylist, function('Odd'))
> > <   It is shorter when using a |lambda|: >
> > call filter(myList, {idx, val -> idx * val <= 42})
> > <   If you do not use "val" you can leave it out: >
> > call filter(myList, {idx -> idx % 2 == 1})
> 
> I found a typo.
> Please check the attached patch.

Thanks.

-- 
Q: How does a UNIX Guru do Sex ?
A: unzip;strip;touch;finger;mount;fsck;more;yes;umount;sleep

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


Re: [bug] lambda expressions

2016-09-25 Fir de Conversatie Ken Takata
Hi Bram,

2016/9/23 Fri 3:46:32 UTC+9 Bram Moolenaar wrote:
> Ken Takata wrote:
> 
> > 2016/9/22 Thu 2:37:29 UTC+9 Bram Moolenaar wrote:
> > > Christian Brabandt wrote:
> > > 
> > > > Am 2016-09-21 00:00, schrieb Ken Takata:
> > > > > Hi Christian,
> > > > > 
> > > > > 2016/9/21 Wed 5:31:26 UTC+9 Christian Brabandt wrote:
> > > > >> Hi,
> > > > >> I think I found a bug with lambda expressions.
> > > > >> 
> > > > >> I was looking into writing some automated tests and was trying to use
> > > > >> the new lambda expressions. However this does not work as expected:
> > > > >> 
> > > > >> Here is an example:
> > > > >> #v+
> > > > >>   let a = ['FOOBAR"word"', 'FOOBAR"word2"']
> > > > >>   let pat='^FOOBAR\s\+\zs"[^"]\+"'
> > > > >>   let pat2='^FOOBAR\s\+\("[^"]\+"\)'
> > > > >>   :echo map(copy(a), 'matchstr(v:val, g:pat)')
> > > > >>   -> result ['"word"', '"word2"']
> > > > >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > > > >>   -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
> > > > >>   :echo map(copy(a), 'substitute(v:val, g:pat2, 
> > > > >> ''\=submatch(1)'',"")')
> > > > >>   -> result ['"word"', '"word2"']
> > > > >>   :echo map(copy(a), {val -> substitute(val, g:pat2, 
> > > > >> '\=submatch(1)', 
> > > > >> '')})
> > > > >>   -> BUG: result ['0', '1'], expected ['"word", '"word2"']
> > > > >> #v-
> > > > > 
> > > > > This is not a bug. map() always passes two arguments (key and val) to 
> > > > > the
> > > > > specified Funcref. So,
> > > > > 
> > > > >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > > > > 
> > > > > this should be:
> > > > > 
> > > > >   :echo map(copy(a), {key, val -> matchstr(val, g:pat)})
> > > > > 
> > > > > Or you can still use v:val:
> > > > > 
> > > > >   :echo map(copy(a), {-> matchstr(v:val, g:pat)})
> > > > 
> > > > Hm, should there be an error when only one argument is given?
> > > > Or at least it should be more stressed in the documentatio, that two
> > > > arguments are expected.
> > > 
> > > How about extending the example in the help:
> > > 
> > >   If {expr2} is a |Funcref| it is called with two arguments:
> > >   1. The key or the index of the current item.
> > >   2. the value of the current item.
> > >   The function must return the new value of the item. Example
> > >   that changes each value by "key-value": >
> > >   func KeyValue(key, val)
> > > return a:key . '-' . a:val
> > >   endfunc
> > >   call map(myDict, function('KeyValue'))
> > > < It is shorter when using a |lambda|: >
> > >   call map(myDict, {key, val -> key . '-' . val})
> > > < If you do not use "val" you can leave it out: >
> > >   call map(myDict, {key -> 'item: ' . key})
> > 
> > A similar update might be needed for `:help filter()`.
> 
> How about this:
> 
>   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.
>   The function must return |TRUE| if the item should be kept.
>   Example that keeps the odd items of a list: >
>   func Odd(idx, val)
> return a:idx % 2 == 1
>   endfunc
>   call filter(mylist, function('Odd'))
> < It is shorter when using a |lambda|: >
>   call filter(myList, {idx, val -> idx * val <= 42})
> < If you do not use "val" you can leave it out: >
>   call filter(myList, {idx -> idx % 2 == 1})

I found a typo.
Please check the attached patch.

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.
# HG changeset patch
# Parent  483030e5352aa4f69a442102bfc486ee3701166b

diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -3732,7 +3732,7 @@ filter({expr1}, {expr2})*filter()*
 			call filter(myList, {idx, val -> idx * val <= 42})
 <		If you do not use "val" you can leave it out: >
 			call filter(myList, {idx -> idx % 2 == 1})
-
+<
 		The operation is done in-place.  If you want a |List| or
 		|Dictionary| to remain unmodified make a copy first: >
 			:let l = filter(copy(mylist), 'v:val =~ "KEEP"')


Re: [bug] lambda expressions

2016-09-22 Fir de Conversatie Bram Moolenaar

Ken Takata wrote:

> 2016/9/22 Thu 2:37:29 UTC+9 Bram Moolenaar wrote:
> > Christian Brabandt wrote:
> > 
> > > Am 2016-09-21 00:00, schrieb Ken Takata:
> > > > Hi Christian,
> > > > 
> > > > 2016/9/21 Wed 5:31:26 UTC+9 Christian Brabandt wrote:
> > > >> Hi,
> > > >> I think I found a bug with lambda expressions.
> > > >> 
> > > >> I was looking into writing some automated tests and was trying to use
> > > >> the new lambda expressions. However this does not work as expected:
> > > >> 
> > > >> Here is an example:
> > > >> #v+
> > > >>   let a = ['FOOBAR"word"', 'FOOBAR"word2"']
> > > >>   let pat='^FOOBAR\s\+\zs"[^"]\+"'
> > > >>   let pat2='^FOOBAR\s\+\("[^"]\+"\)'
> > > >>   :echo map(copy(a), 'matchstr(v:val, g:pat)')
> > > >>   -> result ['"word"', '"word2"']
> > > >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > > >>   -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
> > > >>   :echo map(copy(a), 'substitute(v:val, g:pat2, 
> > > >> ''\=submatch(1)'',"")')
> > > >>   -> result ['"word"', '"word2"']
> > > >>   :echo map(copy(a), {val -> substitute(val, g:pat2, '\=submatch(1)', 
> > > >> '')})
> > > >>   -> BUG: result ['0', '1'], expected ['"word", '"word2"']
> > > >> #v-
> > > > 
> > > > This is not a bug. map() always passes two arguments (key and val) to 
> > > > the
> > > > specified Funcref. So,
> > > > 
> > > >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > > > 
> > > > this should be:
> > > > 
> > > >   :echo map(copy(a), {key, val -> matchstr(val, g:pat)})
> > > > 
> > > > Or you can still use v:val:
> > > > 
> > > >   :echo map(copy(a), {-> matchstr(v:val, g:pat)})
> > > 
> > > Hm, should there be an error when only one argument is given?
> > > Or at least it should be more stressed in the documentatio, that two
> > > arguments are expected.
> > 
> > How about extending the example in the help:
> > 
> > If {expr2} is a |Funcref| it is called with two arguments:
> > 1. The key or the index of the current item.
> > 2. the value of the current item.
> > The function must return the new value of the item. Example
> > that changes each value by "key-value": >
> > func KeyValue(key, val)
> >   return a:key . '-' . a:val
> > endfunc
> > call map(myDict, function('KeyValue'))
> > <   It is shorter when using a |lambda|: >
> > call map(myDict, {key, val -> key . '-' . val})
> > <   If you do not use "val" you can leave it out: >
> > call map(myDict, {key -> 'item: ' . key})
> 
> A similar update might be needed for `:help filter()`.

How about this:

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.
The function must return |TRUE| if the item should be kept.
Example that keeps the odd items of a list: >
func Odd(idx, val)
  return a:idx % 2 == 1
endfunc
call filter(mylist, function('Odd'))
<   It is shorter when using a |lambda|: >
call filter(myList, {idx, val -> idx * val <= 42})
<   If you do not use "val" you can leave it out: >
call filter(myList, {idx -> idx % 2 == 1})

-- 
   "To whoever finds this note -
   I have been imprisoned by my father who wishes me to marry
   against my will.  Please please please please come and rescue me.
   I am in the tall tower of Swamp Castle."
   SIR LAUNCELOT's eyes light up with holy inspiration.
 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

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


Re: [bug] lambda expressions

2016-09-21 Fir de Conversatie Ken Takata
Hi Bram,

2016/9/22 Thu 2:37:29 UTC+9 Bram Moolenaar wrote:
> Christian Brabandt wrote:
> 
> > Am 2016-09-21 00:00, schrieb Ken Takata:
> > > Hi Christian,
> > > 
> > > 2016/9/21 Wed 5:31:26 UTC+9 Christian Brabandt wrote:
> > >> Hi,
> > >> I think I found a bug with lambda expressions.
> > >> 
> > >> I was looking into writing some automated tests and was trying to use
> > >> the new lambda expressions. However this does not work as expected:
> > >> 
> > >> Here is an example:
> > >> #v+
> > >>   let a = ['FOOBAR"word"', 'FOOBAR"word2"']
> > >>   let pat='^FOOBAR\s\+\zs"[^"]\+"'
> > >>   let pat2='^FOOBAR\s\+\("[^"]\+"\)'
> > >>   :echo map(copy(a), 'matchstr(v:val, g:pat)')
> > >>   -> result ['"word"', '"word2"']
> > >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > >>   -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
> > >>   :echo map(copy(a), 'substitute(v:val, g:pat2, 
> > >> ''\=submatch(1)'',"")')
> > >>   -> result ['"word"', '"word2"']
> > >>   :echo map(copy(a), {val -> substitute(val, g:pat2, '\=submatch(1)', 
> > >> '')})
> > >>   -> BUG: result ['0', '1'], expected ['"word", '"word2"']
> > >> #v-
> > > 
> > > This is not a bug. map() always passes two arguments (key and val) to 
> > > the
> > > specified Funcref. So,
> > > 
> > >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > > 
> > > this should be:
> > > 
> > >   :echo map(copy(a), {key, val -> matchstr(val, g:pat)})
> > > 
> > > Or you can still use v:val:
> > > 
> > >   :echo map(copy(a), {-> matchstr(v:val, g:pat)})
> > 
> > Hm, should there be an error when only one argument is given?
> > Or at least it should be more stressed in the documentatio, that two
> > arguments are expected.
> 
> How about extending the example in the help:
> 
>   If {expr2} is a |Funcref| it is called with two arguments:
>   1. The key or the index of the current item.
>   2. the value of the current item.
>   The function must return the new value of the item. Example
>   that changes each value by "key-value": >
>   func KeyValue(key, val)
> return a:key . '-' . a:val
>   endfunc
>   call map(myDict, function('KeyValue'))
> < It is shorter when using a |lambda|: >
>   call map(myDict, {key, val -> key . '-' . val})
> < If you do not use "val" you can leave it out: >
>   call map(myDict, {key -> 'item: ' . key})

A similar update might be needed for `:help filter()`.

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.


Re: [bug] lambda expressions

2016-09-21 Fir de Conversatie Bram Moolenaar

Christian Brabandt wrote:

> Am 2016-09-21 00:00, schrieb Ken Takata:
> > Hi Christian,
> > 
> > 2016/9/21 Wed 5:31:26 UTC+9 Christian Brabandt wrote:
> >> Hi,
> >> I think I found a bug with lambda expressions.
> >> 
> >> I was looking into writing some automated tests and was trying to use
> >> the new lambda expressions. However this does not work as expected:
> >> 
> >> Here is an example:
> >> #v+
> >>   let a = ['FOOBAR"word"', 'FOOBAR"word2"']
> >>   let pat='^FOOBAR\s\+\zs"[^"]\+"'
> >>   let pat2='^FOOBAR\s\+\("[^"]\+"\)'
> >>   :echo map(copy(a), 'matchstr(v:val, g:pat)')
> >>   -> result ['"word"', '"word2"']
> >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> >>   -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
> >>   :echo map(copy(a), 'substitute(v:val, g:pat2, 
> >> ''\=submatch(1)'',"")')
> >>   -> result ['"word"', '"word2"']
> >>   :echo map(copy(a), {val -> substitute(val, g:pat2, '\=submatch(1)', 
> >> '')})
> >>   -> BUG: result ['0', '1'], expected ['"word", '"word2"']
> >> #v-
> > 
> > This is not a bug. map() always passes two arguments (key and val) to 
> > the
> > specified Funcref. So,
> > 
> >>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
> > 
> > this should be:
> > 
> >   :echo map(copy(a), {key, val -> matchstr(val, g:pat)})
> > 
> > Or you can still use v:val:
> > 
> >   :echo map(copy(a), {-> matchstr(v:val, g:pat)})
> 
> Hm, should there be an error when only one argument is given?
> Or at least it should be more stressed in the documentatio, that two
> arguments are expected.

How about extending the example in the help:

If {expr2} is a |Funcref| it is called with two arguments:
1. The key or the index of the current item.
2. the value of the current item.
The function must return the new value of the item. Example
that changes each value by "key-value": >
func KeyValue(key, val)
  return a:key . '-' . a:val
endfunc
call map(myDict, function('KeyValue'))
<   It is shorter when using a |lambda|: >
call map(myDict, {key, val -> key . '-' . val})
<   If you do not use "val" you can leave it out: >
call map(myDict, {key -> 'item: ' . key})

-- 
Eight Megabytes And Continually Swapping.

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


Re: [bug] lambda expressions

2016-09-21 Fir de Conversatie Christian Brabandt

Am 2016-09-21 00:00, schrieb Ken Takata:

Hi Christian,

2016/9/21 Wed 5:31:26 UTC+9 Christian Brabandt wrote:

Hi,
I think I found a bug with lambda expressions.

I was looking into writing some automated tests and was trying to use
the new lambda expressions. However this does not work as expected:

Here is an example:
#v+
  let a = ['FOOBAR"word"', 'FOOBAR"word2"']
  let pat='^FOOBAR\s\+\zs"[^"]\+"'
  let pat2='^FOOBAR\s\+\("[^"]\+"\)'
  :echo map(copy(a), 'matchstr(v:val, g:pat)')
  -> result ['"word"', '"word2"']
  :echo map(copy(a), {val -> matchstr(val, g:pat)})
  -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
  :echo map(copy(a), 'substitute(v:val, g:pat2, 
''\=submatch(1)'',"")')

  -> result ['"word"', '"word2"']
  :echo map(copy(a), {val -> substitute(val, g:pat2, '\=submatch(1)', 
'')})

  -> BUG: result ['0', '1'], expected ['"word", '"word2"']
#v-


This is not a bug. map() always passes two arguments (key and val) to 
the

specified Funcref. So,


  :echo map(copy(a), {val -> matchstr(val, g:pat)})


this should be:

  :echo map(copy(a), {key, val -> matchstr(val, g:pat)})

Or you can still use v:val:

  :echo map(copy(a), {-> matchstr(v:val, g:pat)})


Hm, should there be an error when only one argument is given?
Or at least it should be more stressed in the documentatio, that two 
arguments

are expected.

Best,
Christian

--
--
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.


Re: [bug] lambda expressions

2016-09-20 Fir de Conversatie Ken Takata
Hi Christian,

2016/9/21 Wed 5:31:26 UTC+9 Christian Brabandt wrote:
> Hi,
> I think I found a bug with lambda expressions.
> 
> I was looking into writing some automated tests and was trying to use 
> the new lambda expressions. However this does not work as expected:
> 
> Here is an example:
> #v+
>   let a = ['FOOBAR"word"', 'FOOBAR"word2"']
>   let pat='^FOOBAR\s\+\zs"[^"]\+"'
>   let pat2='^FOOBAR\s\+\("[^"]\+"\)'
>   :echo map(copy(a), 'matchstr(v:val, g:pat)')
>   -> result ['"word"', '"word2"']
>   :echo map(copy(a), {val -> matchstr(val, g:pat)})
>   -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
>   :echo map(copy(a), 'substitute(v:val, g:pat2, ''\=submatch(1)'',"")')
>   -> result ['"word"', '"word2"']
>   :echo map(copy(a), {val -> substitute(val, g:pat2, '\=submatch(1)', '')})
>   -> BUG: result ['0', '1'], expected ['"word", '"word2"']
> #v-

This is not a bug. map() always passes two arguments (key and val) to the
specified Funcref. So,

>   :echo map(copy(a), {val -> matchstr(val, g:pat)})

this should be:

  :echo map(copy(a), {key, val -> matchstr(val, g:pat)})

Or you can still use v:val:

  :echo map(copy(a), {-> matchstr(v:val, g:pat)})

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.


[bug] lambda expressions

2016-09-20 Fir de Conversatie Christian Brabandt
Hi,
I think I found a bug with lambda expressions.

I was looking into writing some automated tests and was trying to use 
the new lambda expressions. However this does not work as expected:

Here is an example:
#v+
  let a = ['FOOBAR"word"', 'FOOBAR"word2"']
  let pat='^FOOBAR\s\+\zs"[^"]\+"'
  let pat2='^FOOBAR\s\+\("[^"]\+"\)'
  :echo map(copy(a), 'matchstr(v:val, g:pat)')
  -> result ['"word"', '"word2"']
  :echo map(copy(a), {val -> matchstr(val, g:pat)})
  -> BUG: result ['""', '""'], expected ['"word"', '"word2"']
  :echo map(copy(a), 'substitute(v:val, g:pat2, ''\=submatch(1)'',"")')
  -> result ['"word"', '"word2"']
  :echo map(copy(a), {val -> substitute(val, g:pat2, '\=submatch(1)', '')})
  -> BUG: result ['0', '1'], expected ['"word", '"word2"']
#v-



Best,
Christian
-- 
In den ersten Lebensjahren eines Kindes bringen ihm die Eltern Gehen
und Sprechen bei, in den späteren verlangen sie dann, daß es
stillsitzt und den Mund hält.
-- Johann Nepomuk Nestroy

-- 
-- 
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.