Home Explore Blog CI



neovim

56th chunk of `runtime/doc/lua.txt`
3b88efc77dc252ee875a52c25cc45b80b5ef226bd703fb240000000100000fb1
   -- 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({ 3, 6, 9, 12, 15 })
        it:last()
        -- 15
<

    Return: ~
        (`any`)

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

Iter:map({f})                                                     *Iter:map()*
    Maps the items of an iterator pipeline to the values returned by `f`.

    If the map function returns nil, the value is filtered from the iterator.

    Example: >lua
        local it = vim.iter({ 1, 2, 3, 4 }):map(function(v)
          if v % 2 == 0 then
            return v * 3
          end
        end)
        it:totable()
        -- { 6, 12 }
<

    Parameters: ~
      • {f}  (`fun(...):...:any`) Mapping function. Takes all values returned
             from the previous stage in the pipeline as arguments and returns
             one or more new values, which are used in the next pipeline
             stage. Nil return values are filtered from the output.

    Return: ~
        (`Iter`)

Iter:next()                                                      *Iter:next()*
    Gets the next value from the iterator.

    Example: >lua

        local it = vim.iter(string.gmatch('1 2 3', '%d+')):map(tonumber)
        it:next()
        -- 1
        it:next()
        -- 2
        it:next()
        -- 3
<

    Return: ~
        (`any`)

Iter:nth({n})                                                     *Iter:nth()*
    Gets the nth value of an iterator (and advances to it).

    If `n` is negative, offsets from the end of a |list-iterator|.

    Example: >lua
        local it = vim.iter({ 3, 6, 9, 12 })
        it:nth(2)
        -- 6
        it:nth(2)
        -- 12

        local it2 = vim.iter({ 3, 6, 9, 12 })
        it2:nth(-2)
        -- 9
        it2:nth(-2)
        -- 3
<

    Parameters: ~
      • {n}  (`number`) Index of the value to return. May be negative if the
             source is a |list-iterator|.

    Return: ~
        (`any`)

Iter:peek()                                                      *Iter:peek()*
    Gets the next value in a |list-iterator| without 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.

Title: Iter:join, Iter:last, Iter:map, Iter:next, Iter:nth, Iter:peek, Iter:pop, Iter:rev
Summary
This section details the functions available for iterators, including `Iter:join`, `Iter:last`, `Iter:map`, `Iter:next`, `Iter:nth`, `Iter:peek`, `Iter:pop`, and `Iter:rev`. Each function is described with its parameters, return values, and examples. `Iter:join` collects the iterator into a delimited string, `Iter:last` drains the iterator and returns the last item, `Iter:map` maps the items of an iterator pipeline to the values returned by a function, `Iter:next` gets the next value from the iterator, `Iter:nth` gets the nth value of an iterator, `Iter:peek` gets the next value in a list-iterator without consuming it, `Iter:pop` pops a value from a list-iterator, and `Iter:rev` reverses a list-iterator pipeline.