Home Explore Blog CI



neovim

55th chunk of `runtime/doc/lua.txt`
5db4b8264a9637444a7ae69f98c9c7d3668843dc17f8259d0000000100000fb3
 Parameters: ~
      • {f}  (`fun(...)`) Function to execute for each item in the pipeline.
             Takes all of the values returned by the previous stage in the
             pipeline as arguments.

Iter:enumerate()                                            *Iter:enumerate()*
    Yields the item index (count) and value for each item of an iterator
    pipeline.

    For list tables, this is more efficient: >lua
        vim.iter(ipairs(t))
<

    instead of: >lua
        vim.iter(t):enumerate()
<

    Example: >lua

        local it = vim.iter(vim.gsplit('abc', '')):enumerate()
        it:next()
        -- 1	'a'
        it:next()
        -- 2	'b'
        it:next()
        -- 3	'c'
<

    Return: ~
        (`Iter`)

Iter:filter({f})                                               *Iter:filter()*
    Filters an iterator pipeline.

    Example: >lua
        local bufs = vim.iter(vim.api.nvim_list_bufs()):filter(vim.api.nvim_buf_is_loaded)
<

    Parameters: ~
      • {f}  (`fun(...):boolean`) Takes all values returned from the previous
             stage in the pipeline and returns false or nil if the current
             iterator element should be removed.

    Return: ~
        (`Iter`)

Iter:find({f})                                                   *Iter:find()*
    Find the first value in the iterator that satisfies the given predicate.

    Advances the iterator. Returns nil and drains the iterator if no value is
    found.

    Examples: >lua

        local it = vim.iter({ 3, 6, 9, 12 })
        it:find(12)
        -- 12

        local it = vim.iter({ 3, 6, 9, 12 })
        it:find(20)
        -- nil

        local it = vim.iter({ 3, 6, 9, 12 })
        it:find(function(v) return v % 4 == 0 end)
        -- 12
<

    Parameters: ~
      • {f}  (`any`)

    Return: ~
        (`any`)

Iter:flatten({depth})                                         *Iter:flatten()*
    Flattens a |list-iterator|, un-nesting nested values up to the given
    {depth}. Errors if it attempts to flatten a dict-like value.

    Examples: >lua
        vim.iter({ 1, { 2 }, { { 3 } } }):flatten():totable()
        -- { 1, 2, { 3 } }

        vim.iter({1, { { a = 2 } }, { 3 } }):flatten():totable()
        -- { 1, { a = 2 }, 3 }

        vim.iter({ 1, { { a = 2 } }, { 3 } }):flatten(math.huge):totable()
        -- error: attempt to flatten a dict-like table
<

    Parameters: ~
      • {depth}  (`number?`) Depth to which |list-iterator| should be
                 flattened (defaults to 1)

    Return: ~
        (`Iter`)

Iter:fold({init}, {f})                                           *Iter:fold()*
    Folds ("reduces") an iterator into a single value.         *Iter:reduce()*

    Examples: >lua
        -- Create a new table with only even values
        vim.iter({ a = 1, b = 2, c = 3, d = 4 })
          :filter(function(k, v) return v % 2 == 0 end)
          :fold({}, function(acc, k, v)
            acc[k] = v
            return acc
          end) --> { b = 2, d = 4 }

        -- Get the "maximum" item of an iterable.
        vim.iter({ -99, -4, 3, 42, 0, 0, 7 })
          :fold({}, function(acc, v)
            acc.max = math.max(v, acc.max or v)
            return acc
          end) --> { max = 42 }
<

    Parameters: ~
      • {init}  (`any`) Initial value of the accumulator.
      • {f}     (`fun(acc:A, ...):A`) Accumulation function.

    Return: ~
        (`any`)

Iter:join({delim})                                               *Iter:join()*
    Collect the iterator into a delimited string.

    Each element in the iterator is joined into a string separated by {delim}.

    Consumes the iterator.

    Parameters: ~
      • {delim}  (`string`) Delimiter

    Return: ~
        (`string`)

Iter:last()                                                      *Iter:last()*
    Drains the iterator and returns the last item.

    Example: >lua

        local it = vim.iter(vim.gsplit('abcdefg', ''))
        it:last()
        -- 'g'

        local it = vim.iter({

Title: Iter:filter, Iter:find, Iter:flatten, Iter:fold, Iter:join, Iter:last
Summary
This section details the functions available for iterators, including `Iter:filter`, `Iter:find`, `Iter:flatten`, `Iter:fold`, `Iter:join`, and `Iter:last`. Each function is described with its parameters, return values, and examples. `Iter:filter` removes elements based on a given function, `Iter:find` returns the first value that satisfies a predicate, `Iter:flatten` unnests nested values, `Iter:fold` reduces the iterator to a single value, `Iter:join` concatenates elements into a delimited string, and `Iter:last` drains the iterator and returns the last item.