Skip to content

Commit 4a13e9e

Browse files
pierre-haessigfingolfinjishnub
authored
Clarify the difference between collect and bracket notation for generators (#50619)
As a follow-up to my question on the forum [Unclear documentation of `collect`? (similarity and difference with brackets)](https://discourse.julialang.org/t/unclear-documentation-of-collect-similarity-and-difference-with-brackets/), here is a tentative expansion of the docstring of `Base.collect` to clarify both the similarities and differences with the bracket notation for generators. I'm pretty sure it needs so edits, but here are the motivating factors behind the changes, by decreasing order of importance: 1. avoid the presence of bracket notation in the example without explanation (in the present docstring, I find it highly *implicit*) 2. simplification of examples to focus on the topic (and not on range syntax or filtering in comprehensions) 3. avoid wasting vertical space by using longer than needed iterators/generators Feedback on my formulation is welcome! --------- Co-authored-by: Max Horn <[email protected]> Co-authored-by: Jishnu Bhattacharya <[email protected]>
1 parent aaed1cc commit 4a13e9e

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

base/array.jl

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -672,30 +672,34 @@ _array_for(::Type{T}, itr, isz) where {T} = _array_for(T, isz, _similar_shape(it
672672
collect(collection)
673673
674674
Return an `Array` of all items in a collection or iterator. For dictionaries, returns
675-
`Vector{Pair{KeyType, ValType}}`. If the argument is array-like or is an iterator with the
676-
[`HasShape`](@ref IteratorSize) trait, the result will have the same shape
675+
a `Vector` of `key=>value` [Pair](@ref Pair)s. If the argument is array-like or is an iterator
676+
with the [`HasShape`](@ref IteratorSize) trait, the result will have the same shape
677677
and number of dimensions as the argument.
678678
679-
Used by comprehensions to turn a generator into an `Array`.
679+
Used by [comprehensions](@ref man-comprehensions) to turn a [generator expression](@ref man-generators)
680+
into an `Array`. Thus, *on generators*, the square-brackets notation may be used instead of calling `collect`,
681+
see second example.
680682
681683
# Examples
684+
685+
Collect items from a `UnitRange{Int64}` collection:
686+
682687
```jldoctest
683-
julia> collect(1:2:13)
684-
7-element Vector{Int64}:
685-
1
686-
3
687-
5
688-
7
689-
9
690-
11
691-
13
688+
julia> collect(1:3)
689+
3-element Vector{Int64}:
690+
1
691+
2
692+
3
693+
```
692694
693-
julia> [x^2 for x in 1:8 if isodd(x)]
694-
4-element Vector{Int64}:
695-
1
696-
9
697-
25
698-
49
695+
Collect items from a generator (same output as `[x^2 for x in 1:3]`):
696+
697+
```jldoctest
698+
julia> collect(x^2 for x in 1:3)
699+
3-element Vector{Int64}:
700+
1
701+
4
702+
9
699703
```
700704
"""
701705
collect(itr) = _collect(1:1 #= Array =#, itr, IteratorEltype(itr), IteratorSize(itr))

doc/src/manual/arrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ the result in single precision by writing:
398398
Float32[ 0.25*x[i-1] + 0.5*x[i] + 0.25*x[i+1] for i=2:length(x)-1 ]
399399
```
400400

401-
## Generator Expressions
401+
## [Generator Expressions](@id man-generators)
402402

403403
Comprehensions can also be written without the enclosing square brackets, producing an object
404404
known as a generator. This object can be iterated to produce values on demand, instead of allocating

0 commit comments

Comments
 (0)