-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Define the output order from map
#50857
Comments
We could be even more precise, by matching the definition here; in lay terms, applying a function while preserving the structure of the object we're mapping over (as well as respecting composition of mapped functions). The only downside is that we can't use "functor" for that, because we've chosen that to mean a callable struct instead.. |
Well, julia> map(identity, Iterators.take(1:3, 2))
2-element Vector{Int64}:
1
2 Also, as far as I can tell, the functor rules don't actually imply anything about the order of the output, except in the |
this is a bit misleading as it is not always true in general. For this to hold the collection must be non stateful,
And yeah, the order of |
I thought about stateful iterators a while ago, but I finally got around to opening the issue today. So I forgot to add a all(zip(map(f, copy(xs)), xs)) do (fx, x)
isequal(fx, f(x))
end Impure functions seem harder to handle. The following works, but it feels like it works for the wrong reasons: julia> f(x) = push!(x, 10)
f (generic function with 1 method)
julia> xs = [[1, 2], [3, 4]]
2-element Vector{Vector{Int64}}:
[1, 2]
[3, 4]
julia> all(zip(map(f, copy(xs)), xs)) do (fx, x)
a = f(x)
@show a
@show fx
isequal(fx, a)
end
a = [1, 2, 10, 10]
fx = [1, 2, 10, 10]
a = [3, 4, 10, 10]
fx = [3, 4, 10, 10]
true There's probably a counter-example with an impure function where the At any rate, the
I think that comment is referring to the fact that, for a, state1 = iterate(itr1, state1)
b, state2 = iterate(itr2, state2)
return (a, b) or like this: b, state2 = iterate(itr2, state2)
a, state1 = iterate(itr1, state1)
return (a, b) The order of the output tuples, on the other hand, is most definitely intended to correspond to the order of the input tuples, except for weird cases with aliased, stateful iterators (e.g. |
I should reiterate the main message of my original post: Without a guarantee in the docstring on the order of the output, no one should ever use |
I agree with you issue, I just feel it unnecessary to come up with a "code explanation", which has many other implicit assumptions. You've explained it pretty well in one sentence "The order of the output matches the order of the input.". That should be enough? |
Since you're referring to the order being undefined, do you mean that the reduction operator should be commutative? |
Yeah, I guess the operator needs to be both associative and commutative to be able to arbitrarily rearrange the order of the sequence without changing the value of the reduction. But that's not part of the proposed docstring. That was just me complaining about the vagueness of the current |
Related observation: the
This kind of (but not really, perhaps) implies the expected output order of It was added all the way back in 71fc3b3, together with the NEWS entry for |
Currently the
map
docstring makes no assertion about the order of the output collection frommap
. So,map(x -> 2x, 1:2) == [4, 2]
is conformant with themap
docstring. Thus, strictly speaking, no one should ever usemap
unless they are going to perform a reduction (with an associative operator) on the output. Here is a proposal for an updated docstring that defines the output order:Note that I've also switched the terminology from "collection" to "iterable", since
map
works on arbitrary iterable objects which are not necessarily collections. For example,length
throws aMethodError
when called on an iterator likeand the manual implies that
length
must be implemented for an object to be considered a collection.The docstring for
zip
is also a bit vague, so in order to adopt the above change to themap
docstring, we would also need to clarify thezip
docstring.The text was updated successfully, but these errors were encountered: