-
-
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
Add Iterators.map #34352
Add Iterators.map #34352
Conversation
NEWS.md
Outdated
@@ -19,7 +19,7 @@ Build system changes | |||
|
|||
New library functions | |||
--------------------- | |||
|
|||
* `Iterators.map` is added ([#34352]). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to indicate what it does.
base/iterators.jl
Outdated
@@ -22,7 +22,27 @@ import .Base: | |||
getindex, setindex!, get, iterate, | |||
popfirst!, isdone, peek | |||
|
|||
export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten, partition | |||
export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten, partition, map |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not be exported, since it will conflict with Base.map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little sad that we didn't stick with the convention that the capitalized type name is the lazy construct while the lowercase function name is the eager construct. IIRC, we had agreed on that around 0.2 era but then didn't end up sticking to it.
I addressed the points in the review. Now |
Can we have generators lower to |
That sounds like a great idea. Same holds for |
Sure #34368 |
Co-Authored-By: Stefan Karpinski <stefan@karpinski.org>
Seems to have trailing whitespace. |
buildbot/whitespace_linux32 (https://build.julialang.org/#/builders/150/builds/4226) seems to fail due to network failure (?):
|
This has been in IterTools.jl forever as Julia since the 0.7 release (and even before) has pushed towards functionality should live in packages, not that standard library. In particular, I also have this complaint about #34033 but I didn't see it before it was merged. |
#34033 doesn't seem to imply this. |
As I said, I would have called #34033 out if I had have seen it before it was merged. |
I support not adding unnecessary stuff to Base. This issue isn't about adding a new iterator, just an interface to one that already exists (namely, Base.Generator). |
Yes, that's a good point. It'd be nice to have a public and functional interface for constructing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I support this change, since it essentially just adds an alias that is very intuitive.
@testset "Iterators.map" begin | ||
@test collect(Iterators.map(string, 1:3)::Base.Generator) == map(string, 1:3) | ||
@test collect(Iterators.map(tuple, 1:3, 4:6)::Base.Generator) == map(tuple, 1:3, 4:6) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be good to test that Iterators.map
can be applied to infinite iterables such as Iterators.cycle(1:3)
and has the expected behaviour for the first n iterates.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason why testing infinite iterators is interesting here? I'm testing these two invocations since Base.Generator
has different methods for them. So, I think it's somewhat meaningful to make sure we can hit these methods. Other than that, I don't think we need to add tests specially for Iterators.map
and they can be done in Base.Generator
. I think it makes sense to rename direct uses of Base.Generator
in Base/stdlib. But I think it'd be better to do this in a separate PR to make this PR more "atomic".
|
||
!!! compat "Julia 1.5" | ||
This function requires at least Julia 1.5. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe cross-reference with the docstring of Base.Generator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think Base.Generator
is included in the documentation. This is partially why I want to add Iterators.map
.
What is the reason why testing infinite iterators is interesting here?
It tests the laziness of `Iterators.map` since non-lazy `map` can't handle
infinite iterators.
I don't think we need to add tests specially for `Iterators.map` and they can
be done in `Base.Generator`. I think it makes sense to rename direct uses of
`Base.Generator` in Base/stdlib. But I think it'd be better to do this
in a separate PR to make this PR more "atomic".
I agree.
|
I don't think `Base.Generator` is included in the documentation. This is
partially why I want to add `Iterators.map`.
It has a docstring on Julia 1.4.1 at least.
|
Existing tests already check that
Having a docstring does not mean it's a public API. See #35715 |
Having a docstring does not mean it's a public API. See #35715
Thank you for clarifying, but even internal documentation can benefit from
cross-referencing. Mentioning the alias `Iterators.map` in the docstring of
`Base.Generator` does not seem like any harm to me.
|
There is a discussion on the extension of |
I resolved the merge conflict and CI is all green now. Can someone merge this? (ping @StefanKarpinski @JeffBezanson) |
(ping @StefanKarpinski @JeffBezanson) |
This is exciting! I'd note that the NEWS entry seems to imply that |
Hmm... or maybe I'm just reading that sentence wrong? |
My intention was that both |
As @JeffBezanson mentioned in #34033 (comment), it makes sense to have
Iterators.map
. This PR adds it (with some tests).