Skip to content

7ML7W Julia Day 1: Resistance Is Futile

Charlie Egan edited this page Jul 6, 2018 · 2 revisions

Julia Day 1

July 3rd, 2018 - Unboxed.

Strings are now just strings, ASCIIString no longer seems to be the default inferred type for stringy things.

julia> typeof("julia")
String

We also soon learned that the following { } vector syntax was 'discontinued':

julia> typeof({ :key => "value" })
ERROR: syntax: { } vector syntax is discontinued

The new way to do this seems to be:

julia> typeof(Dict(:key => "value"))
Dict{Symbol,String}

This is seems to infer the types where the deprecated syntax would have given Dict{Any,Any} for the same input. It seems to get this now you need to create an empty Dict first:

julia> typeof(Dict())
Dict{Any,Any}

The 'explicit' [:key => "value"] syntax now also seems to construct arrays now - not Dicts:

julia> typeof([:key => "value"])
Array{Pair{Symbol,String},1}

We pondered a little about the use case for this reverse division operator:

julia> 10 \ 2
0.2

julia> 10 / 2
5.0

We also spent some time thinking about the type coercion:

julia> [1, 1.2, 3]
3-element Array{Float64,1}:
 1.0
 1.2
 3.0

julia> push!(Int[1,2,3], 1.2)
ERROR: InexactError()
Stacktrace:
 [1] convert(::Type{Int64}, ::Float64) at ./float.jl:679
 [2] push!(::Array{Int64,1}, ::Float64) at ./array.jl:646

We also discovered that the one top of the type hierarchy seemed to be Datatype:

julia> typeof(Float64)
DataType

julia> typeof(Int64)
DataType

julia> typeof(DataType)
DataType

julia> typeof(typeof)
Core.#typeof

julia> typeof(+)
Base.#+

We didn't get round to looking into what the difference between Any and DataType was.

julia> typeof(Any)
DataType

Next came arrays, something that's not quite as you know it in Julia.

Indexing is from 1 and there's :end to get the last position:

julia> [1,2,3,4][2:end]
3-element Array{Int64,1}:
 2
 3
 4

You can assign to ranges in arrays but only if they're the same length:

julia> [1,2,3,4][3:end] = [1,2]
2-element Array{Int64,1}:
 1
 2

julia> [1,2,3,4][3:end] = [1,2,3]
ERROR: DimensionMismatch("tried to assign 3 elements to 2 destinations")
Stacktrace:
 [1] throw_setindex_mismatch(::Array{Int64,1}, ::Tuple{Int64}) at ./indices.jl:92
 [2] setindex_shape_check(::Array{Int64,1}, ::Int64) at ./indices.jl:143
 [3] setindex!(::Array{Int64,1}, ::Array{Int64,1}, ::UnitRange{Int64}) at ./array.jl:613

We then came across an unusual new syntax for multi dimensional arrays:

julia> [1 2 3; 4 5 6]
2×3 Array{Int64,2}:
 1  2  3
 4  5  6

julia> [1, 2, 3; 4, 5, 6]
ERROR: syntax: unexpected semicolon in array expression
Clone this wiki locally
You can’t perform that action at this time.