Create a two-dimensional array at runtime: Difference between revisions

Added Julia language
(→‎{{header|Rust}}: switch to `vec` macro)
(Added Julia language)
Line 1,136:
Julia supports n-dimensional arrays as native data types: `Array{T, N}`, where `T` is the type of it´s elements and `N` is the number of dimensions.
 
<lang julia>function input(prompt::AbstractString)
print(prompt)
julia> "Inspired by Python's `input` function."
return chomp(readline())
function input(prompt::AbstractString="")
end
print(prompt)
chomp(readline())
end
input (generic function with 2 methods)
 
julia> n = parse(Int, input("Upper bound for dimension 1: ")) # parse as `Int`|>
x -> parse(Int, x)
m = input("Upper bound for dimension 12: 5") |>
5
x -> parse(Int, x)
 
x = rand(n, m)
julia> m = parse(Int, input("Upper bound for dimension 2: "))
display(x)
Upper bound for dimension 2: 5
julia> x[3, 3] # overloads `getindex` generic function
5
julia> x[3, 3] = 5 .0 # overloads `setindex!` generic function
 
julia> x::Matirx Matrix # `Matrix{T}` is an alias for `Array{T, 2}`
julia> x = rand(n, m) # create an n·m random matrix
julia> x = 0; gc() # Julia has no `del` command, rebind `x` and call the garbage collector</lang>
5x5 Array{Float64,2}:
0.80217 0.422318 0.594049 0.45547 0.208822
0.0533981 0.304858 0.0276755 0.797732 0.828796
0.522506 0.563856 0.216759 0.865961 0.034306
0.792363 0.815744 0.868697 0.42509 0.588946
0.112034 0.539611 0.674581 0.508299 0.939373
 
julia> x[3, 3] # overloads `getindex` generic function
0.21675944652281487
 
julia> x[3, 3] = 5 # overloads `setindex!` generic function
5
 
julia> x::Matirx # `Matrix{T}` is an alias for `Array{T, 2}`
5x5 Array{Float64,2}:
0.80217 0.422318 0.594049 0.45547 0.208822
0.0533981 0.304858 0.0276755 0.797732 0.828796
0.522506 0.563856 5.0 0.865961 0.034306
0.792363 0.815744 0.868697 0.42509 0.588946
0.112034 0.539611 0.674581 0.508299 0.939373
 
julia> x = 0; gc() # Julia has no `del` command, rebind `x` and call the garbage collector
</lang>
 
Manually calling the garbage collector may or may not actually collect the array, but it will be eventually.
Anonymous user