Conversation
Codecov Report
@@ Coverage Diff @@
## master #3676 +/- ##
==========================================
- Coverage 78.47% 78.47% -0.01%
==========================================
Files 103 103
Lines 141874 141913 +39
==========================================
+ Hits 111333 111362 +29
- Misses 30541 30551 +10
Continue to review full report at Codecov.
|
|
Mopp wrote:
This change add new small function `flatten({list} [, {maxdepth}])` to
flatten the given list to depth `{maxdepth}`.
This function assists list processing combining list manipulation functions.
Here is example
```vim
" Collect paths from some patterns
echo flatten(map(['./xxx/*', './yyy/*'], 'glob(v:val, 0, 1)'))
" Split and filter strings
echo filter(flatten(map(zzz, 'split(v:val, ",")')), '!empty(v:val)')
```
The performance is good because it uses loop instead of recursive
function call.
I'm a bit confused by the maxdepth argument. I would expect it to
specify the maximum depth of the resulting list, thus flatten(list, 1)
would always result in items that are not a list. But it appears it
flattens by one level, thus if one of the items is a list of lists, the
item becomes a list.
Isn't it more useful to specify the maximum resulting depth? With the
default being 1 this would result in a list of items:
flatten([[[1, 2], 3, [4]], [5, 6]])
gives
[1, 2, 3, 4, 5, 6]
And
flatten([[[1, 2], 3, [4]], [5, 6]], 2)
gives
[[1, 2, 3, 4], [5, 6]]
That would work for two-dimensional arrays.
I would expect the most often used form is to flatten a nested list into
a list of items.
…--
BLACK KNIGHT: The Black Knight always triumphs. Have at you!
ARTHUR takes his last leg off. The BLACK KNIGHT's body lands upright.
BLACK KNIGHT: All right, we'll call it a draw.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
/// Bram Moolenaar -- Bram@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 ///
|
|
@brammool Thanks for your comment.
Yes, I think so too. The documentation about It is called echo s:L.flatten([[['a']], [[['b']], 'c']], 2)
" => ['a', ['b'], 'c']It is called a = [ 1, 2, [3, [4, 5] ] ]
a.flatten!(1)
# => [1, 2, 3, [4, 5]]The argument means "How deeply it makes flatten the list" usually. I think it is better to follow them. |
| item = list->lv_first; | ||
| while (item != NULL && !got_int) | ||
| { | ||
| line_breakcheck(); |
There was a problem hiding this comment.
Why do a breakcheck? I think many list processing functions do not check for interrupts. But most importantly, it appears to put the list into an indeterminate state; if the function does return OK I would expect the list to be list flattened to maxdepth.
There was a problem hiding this comment.
it appears to put the list into an indeterminate state; if the function does return OK
Yes, It should return FAIL if interrupted.
I will fix it.
| it finds the file "tags.vim". | ||
|
|
||
| flatten({list} [, {maxdepth}]) *flatten()* | ||
| Flatten {list} to depth {maxdepth}. {maxdepth} means how |
There was a problem hiding this comment.
It should be noted that flattening is done in-place, i.e., modifies the list (as other vim list functions)
| :echo flatten([[[1], [2], [3]]], 1) | ||
| < [[1], [2], [3]] | ||
| *E964* | ||
| {maxdepth} must be positive number. |
There was a problem hiding this comment.
Since zero is explicitly allowed (why?), this should say "must be a non-negative Number."
There was a problem hiding this comment.
This description is right because the given list is NOT modified when the maxdepth is 0.
It can deal with when a calculation result of maxdepth becomes 0.
runtime/doc/eval.txt
Outdated
|
|
||
| flatten({list} [, {maxdepth}]) *flatten()* | ||
| Flatten {list} to depth {maxdepth}. {maxdepth} means how | ||
| deeply it makes flatten the list. the depth is 1 when |
There was a problem hiding this comment.
This is not completely clear. Is it correct to say that the result is a List having depth N, i.e., a depth 1 List contains only Dict, Number, String, etc, whereas a depth 2 List may contain other lists (which do not contain lists themselves)?
There was a problem hiding this comment.
I'm sorry. My English is not good.
Is it correct to say that the result is a List having depth N
I already described at the #3676 (comment) .
Please check src/testdir/test_flatten.vim for more examples.
Thanks for your reviews 😄
|
Thank you for merging! 🎉 |
|
Thank you for merging! 🎉
I did change the default depth from 1 to "max". It think it makes more
sense.
…--
Luxury. We used to have to get out of the lake at three o'clock in the
morning, clean the lake, eat a handful of hot gravel, go to work at the
mill every day for tuppence a month, come home, and Dad would beat us
around the head and neck with a broken bottle, if we were LUCKY!
/// Bram Moolenaar -- Bram@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 ///
|
|
Hi,
On Tue, Jun 9, 2020 at 5:51 AM Bram Moolenaar ***@***.***> wrote:
> Thank you for merging! 🎉
I did change the default depth from 1 to "max". It think it makes more
sense.
The ASAN build in Travis/CI is failing in test_flatten.vim.
- Yegappan
|
Problem: Flattening a list with existing code is slow. Solution: Add flatten(). (Mopp, closes vim/vim#3676) vim/vim@077a1e6
Problem: Flattening a list with existing code is slow. Solution: Add flatten(). (Mopp, closes vim/vim#3676) vim/vim@077a1e6
This change adds a new small function
flatten({list} [, {maxdepth}])to flatten the given list to depth{maxdepth}like Ruby'sflattenand Scala'sflatten.This function assists list processing combining list manipulation functions.
Here is example
The performance is good because it uses a loop instead of a recursive function call.
Some plugins (e.g., gina.vim, vim-snipmake) also need this function.
Currently these use library provided function (e.g., vital.vim, tlib_vim).
Thanks