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
" 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.
Thanks
https://github.com/vim/vim/pull/3676
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
Merging #3676 into master will increase coverage by
0.01%.
The diff coverage is97.36%.
@@ Coverage Diff @@ ## master #3676 +/- ## ========================================== + Coverage 77.41% 77.42% +0.01% ========================================== Files 99 99 Lines 139440 139478 +38 ========================================== + Hits 107943 107989 +46 + Misses 31497 31489 -8
| Impacted Files | Coverage Δ | |
|---|---|---|
| src/evalfunc.c | 87.22% <100%> (+0.03%) |
⬆️ |
| src/list.c | 89.41% <95.23%> (+0.3%) |
⬆️ |
| src/gui.c | 57.63% <0%> (-0.57%) |
⬇️ |
| src/ex_cmds2.c | 84.49% <0%> (-0.1%) |
⬇️ |
| src/if_xcmdsrv.c | 84.17% <0%> (ø) |
⬆️ |
| src/window.c | 82.91% <0%> (+0.03%) |
⬆️ |
| src/gui_gtk_x11.c | 49.28% <0%> (+1.03%) |
⬆️ |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered by Codecov. Last update 5393281...4b17de2. Read the comment docs.
@brammool Thanks for your comment.
I would expect the most often used form is to flatten a nested list into a list of items.
Yes, I think so too.
The documentation about maxdepth is not enough or the name maxdepth is not suitable.
Some examples are here
It is called limit in case of vital.vim.
echo s:L.flatten([[['a']], [[['b']], 'c']], 2) " => ['a', ['b'], 'c']
It is called level in case of Ruby's flatten.
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.
The name maxdepth may be required to change such as limit, level, expand_depth or etc.
@mopp pushed 2 commits.
—
You are receiving this because you are subscribed to this thread.
@andymass commented on this pull request.
In src/list.c:
> + * Returns FAIL when out of memory.
+ */
+ int
+list_flatten(list_T *list, long maxdepth)
+{
+ listitem_T *item;
+ int n;
+
+ if (maxdepth == 0)
+ return OK;
+
+ n = 0;
+ item = list->lv_first;
+ while (item != NULL && !got_int)
+ {
+ line_breakcheck();
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.
@andymass commented on this pull request.
> @@ -4034,6 +4035,19 @@ findfile({name} [, {path} [, {count}]]) *findfile()*
< Searches from the directory of the current file upwards until
it finds the file "tags.vim".
+flatten({list} [, {maxdepth}]) *flatten()*
+ Flatten {list} to depth {maxdepth}. {maxdepth} means how
It should be noted that flattening is done in-place, i.e., modifies the list (as other vim list functions)
> @@ -4034,6 +4035,19 @@ findfile({name} [, {path} [, {count}]]) *findfile()*
< Searches from the directory of the current file upwards until
it finds the file "tags.vim".
+flatten({list} [, {maxdepth}]) *flatten()*
+ Flatten {list} to depth {maxdepth}. {maxdepth} means how
+ deeply it makes flatten the list. the depth is 1 when
+ {maxdepth} is omitted. {list} is not modified when
+ {maxdepth} is 0.
+ Example: >
+ :echo flatten([1, [2], 3])
+< [1, 2, 3] >
+ :echo flatten([[[1], [2], [3]]], 1)
+< [[1], [2], [3]]
+ *E964*
+ {maxdepth} must be positive number.
Since zero is explicitly allowed (why?), this should say "must be a non-negative Number."
> @@ -4034,6 +4035,19 @@ findfile({name} [, {path} [, {count}]]) *findfile()*
< Searches from the directory of the current file upwards until
it finds the file "tags.vim".
+flatten({list} [, {maxdepth}]) *flatten()*
+ Flatten {list} to depth {maxdepth}. {maxdepth} means how
+ deeply it makes flatten the list. the depth is 1 when
This is not completely clear. Is it correct to say that the result is the depth of the list, 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)?
@mopp commented on this pull request.
In src/list.c:
> + * Returns FAIL when out of memory.
+ */
+ int
+list_flatten(list_T *list, long maxdepth)
+{
+ listitem_T *item;
+ int n;
+
+ if (maxdepth == 0)
+ return OK;
+
+ n = 0;
+ item = list->lv_first;
+ while (item != NULL && !got_int)
+ {
+ line_breakcheck();
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.
@mopp commented on this pull request.
> @@ -4034,6 +4035,19 @@ findfile({name} [, {path} [, {count}]]) *findfile()*
< Searches from the directory of the current file upwards until
it finds the file "tags.vim".
+flatten({list} [, {maxdepth}]) *flatten()*
+ Flatten {list} to depth {maxdepth}. {maxdepth} means how
+ deeply it makes flatten the list. the depth is 1 when
+ {maxdepth} is omitted. {list} is not modified when
+ {maxdepth} is 0.
+ Example: >
+ :echo flatten([1, [2], 3])
+< [1, 2, 3] >
+ :echo flatten([[[1], [2], [3]]], 1)
+< [[1], [2], [3]]
+ *E964*
+ {maxdepth} must be positive number.
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.
@mopp commented on this pull request.
> @@ -4034,6 +4035,19 @@ findfile({name} [, {path} [, {count}]]) *findfile()*
< Searches from the directory of the current file upwards until
it finds the file "tags.vim".
+flatten({list} [, {maxdepth}]) *flatten()*
+ Flatten {list} to depth {maxdepth}. {maxdepth} means how
+ deeply it makes flatten the list. the depth is 1 when
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 😄
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
Thank you for merging! 🎉
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
> Thank you for merging! :tada:
I did change the default depth from 1 to "max". It think it makes more
sense.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()