User Tools

Site Tools


arraysvector

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
arraysvector [2018/12/11 01:53]
julia [Notes]
arraysvector [2018/12/28 11:31] (current)
Line 11: Line 11:
  
 ```juliarepl ```juliarepl
-julia> pkgchk( [ "​julia"​ => v"1.0.2", "​CategoricalArrays"​ => v"​0.4.0"​ ] )+julia> pkgchk.( [ "​julia"​ => v"1.0.3", "​CategoricalArrays"​ => v"​0.4.0"​ ] );
 ``` ```
  
Line 17: Line 17:
 # Vectors (1-Dimensional Arrays) # Vectors (1-Dimensional Arrays)
  
-- `Vector` is an alias for a 1-dimensional array.+- `Vector` is an alias for a 1-dimensional array. ​ (The next chapter ([[arraysmatrix]]) discusses two-dimensional matrices and more linear algebra.)
  
 ```juliarepl ```juliarepl
 julia> Vector{Float64} julia> Vector{Float64}
 Array{Float64,​1} Array{Float64,​1}
 +
 +julia> [1,2,3] ## a 1-dimensional array (of Ints) of length 3
 +3-element Array{Int64,​1}:​
 + 1
 + 2
 + 3
 +
 +julia> [1 2 3] ## but spaces between numbers: a 2-dimensional array (of Ints) with 3 columns and 1 row
 +1×3 Array{Int64,​2}:​
 + ​1 ​ 2  3
 +
 ``` ```
  
-All vectors areby default, (displayed ascolumn vectors.+Vectors offer stackqueueand set operations. ​ `end` is a valid abbreviation for `length()` inside the index operator.
  
-- Vectors come with stack, queue, and set operations. ​ `end` is a valid abbreviation for `length()` inside the index operator. 
  
-- Row vectors are rarely useful. ​ In fact, Julia has both `RowVector` types and matrices with one row and many columns. ​ Matrices are discussed in the [[arraysmatrix]] chapter. 
  
-FIXME (Andreas:) Avoid writing `RowVector` as a type since it's no longer an actual type. Most often, it will be `Adjoint{T,​Vector{T}}`. 
  
-## Creating a Vector from Values+## Plain and Linear Algebra Vectors and Matrices
  
-A vector ​can be created by specifying all elements in a comma-separated list.  (Space separated values create matrices.)  ​Vectors usually convert seamlessly ​and as expected.  ​+Julia can distinguish between ordinary vectors and LinearAlgebra vectors.  ​LinearAlgebra vectors can have a (columnorientation,​ too.  When needed, julia converts ordinary vectors into column vectors ​and vice-versa.  ​A row vector is typically represented as a matrix with one row and many columns.
  
-FIXME add comprehensions from [[controlflow#​Special Loops for Vectors ​and Tuples]].  ​perhaps add for vectors of other itemslike structs+Thus, a transpose on a plain vector considers its input as a column vector, ​and renders it such that it can be used as if it were a row vector (which is really more like a matrix with one row, albeit with a different type name).  ​This "​transposed-vector"​ type is still different from a "​1-row-matrix"​ typebut the "​transposed-vector"​ values can seamlessly be treated as or compared to the "​1-row-matrix"​ values.
  
 +```juliarepl
 +julia> a= [1,2,3] ## a plain (column) vector
 +3-element Array{Int64,​1}:​
 + 1
 + 2
 + 3
  
-### Column Vectors+julia> ta1= transpose( a ) ## transpose is *not fully* a matrix, but a similar *sort-of-row* vector now 
 +1×3 LinearAlgebra.Transpose{Int64,​Array{Int64,​1}}:​ 
 + ​1 ​ 2  3
  
-The default orientation of `Vector` ​is column.+julia> b= [1 2 3] ## this is a matrix, with one row 
 +1×3 Array{Int64,​2}:​ 
 + ​1 ​ 2  3
  
-FIXME (Andreas:) It's more correct to say that outside ​linear algebra, `Vectordoesn'​t ​have a direction.+julia> tb1= transposeb ) ## this is a matrix, with one column 
 +3×1 LinearAlgebra.Transpose{Int64,​Array{Int64,​2}}: 
 + 1 
 + 2 
 + 3 
 + 
 +julia> ( a==b, a==ta1, a==tb1, b==a, b==ta1, b==tb1 ​) ## note the fifth element that is 'true'​ 
 +(false, false, false, false, true, false) 
 + 
 +``` 
 + 
 +- Despite its type `1×3 LinearAlgebra.Transpose{Int64,​Array{Int64,​1}}`,​ `ta1` can be used just like `b` which is a `1×3 Array{Int64,​2}`. ​ This is why `b==ta1`. ​ However `typeof(b) == typeof(ta1)` is false. ​ (The type information is useful, because if you transpose `ta1` [again], you get the plain vector back; but if you transpose `b`, you get the two-dimensional matrix back.) 
 + 
 +- Note that this is not symmetric: although `b` can be the same as the transpose of `a`, `a` can never be the same as a transpose of `b`.  This is because a transpose of `b` remains a matrix with one column, and a plain one-dimensional vector is never the same as a two-dimensional matrix with one column. 
 + 
 +- Not shown: The REPL-printed name of the transpose vector changes when LinearAlgebra is loaded. ​ This is *not* because the type has changed, but because the display changes: after LinearAlgebra is loaded, the prefix is no longer shown. 
 + 
 + 
 + 
 +### Linear Algebra Operations and Element-by-Element Operations 
 + 
 +```juliarepl 
 +julia> [1 2 3] * [2,3,4] ## even without LinearAlgebra.jl loaded, julia knows vector products 
 +1-element Array{Int64,​1}:​ 
 + 20 
 + 
 +julia> [1,2,3] .* [2,3,4] ## with the dot, this is an element-by-element operation 
 +3-element Array{Int64,​1}:​ 
 +  2 
 +  6 
 + 12 
 + 
 +julia> transpose([1,​2,​3]) .* [2,3,4] ## this is quasi-element by element, but really ​linear algebra ​cross-product 
 +3×3 Array{Int64,2}: 
 + ​2 ​ 4   6 
 + ​3 ​ 6   9 
 + ​4 ​ 8  12 
 + 
 +julia> transpose([1,​2,​3]) .* transpose([2,​3,​4]) ##​ but this is again just element by element 
 +1×3 Array{Int64,​2}:​ 
 + ​2 ​ 6  12 
 + 
 +``
 + 
 +- `[1,2,3] * [1,2,3]` is an error. ​ Column vectors ​have no vector product. 
 + 
 + 
 + 
 + 
 + 
 +## Creating ​Vector from Values 
 + 
 +A vector can be created by specifying all elements in a comma-separated list ​(Space separated values create matrices.) ​ Vectors usually convert seamlessly and as expected. ​  
 + 
 +### Plain Vectors 
 + 
 +A plain vector, without orientation,​ looks like a column vector for most purposes:
  
 ```juliarepl ```juliarepl
Line 57: Line 131:
 ``` ```
  
-A column vector is not the same as a matrix with 1 column: 
  
-```juliarepl +### Comprehensions
-julia> cv= fill(0.0, 3) +
-3-element Array{Float64,​1}:​ +
- 0.0 +
- 0.0 +
- 0.0+
  
-julia> ​mcv= fill(0.0, 31           ## 3 by 1 array matrix +Mentioned in [[controlflow#​Special Loops for Vectors and Tuples]], you can also initialize vector with comprehensions:​ 
-3×1 Array{Float64,​2}: + 
- 0.0 +```juliarepl 
- 0.0 +julia> ​(i+2,i/2for i=4:6 ] 
- 0.0+3-element ​Array{Tuple{Int64,​Float64},1}: 
 + (6, 2.0) 
 + (7, 2.5) 
 + (8, 3.0)
  
-julia> typeof(cv) == typeof(mcv) 
-false 
 ``` ```
  
  
  
 +### Row Vectors (Are Matrices)
  
-### Row Vectors +As noted, ​Julia no longer features ​row vectors ​but uses two-dimensional matrices with one row instead. ​ Equivalentlyyou can transpose a (column) vector.  ​To initialize ​quasi-row vector (which ​is really just matrix with one row), you have two choices: create a matrix or a transposed ​vector
- +
-Julia'​s ​row vectors ​are rarely usefulwhich is why they require loading the linear algebra package and are not part of the base Julia.  ​Julia understands that "row within ​1x matrix" ​is different from a row vector.+
  
 ```juliarepl ```juliarepl
-julia> ​[1 2 3]                        ​## space, instead of comma: one row in a two-dimensional 1x3 matrix!!  ​ +julia> ​fill(0.0, ​3, 1)            ​## 3 by 1 array = 2-dim array = matrix 
-1×3 Array{Int64,2}: +3×1 Array{Float64,2}: 
- 1  2  3+ 0.0 
 + 0.0 
 + 0.0
  
-julia> ​using LinearAlgebra+julia> ​fill(0.0,​3)'​ ##​ the ''​ is the transpose() operator 
 +1×3 LinearAlgebra.Adjoint{Float64,​Array{Float64,​1}}:​ 
 + ​0.0 ​ 0.0  0.0
  
-julia> Transpose( [1,2,3] )           ## confirm this is not exactly the same type. 
-1×3 Transpose{Int64,​Array{Int64,​1}}:​ 
- ​1 ​ 2  3 
 ``` ```
- 
-FIXME (Andreas:) Avoid the `Transpose` constructor here. Either use `'` or `transpose`. 
  
  
Line 112: Line 179:
  NaN  NaN
      2.0      2.0
-``` 
-FIXME (Andreas:) Maybe mention that this depends on the literals and you could also do `[1.0f0,​NaN32,​2.0f0]` to get a `Float32` array. Also mention the promotion behavior in the literal constructor. 
  
-### Checking whether a Vector ​is Numeric+julia> typeof([ 1.0f0, NaN32, 2.0f0 ]) ## f0 designates F32, so this is the same 
 +Array{Float32,​1}
  
-```juliarepl +julia> ​typeof([ 1.0f0NaN32, 2.0 ]) ## 2.0 requires F64so it is promoted 
-julia> ​isnumeric(x::​Vector)::​Bool= (eltype(x) <: Union{Missing,​Real});​ +Array{Float64,1}
- +
-julia> for x in ( [1,2], [1,2.0][1,2.0,missing], [1,'​a',​2] ); @info("'​$x'​ is **$(isnumeric(x))**"​);​ end +
-[ Info: '​[1, ​2]' is **true** +
-[ Info: '[1.0, 2.0]' ​is **true** +
-[ Info: 'Union{Missing, ​Float64}[1.02.0, missing]'​ is **true** +
-[ Info: 'Any[1, '​a',​ 2]' is **false**+
  
 ``` ```
Line 149: Line 209:
 julia> using LinearAlgebra julia> using LinearAlgebra
  
-julia> repeat( [ NaN, 1.0 ], 3 )+julia> repeat( [ NaN, 1.0 ], 3 ) ## for more than one value, start with an array and use repeat
 6-element Array{Float64,​1}:​ 6-element Array{Float64,​1}:​
  NaN  NaN
Line 159: Line 219:
 ``` ```
  
-Do not use fill instead of repeat:+Do not use fill instead of repeat, or you will get an array of arrays:
  
 ```juliarepl ```juliarepl
Line 170: Line 230:
  
 These functions also work on higher-dimensional arrays. ​ [[arraysmatrix#​initializing_a_matrix_with_same_values|Matrices]] also often use `fill()`. ​ Type-specialized `zeros(Float64,​5,​5)` (five by five zeros), or `ones(Bool, 4, 4)` (four by four trues), or `rand(3,5)` function, explained in [[random|Random Variables]],​ can also initialize arrays of any dimension. These functions also work on higher-dimensional arrays. ​ [[arraysmatrix#​initializing_a_matrix_with_same_values|Matrices]] also often use `fill()`. ​ Type-specialized `zeros(Float64,​5,​5)` (five by five zeros), or `ones(Bool, 4, 4)` (four by four trues), or `rand(3,5)` function, explained in [[random|Random Variables]],​ can also initialize arrays of any dimension.
 +
 +
 +
 +
 +## Checking whether a Vector is Numeric
 +
 +```juliarepl
 +julia> isnumeric(x::​Vector)::​Bool= (eltype(x) <: Union{Missing,​Real});​
 +
 +julia> for x in ( [1,2], [1,2.0], [1,​2.0,​missing],​ [1,'​a',​2] ); @info("'​$x'​ is **$(isnumeric(x))**"​);​ end
 +[ Info: '[1, 2]' is **true**
 +[ Info: '[1.0, 2.0]' is **true**
 +[ Info: '​Union{Missing,​ Float64}[1.0,​ 2.0, missing]'​ is **true**
 +[ Info: '​Any[1,​ '​a',​ 2]' is **false**
 +
 +```
  
  
Line 190: Line 266:
  
  
-### Push!, Pop!, Shift!, Unshift!+### Appending (Values or Arrays) to an Array
  
-FIXME (Andreas:) Needs to be updated. ​`shift!and `unshift!` are no longer defined. They are now `popfirst!` and `pushfirst!`.+`vcat``append`and `pushall do the same thing.
  
 ```juliarepl ```juliarepl
-julia> x= [1,2,3]; y= append!(x,​4);​ (y === x) ## both y and x have 4 elements now 
-true 
- 
 julia> x= [1,2,3]; julia> x= [1,2,3];
  
-julia> ​pushfirst!(x,0) ## shift right and put 0 into pos 1 (often also called unshift)+julia> ​vcat(x,4                  ​## appends 4, but does not modify x
 4-element Array{Int64,​1}:​ 4-element Array{Int64,​1}:​
- 0 
  1  1
  2  2
  3  3
 + 4
  
-julia> ​println( shift!(x) , " ​  ",​ x) ## drop 0 from pos 1 and shift left +julia> x ## the original vector x is not modified 
-ERROR: UndefVarError:​ shift! ​not defined +3-element Array{Int64,​1}: 
-Stacktrace:+ 1 
 + 2 
 + 3 
 +```
  
-julia> push!(x,​4) ​              ## add at the end, like append +They also exist with an ending '​!'​ to modify the array in place. 
-5-element Array{Int64,​1}:​ + 
- 0+``` 
 + 
 +julia> push!(x,4); x               ## push!() is really vcat!(); appends ​*and* modifies x 
 +4-element Array{Int64,​1}:​
  1  1
  2  2
Line 219: Line 298:
  4  4
  
-julia> println( pop!(x) , " ​  ",​ x) ## drop 4 from the end 
-4   [0, 1, 2, 3] 
 ``` ```
  
-### Appending (Values or Arrays) to an Array+There is a convenient short-hand "​bracket"​ notation for vcat:
  
-`vcat`, `append`, and `pushall do the same thing.+```juliarepl 
 +julia> [ [1,2,3] ; [ 4,5,6 ] ] 
 +6-element Array{Int64,​1}:​ 
 + 1 
 + 2 
 + 3 
 + 4 
 + 5 
 + 6 
 + 
 +``
 + 
 + 
 +### Push!, Pop!, PopFirst!, PushFirst!
  
 ```juliarepl ```juliarepl
 julia> x= [1,2,3]; julia> x= [1,2,3];
  
-julia> ​vcat(x,4)                   ​## appends 4, but does not modify x+julia> ​append!(x,4) ## append = push.  note '​!'​ for changing contents of array
 4-element Array{Int64,​1}:​ 4-element Array{Int64,​1}:​
  1  1
Line 237: Line 327:
  4  4
  
-julia> x +julia> ​pushfirst!(x,0) ## shift right and place 0 into pos 1 (often also called unshift) 
-3-element Array{Int64,​1}:​+5-element Array{Int64,​1}:​ 
 + 0
  1  1
  2  2
  3  3
 + 4
 + 
 +julia> ( popfirst!(x),​ " -- ", x ) ## drop 0 from pos 1 and shift left
 +(0, " -- ", [1, 2, 3, 4])
 +
 +julia> ( pop!(x), " -- ", x )                   ## drop 4 from last pos
 +(4, " -- ", [1, 2, 3])
  
-julia> push!(x,4); x               ## push!() is really vcat!(); appends 4 and modifies x 
-4-element Array{Int64,​1}:​ 
- 1 
- 2 
- 3 
- 4 
 ``` ```
 +
 +- In other languages, shift and unshift play the same role as popfirst and pushfirst
  
  
Line 264: Line 358:
  ​["​c",​ "​d"​]  ​["​c",​ "​d"​]
  
-julia> vcat( a1, a2 )                           ## flattened+julia> vcat( a1, a2 )                           ## flattened.  or use shortcut [ a1 ; a2 ]
 4-element Array{String,​1}:​ 4-element Array{String,​1}:​
  "​a"​  "​a"​
Line 328: Line 422:
 ## Arithmetic Operations With Flattening ## Arithmetic Operations With Flattening
  
-FIXME (Andreas:) It's unclear what is going on here so it would be good with more explanation. Have you introduced `@.` at all?+The '@.' expression ​is syntactic sugar that inserts dots whereever ​it is needed in function calls.
  
 ```juliarepl ```juliarepl
-julia> ​@. [ 10 + [-1,+1]*2; 20 + [-3,+5]^1 ]+julia> [ 10 .+ [-1,+1].*2; 20 .+ [-3,+5].^1 ] ## accurate
 4-element Array{Int64,​1}:​ 4-element Array{Int64,​1}:​
   8   8
Line 338: Line 432:
  25  25
  
-julia> vcat(@. (10 + (-i:i)*2 for i=1:2)...)+julia> @. [ 10 + [-1,+1]*2; 20 + [-3,+5]^1 ] ## lazy way of doing this 
 +4-element Array{Int64,​1}:​ 
 +  8 
 + 12 
 + 17 
 + 25 
 + 
 +julia> vcat(@. (10 + (-i:i)*2 for i=1:2)...) ## a more useful example: changes iterator into range
 8-element Array{Int64,​1}:​ 8-element Array{Int64,​1}:​
   8   8
Line 349: Line 450:
  14  14
 ``` ```
 +
  
  
 ## Leading or Lagging Arrays ## Leading or Lagging Arrays
  
-* A `lag()` function unshifts ​NaN or Missing onto a vector and pops off the vector'​s last element.+It is often useful to lead or lag an array. ​ For example, you may want to see whether values in a timeseries like `[ 1, 2, 4, 7, 11 ]` can be explained by their past value. 
 + 
 +```juliarepl 
 +julia> ​lag(x::​Vector{Float64},​ num=1)::​Vector{Float64}= vcat( fill(NaN,num), [ x[i] for i=1:​length(x)-num] ); 
 + 
 +julia> x= [ 1.0,2,4,7,11 ]; [ x lag(x) ] 
 +5×2 Array{Float64,​2}:​ 
 +  1.0  NaN 
 +  2.0    1.0 
 +  4.0    2.0 
 +  7.0    4.0 
 + ​11.0 ​   7.0 
 + 
 +```
  
-FIXME (Andreas:) This is not function that exists which isn't clear from the sentence.+You could now run regression explaining ​the first column with the second.
  
-* The [[functions#​adding_missing_unions_to_array_arguments|functions]] chapter defines a good `lag()` vector function that also handles missing well.+* The [[fundispatch#​adding_missing_unions_to_array_arguments|function dispatch]] chapter defines a better ​`lag()` vector function that works for more types.
  
 * [ShiftedArrays.jl](https://​github.com/​piever/​ShiftedArrays.jl) offers similar functionality (and even some event-study capability to shift multiple vectors for alignment). ​ It is a little more efficient (working with views rather than copies, but it always uses [[missings|Missing]] instead of NaN.  Missing works for all types, but are much slower). * [ShiftedArrays.jl](https://​github.com/​piever/​ShiftedArrays.jl) offers similar functionality (and even some event-study capability to shift multiple vectors for alignment). ​ It is a little more efficient (working with views rather than copies, but it always uses [[missings|Missing]] instead of NaN.  Missing works for all types, but are much slower).
Line 393: Line 508:
 B B
 C C
 +
 +julia> [ item for item in ["​a",​ "​b",​ "​c"​ ] ] ## a "​comprehension"​ is often convenient, creating a new array
 +3-element Array{String,​1}:​
 + "​a"​
 + "​b"​
 + "​c"​
 ``` ```
  
Line 410: Line 531:
 8 => 4 8 => 4
 9 => 1 9 => 1
 +
 ``` ```
  
-FIXME (Andreas:) Maybe use `Iterators.Cycle` and `Iterators.take` for this and move the example to a section about the `Iterators` module.+- Julia offers a set of common iteration tools in [[https://github.com/​JuliaCollections/​IterTools.jl|IterTools]]. 
  
 ### Iterating over Array Index and Contents ### Iterating over Array Index and Contents
Line 446: Line 569:
 ## Applying the same Function to each Element (Map) ## Applying the same Function to each Element (Map)
  
-Most of the time, you can just use a '[[functions#​dot_postfix_functions|postfix-dot]]'​ function equivalent call (e.g., `sqrt.([1.0,​2.0])`) to  apply a function to each element of a vector. ​ Sometimes, the `map()` function is more convenient:+Most of the time, you can just use a '[[funother#​dot_postfix_functions|postfix-dot]]'​ function equivalent call (e.g., `sqrt.([1.0,​2.0])`) to  apply a function to each element of a vector. ​ Sometimes, the `map()` function is more convenient:
  
 ```juliarepl ```juliarepl
Line 462: Line 585:
 ## Element-Wise Comparisons (Dot Operators) ## Element-Wise Comparisons (Dot Operators)
  
-Use `.==` (and equivalent [[functions#​dot_postfix_functionsdot operators]]):​+Use `.==` (and equivalent, see [[functions|funother#​dot_postfix_functionsdot operators]] ​chapter):
  
 ```juliarepl ```juliarepl
Line 527: Line 650:
 ## Recoding all Values in an Array ## Recoding all Values in an Array
  
 +```juliarepl
 julia> using Missings julia> using Missings
- 
-```juliarepl 
  
 julia> v= [1.0, NaN, missing, 4.0]; replace( v, NaN => missing )       ## special case: could use Missings.replace(v,​ NaN) julia> v= [1.0, NaN, missing, 4.0]; replace( v, NaN => missing )       ## special case: could use Missings.replace(v,​ NaN)
Line 540: Line 662:
 ``` ```
  
-* **M**issing is a type, **m**issing is a value.+**Important**: ​**M**issing is a type, **m**issing is a value.
  
  
  
-## R-Like ​List Apply (lapply) and Parallel List Apply (mclapply)+## R-Like ​By
  
-FIXME (Andreas:) I don't think it'​s ​good idea to invite people ​to keep their R habits in Julia. It'​s ​better to get used to expressing these things with native ​Julia constructs.+It is not great idea to keep R habits in Julia. ​ It is better to learn to think the Julia way ​However,​ when needed, the following function will work like `by` in R, applying a function to all elements split by a (categorical) vector:
  
 ```juliarepl ```juliarepl
 julia> using CategoricalArrays julia> using CategoricalArrays
  
-julia> function ​lapply(obj::​AbstractVector,​ ind::​AbstractVector,​ func::​Function,​ x...)+julia> function ​Rby(obj::​AbstractVector,​ ind::​AbstractVector,​ func::​Function,​ x...)
            map( elem->​(elem,​ func(obj[findall(elem .== ind)], x...)), levels(ind))            map( elem->​(elem,​ func(obj[findall(elem .== ind)], x...)), levels(ind))
        ​end;#​function##​        ​end;#​function##​
Line 559: Line 681:
 julia> Random.seed!(0);​ julia> Random.seed!(0);​
  
-julia> randcateg4 = rand('​a':'​d',​ 100); ## sample testset+julia> randcateg4 = rand('​a':'​d',​ 100); ## sample testset: 4 categories
  
 julia> hundredsquares= [1:100;].^2 ; julia> hundredsquares= [1:100;].^2 ;
Line 565: Line 687:
 julia> using Statistics julia> using Statistics
  
-julia> ​lapply( hundredsquares,​ randcateg4, mean ) ## for each element, by randcateg4, calculate mean+julia> ​Rby( hundredsquares,​ randcateg4, mean ) ## for each element, by randcateg4, calculate mean
 4-element Array{Tuple{Char,​Float64},​1}:​ 4-element Array{Tuple{Char,​Float64},​1}:​
  ​('​a',​ 3575.25)  ​('​a',​ 3575.25)
Line 572: Line 694:
  ​('​d',​ 2854.310344827586)  ​('​d',​ 2854.310344827586)
  
-julia> ​lapply( hundredsquares,​ randcateg4, quantile, [0.25, 0.50, 0.75] )+julia> ​Rby( hundredsquares,​ randcateg4, quantile, [0.25, 0.50, 0.75] ) ## or calculate the three quantiles
 4-element Array{Tuple{Char,​Array{Float64,​1}},​1}:​ 4-element Array{Tuple{Char,​Array{Float64,​1}},​1}:​
  ​('​a',​ [885.25, 2234.0, 6321.0])  ​('​a',​ [885.25, 2234.0, 6321.0])
Line 584: Line 706:
  
  
-## SUGGESTIONAVOID Any (and UnitializedArrays+## SuggestionAvoid `Anyand Unitialized Arrays
  
 An array of type `Any` can support elements of all types: An array of type `Any` can support elements of all types:
Line 608: Line 730:
 ``` ```
  
-FIXME (Andreas:) It's still not clear why users should avoid these. Better to explain and provide examples ​of what could go wrong.+Using such constructs can not only rob you of compile-time type checking, but also of memory and speed if ever accidentally used the wrong way.  After all, your computer is good at operating on its native types, and if this is all you need, then restrict yourself to it.
  
  
Line 623: Line 745:
 UnitRange{Int64} UnitRange{Int64}
  
-julia> collect(1:​3)+julia> collect(1:​3) ## convert iterator (range) now into an array of ints
 3-element Array{Int64,​1}:​ 3-element Array{Int64,​1}:​
 1 1
Line 693: Line 815:
 It is a pity that julia does not force type declarations of all variables, and especially of its `Any` type. It is a pity that julia does not force type declarations of all variables, and especially of its `Any` type.
  
-When an array display would exceed the terminal display, Julia fits it with dot indicators, omitting middle elements.  ​For  +When an array display would exceed the terminal display, Julia fits it with dot indicators, omitting middle elements.  ​If you really want to see a long displayyou can use `show( [1:10000;] )`.
-brutalists, `show( [1:10000;] )` would print all 10,000 numbers, even when the terminal can hold only 50 rows!+
  
-FIXME (Andreas:) The last sentence is a bit confusing. What is going on? As it is written now, it mainly confuses. 
  
 ## References ## References
arraysvector.1544521984.txt.gz · Last modified: 2018/12/11 01:53 by julia