@@ -115,9 +115,12 @@ Now, when we ask Julia to [`collect`](@ref) all the elements into an array it ca
115
115
of the right size instead of blindly [ ` push! ` ] ( @ref ) ing each element into a ` Vector{Any} ` :
116
116
117
117
``` jldoctest squaretype
118
- julia> collect(Squares(10))' # transposed to save space
119
- 1×10 RowVector{Int64,Array{Int64,1}}:
120
- 1 4 9 16 25 36 49 64 81 100
118
+ julia> collect(Squares(4))
119
+ 4-element Array{Int64,1}:
120
+ 1
121
+ 4
122
+ 9
123
+ 16
121
124
```
122
125
123
126
While we can rely upon generic implementations, we can also extend specific methods where we know
@@ -150,9 +153,12 @@ julia> Base.next(::Iterators.Reverse{Squares}, state) = (state*state, state-1)
150
153
151
154
julia> Base.done(::Iterators.Reverse{Squares}, state) = state < 1
152
155
153
- julia> collect(Iterators.reverse(Squares(10)))' # transposed to save space
154
- 1×10 RowVector{Int64,Array{Int64,1}}:
155
- 100 81 64 49 36 25 16 9 4 1
156
+ julia> collect(Iterators.reverse(Squares(4)))
157
+ 4-element Array{Int64,1}:
158
+ 16
159
+ 9
160
+ 4
161
+ 1
156
162
```
157
163
158
164
## Indexing
@@ -275,28 +281,31 @@ methods are all it takes for `SquaresVector` to be an iterable, indexable, and c
275
281
array:
276
282
277
283
``` jldoctest squarevectype
278
- julia> s = SquaresVector(7 )
279
- 7 -element SquaresVector:
284
+ julia> s = SquaresVector(4 )
285
+ 4 -element SquaresVector:
280
286
1
281
287
4
282
288
9
283
289
16
284
- 25
285
- 36
286
- 49
287
290
288
- julia> s[s .> 20]
289
- 3-element Array{Int64,1}:
290
- 25
291
- 36
292
- 49
291
+ julia> s[s .> 8]
292
+ 2-element Array{Int64,1}:
293
+ 9
294
+ 16
293
295
294
- julia> s \ [1 2; 3 4; 5 6; 7 8; 9 10; 11 12; 13 14]
295
- 1×2 RowVector{Float64,Array{Float64,1}}:
296
- 0.305389 0.335329
296
+ julia> s + s
297
+ 4-element Array{Int64,1}:
298
+ 2
299
+ 8
300
+ 18
301
+ 32
297
302
298
- julia> s ⋅ s # dot(s, s)
299
- 4676
303
+ julia> sin.(s)
304
+ 4-element Array{Float64,1}:
305
+ 0.8414709848078965
306
+ -0.7568024953079282
307
+ 0.4121184852417566
308
+ -0.2879033166650653
300
309
```
301
310
302
311
As a more complicated example, let's define our own toy N-dimensional sparse-like array type built
@@ -382,8 +391,8 @@ julia> A[SquaresVector(3)]
382
391
4.0
383
392
9.0
384
393
385
- julia> dot(A[:,1],A[:,2] )
386
- 32 .0
394
+ julia> mean(A )
395
+ 5 .0
387
396
```
388
397
389
398
If you are defining an array type that allows non-traditional indexing (indices that start at
0 commit comments