Home Explore Blog CI



neovim

30th chunk of `runtime/doc/vimfn.txt`
a3d6c40fc51c0122c61577b3704495387517598a908b756c0000000100000fb7
 filter(mydict, 'v:key >= 8')
<		Removes the items with a key below 8. >vim
			call filter(var, 0)
<		Removes all the items, thus clears the |List| or |Dictionary|.

		Note that {expr2} is the result of expression and is then
		used as an expression again.  Often it is good to use a
		|literal-string| to avoid having to double backslashes.

		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: >vim
			func Odd(idx, val)
			  return a:idx % 2 == 1
			endfunc
			call filter(mylist, function('Odd'))
<		It is shorter when using a |lambda|: >vim
			call filter(myList, {idx, val -> idx * val <= 42})
<		If you do not use "val" you can leave it out: >vim
			call filter(myList, {idx -> idx % 2 == 1})
<
		For a |List| and a |Dictionary| the operation is done
		in-place.  If you want it to remain unmodified make a copy
		first: >vim
			let l = filter(copy(mylist), 'v:val =~ "KEEP"')

<		Returns {expr1}, the |List| or |Dictionary| that was filtered,
		or a new |Blob| or |String|.
		When an error is encountered while evaluating {expr2} no
		further items in {expr1} are processed.
		When {expr2} is a Funcref errors inside a function are ignored,
		unless it was defined with the "abort" flag.

                Parameters: ~
                  • {expr1} (`string|table`)
                  • {expr2} (`string|function`)

                Return: ~
                  (`any`)

finddir({name} [, {path} [, {count}]])                               *finddir()*
		Find directory {name} in {path}.  Supports both downwards and
		upwards recursive directory searches.  See |file-searching|
		for the syntax of {path}.

		Returns the path of the first found match.  When the found
		directory is below the current directory a relative path is
		returned.  Otherwise a full path is returned.
		If {path} is omitted or empty then 'path' is used.

		If the optional {count} is given, find {count}'s occurrence of
		{name} in {path} instead of the first one.
		When {count} is negative return all the matches in a |List|.

		Returns an empty string if the directory is not found.

		This is quite similar to the ex-command `:find`.

                Parameters: ~
                  • {name} (`string`)
                  • {path} (`string?`)
                  • {count} (`integer?`)

                Return: ~
                  (`string|string[]`)

findfile({name} [, {path} [, {count}]])                             *findfile()*
		Just like |finddir()|, but find a file instead of a directory.
		Uses 'suffixesadd'.
		Example: >vim
			echo findfile("tags.vim", ".;")
<		Searches from the directory of the current file upwards until
		it finds the file "tags.vim".

                Parameters: ~
                  • {name} (`string`)
                  • {path} (`string?`)
                  • {count} (`integer?`)

                Return: ~
                  (`string|string[]`)

flatten({list} [, {maxdepth}])                                       *flatten()*
		Flatten {list} up to {maxdepth} levels.  Without {maxdepth}
		the result is a |List| without nesting, as if {maxdepth} is
		a very large number.
		The {list} is changed in place, use |flattennew()| if you do
		not want that.
								*E900*
		{maxdepth} means how deep in nested lists changes are made.
		{list} is not modified when {maxdepth} is 0.
		{maxdepth} must be positive number.

		If there is an error the number zero is returned.

		Example: >vim
			echo flatten([1, [2, [3, 4]], 5])
<			[1, 2, 3, 4, 5] >vim
			echo flatten([1, [2, [3, 4]], 5], 1)
<			[1, 2, [3, 4], 5]

                Parameters: ~
                  • {list} (`any[]`)
                  • {maxdepth} (`integer?`)

                Return: ~
                  (`any[]|0`)

flattennew({list} [, {maxdepth}])                                 *flattennew()*
		Like |flatten()| but first

Title: Vimscript Functions: `filter()` (continued), `finddir()`, `findfile()`, `flatten()`, and `flattennew()`
Summary
This section continues the explanation of the `filter()` function, illustrating its in-place operation and return value, including scenarios with Funcrefs and errors. It then describes `finddir()`, which searches for a directory in a specified path, and `findfile()`, which does the same for a file. `flatten()` flattens a list up to a specified depth, modifying it in place, while `flattennew()` does the same but returns a new flattened list.