Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Enum.with_index/2 #4040

Merged

Conversation

GeorgeTaveras1231
Copy link
Contributor

Proposal at #4039

TODO:

  • Implement Stream.with_index/2

@josevalim
Copy link
Member

Thank you @GeorgeTaveras1231! I have added a comment, we also need tests and we are good to go.

map_reduce(enumerable, 0, fn x, acc ->
@spec with_index(t) :: [{element, integer}]
def with_index(enumerable), do: with_index(enumerable, 0)
def with_index(enumerable, offset) do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd better to use defaults: offset \\ 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lexmag I was considering doing this but not sure what the benefit is, would you mind explaining your POV?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GeorgeTaveras1231 technically it is completely the same what you've done, but obviously less code and that's the common practice throughout codebase (I believe it is true for every Elixir project), thus there is no reason not to use it. :bowtie:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit is what it would be considered one function, which is useful in the documentation. For example, in your current patch, you are only documenting with_index/1, with_index/2 would show with no documentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise with current patch extra body-less function needs to be defined. Even this won't combine them for docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thank you guys :)

@josevalim
Copy link
Member

We also need to change Stream.with_index. :)

@GeorgeTaveras1231
Copy link
Contributor Author

Thank you for the feedback @josevalim @lexmag! I will address the concerns some time today.

@spec with_index(t) :: [{element, non_neg_integer}]
def with_index(enumerable) do
map_reduce(enumerable, 0, fn x, acc ->
@spec with_index(t) :: [{element, integer}]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, forgot to update the signature in the spec

This removes the previous implementation `Enum.with_index/1` but
maintains backwards compatibility by providing a default offset of 0

Proposal at elixir-lang#4039
This removes the previous implementation `Stream.with_index/1` but
maintains backwards compatibility by providing a default
offset of 0
@GeorgeTaveras1231
Copy link
Contributor Author

@josevalim @lexmag what do you think?

josevalim added a commit that referenced this pull request Dec 3, 2015
@josevalim josevalim merged commit 609e042 into elixir-lang:master Dec 3, 2015
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@GeorgeTaveras1231 GeorgeTaveras1231 deleted the feature/Enum.with_index/2 branch November 5, 2017 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants