Skip to content

Document map behavior on sparse data structures #44233

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
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
5 changes: 5 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2940,6 +2940,11 @@ mapany(f, itr) = Any[f(x) for x in itr]
Transform collection `c` by applying `f` to each element. For multiple collection arguments,
apply `f` elementwise, and stop when when any of them is exhausted.

Note that `f` makes no guarantees about the order in which `f` is called on elements. In
addition, `f` is assumed to be [pure](https://en.wikipedia.org/wiki/Pure_function). Using
an impure function together with `f` can cause bugs when working with some data structures,
e.g. sparse or diagonal arrays, as `f` will only be called once on duplicated elements.
Copy link
Member

Choose a reason for hiding this comment

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

it will be called once on default 0 but if you have duplicates that are non-default zeros it will be called multiple times.

Copy link
Author

Choose a reason for hiding this comment

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

it will be called once on default 0 but if you have duplicates that are non-default zeros it will be called multiple times.

I've edited to say that it "May" call it only once per duplicate element, and clarify that calling map with an impure function is undefined behavior.


See also [`map!`](@ref), [`foreach`](@ref), [`mapreduce`](@ref), [`mapslices`](@ref), [`zip`](@ref), [`Iterators.map`](@ref).

# Examples
Expand Down