Skip to content

fix example comment in Enum.slide/3 #13779

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

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2706,11 +2706,11 @@ defmodule Enum do
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 5, 1)
[:a, :f, :b, :c, :d, :e, :g]

# Slide a range of elements backward
Copy link
Member

Choose a reason for hiding this comment

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

Both interpretations can be right, but semantically, it is forward, because we are bringing the elements from 3..5 to position 1 (and as a result, the ones from 1..2 are brought forward, but the ones we specify go back).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the ones we specify go back

Could you expand on that a little? Which elements are "the ones we specify" and what do you mean by "go back"?

I'd interpret the elements we specify as the 3..5 slice, i.e. elements [:d, :e, :f]. Originally, these elements are in positions 3-5, but after the slide/3 call, they are in positions 1-3. As these elements have moved towards the head of the list, wouldn't this be "going forward"?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see where the confusion is coming from, it is about what is forward, the head or the tail? What if instead of forwards and backwards and we say "towards the head" and "towards the tail"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was indeed the source of my confusion: "forward" being "in the reading direction" vs "toward the list head". I've reworded the change to remove ambiguity. Thanks for clarifying your meaning!

# Slide a range of elements forward
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 3..5, 1)
[:a, :d, :e, :f, :b, :c, :g]

# Slide a range of elements forward
# Slide a range of elements backward
iex> Enum.slide([:a, :b, :c, :d, :e, :f, :g], 1..3, 5)
[:a, :e, :f, :b, :c, :d, :g]

Expand Down
Loading