Home Explore Blog CI



neovim

57th chunk of `runtime/doc/lua.txt`
2fa3e950b0c05ee710b23997b2c56ccc7ea99c4e9f86e7540000000100000fb0
 consuming it.

    Example: >lua

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

    Return: ~
        (`any`)

Iter:pop()                                                        *Iter:pop()*
    "Pops" a value from a |list-iterator| (gets the last value and decrements
    the tail).

    Example: >lua
        local it = vim.iter({1, 2, 3, 4})
        it:pop()
        -- 4
        it:pop()
        -- 3
<

    Return: ~
        (`any`)

Iter:rev()                                                        *Iter:rev()*
    Reverses a |list-iterator| pipeline.

    Example: >lua

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

    Return: ~
        (`Iter`)

Iter:rfind({f})                                                 *Iter:rfind()*
    Gets the first value satisfying a predicate, from the end of a
    |list-iterator|.

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

    Examples: >lua

        local it = vim.iter({ 1, 2, 3, 2, 1 }):enumerate()
        it:rfind(1)
        -- 5	1
        it:rfind(1)
        -- 1	1
<

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

    Return: ~
        (`any`)

    See also: ~
      • |Iter:find()|

Iter:rpeek()                                                    *Iter:rpeek()*
    Gets the last value of a |list-iterator| without consuming it.

    Example: >lua
        local it = vim.iter({1, 2, 3, 4})
        it:rpeek()
        -- 4
        it:rpeek()
        -- 4
        it:pop()
        -- 4
<

    Return: ~
        (`any`)

    See also: ~
      • |Iter:last()|

Iter:rskip({n})                                                 *Iter:rskip()*
    Discards `n` values from the end of a |list-iterator| pipeline.

    Example: >lua
        local it = vim.iter({ 1, 2, 3, 4, 5 }):rskip(2)
        it:next()
        -- 1
        it:pop()
        -- 3
<

    Parameters: ~
      • {n}  (`number`) Number of values to skip.

    Return: ~
        (`Iter`)

Iter:skip({n})                                                   *Iter:skip()*
    Skips `n` values of an iterator pipeline.

    Example: >lua

        local it = vim.iter({ 3, 6, 9, 12 }):skip(2)
        it:next()
        -- 9
<

    Parameters: ~
      • {n}  (`number`) Number of values to skip.

    Return: ~
        (`Iter`)

Iter:slice({first}, {last})                                     *Iter:slice()*
    Sets the start and end of a |list-iterator| pipeline.

    Equivalent to `:skip(first - 1):rskip(len - last + 1)`.

    Parameters: ~
      • {first}  (`number`)
      • {last}   (`number`)

    Return: ~
        (`Iter`)

Iter:take({n})                                                   *Iter:take()*
    Transforms an iterator to yield only the first n values.

    Example: >lua
        local it = vim.iter({ 1, 2, 3, 4 }):take(2)
        it:next()
        -- 1
        it:next()
        -- 2
        it:next()
        -- nil
<

    Parameters: ~
      • {n}  (`integer`)

    Return: ~
        (`Iter`)

Iter:totable()                                                *Iter:totable()*
    Collect the iterator into a table.

    The resulting table depends on the initial source in the iterator
    pipeline. Array-like tables and function iterators will be collected into
    an array-like table. If multiple values are returned from the final stage
    in the iterator pipeline, each value will be included in a table.

    Examples: >lua
        vim.iter(string.gmatch('100 20 50', '%d+')):map(tonumber):totable()
        -- { 100, 20, 50 }

        vim.iter({ 1, 2, 3 }):map(function(v) return v, 2 * v end):totable()
        -- { { 1, 2 }, { 2, 4 }, { 3, 6 } }

        vim.iter({ a = 1, b = 2, c = 3 }):filter(function(k, v) return v % 2 ~= 0 end):totable()
        -- { { 'a', 1 }, { 'c', 3 } }
<

    The generated table is an array-like table with consecutive, numeric
    indices. To

Title: Iter:rfind, Iter:rpeek, Iter:rskip, Iter:skip, Iter:slice, Iter:take, Iter:totable
Summary
This section details the functions available for iterators, including `Iter:rfind`, `Iter:rpeek`, `Iter:rskip`, `Iter:skip`, `Iter:slice`, `Iter:take`, and `Iter:totable`. Each function is described with its parameters, return values, and examples. `Iter:rfind` gets the first value satisfying a predicate, from the end of a list-iterator, `Iter:rpeek` gets the last value of a list-iterator without consuming it, `Iter:rskip` discards n values from the end of a list-iterator pipeline, `Iter:skip` skips n values of an iterator pipeline, `Iter:slice` sets the start and end of a list-iterator pipeline, `Iter:take` transforms an iterator to yield only the first n values, and `Iter:totable` collects the iterator into a table.