Skip to content

Instantly share code, notes, and snippets.

@andyferris
Last active November 18, 2017 12:37
Show Gist options
  • Save andyferris/7a5156af695e5c0c94458004467e936c to your computer and use it in GitHub Desktop.
Save andyferris/7a5156af695e5c0c94458004467e936c to your computer and use it in GitHub Desktop.
Comparing potential array constructors
# size versions
Array{T}(2, 3)
Array{T}(blah, 2, 3)
Array{T}((2,3))
Array{T}(blah, (2,3))
Array{T}(size = (2,3))
Array{T}(blah, size = (2,3))
# indices versions
Array{T}(OneTo(2), OneTo(3)) # is OneTo(2) a `blah` or the first index?
Array{T}((OneTo(2), OneTo(3)))
Array{T}(blah, OneTo(2), OneTo(3))
Array{T}(blah, (OneTo(2), OneTo(3)))
Array{T}(indices = (OneTo(2), OneTo(3)))
Array{T}(blah, indices = (OneTo(2), OneTo(3)))
# keys versions
Array{T}(CartesianRange(2,3))
Array{T}(blah, CartesianRange(2,3))
Array{T}(indices = CartesianRange(2,3))
Array{T}(blah, keys = CartesianRange(2,3))
# Possible versions for Dicts
Dict{K,T}(keys)
Dict{K,T}(blah, keys)
Dict{K,T}(keys = keys)
Dict{K,T}(blah, keys = keys)
# For example. these three should be all distinct:
Dict{Any,T}(blah, (2,3)) # the keys are 2 and 3
Dict{Any,T}(blah, (OneTo(2), OneTo(3)) # the keys are OneTo(2) and OneTo(3)
Dict{Any,T}(blah, CartesianRange(2,3)) # the keys are CartesianIndex(1,1) ... CartesianIndex(2,3)
#
# Food for thought
#
Vector{Int}(length = 10) # length `n` for vectors is shorthand for size `(n,)`
Vector(Fill(0), length = 10)
Vector{Int}(keys = Base.OneTo(10)) # ambiguous without keyword argument
Matrix{Float64}(size = (10, 10))
Array(Rand(1:10), keys = keys(other_array))
SVector(Fill(0), keys = SUnitRange(1, 10)) # SUnitRange doesn't exist in StaticArrays yet but we want to make it
SArray(Fill(0), keys = keys(other_static_array)) # more likely to be used than above
Dict(Fill(0), keys = [:a, :b, :c])
Dict{Symbol, Symbol}(keys = [:a, :b, :c]) # ambiguous without the keyword argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment