Skip to content

Support Enum.with_index/2 with an offset argument #4039

@GeorgeTaveras1231

Description

@GeorgeTaveras1231

I was recently using the Enum.with_index/1 function but did not want to start with an index of 0, then realized that this is not supported.

This is the proposal:

collection = ~w( a b c )
Enum.with_index(collection, 3)
# => [{"a", 3}, {"b", 4}, {"c", 5}]

These are alternative ways to accomplish this:

collection = ~w( a b c )
offset = 3

# Very similar to the current implementation of .with_index/1
collection
|> Enum.map_reduce(offset, fn (current, i) -> {{current, i}, i + 1} end
|> elem(0)
# => [{"a", 3}, {"b", 4}, {"c", 5}]

# Leverage Enum.with_index/1 and Enum.map/2
collection
|> Enum.with_index
|> Enum.map(fn {el, i} -> {el, i + offset} end)
# => [{"a", 3}, {"b", 4}, {"c", 5}]

I believe the proposal would be a more elegant way of accomplishing this.

I'd be happy to implement this. This seems simple enough for a first time contributor to accomplish easily. What do you guys think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions