I can do that with relative paths include("./lib/finance.jl") and use as Finance.some_finance_fn().
But how to do the same with packages? include("statistics.jl") is not working. And if I include it as using Statistics all the functions will be exported in the current scope. I would like to avoid that and call it like Statistics.mean not mean.
P.S.
Because sometimes it conflicts with local variable names and I can't name variable mean because it conflicts wiht mean functions exported from Statistics module.
Use the import keyword (see Modules documentation):
julia> import Statistics
julia> mean
ERROR: UndefVarError: mean not defined
julia> Statistics.mean
mean (generic function with 5 methods)
For comparison, with the using keyword all exported identifiers are brought into the current scope:
julia> using Statistics
julia> mean
mean (generic function with 5 methods)
julia> Statistics.mean
mean (generic function with 5 methods)
Related
I thought that a lst could be written as following.
But using the typeof() function one can see that its not a list.
julia> a = [1,"test", π]
typeof(a)
Vector{Any} (alias for Array{Any, 1})
If you come from Python then what Python calls a list is the same as Vector{Any} in Julia in your example.
However, if you are interested in a linked list data structure instead then you have it in DataStructures.jl package, see here.
At some point, (I think Julia v0.7) you could do #save savepath thingtosave in order to save files using Julia. I tried to run this on v0.7 to see if I got a deprecation warning but even on 0.7 it says that #save is undefined.
How can I programmatically save files using Julia?
Since you mention #save, presumably, you were using JLD.jl or its successor JLD2.jl.
A simple example for using JLD2 would be
julia> using JLD2
julia> #save "test.jld2" x
julia> x = nothing # "forgetting" x
julia> #load "test.jld2"
1-element Array{Symbol,1}:
:x
julia> x
2×2 Array{Float64,2}:
0.698264 0.319665
0.252174 0.80799
In contrast to write, those packages are based on HDF5 (through HDF5.jl). They pretty much allow you to store arbitrary Julia objects. HDF5 (not necessarily JLD/JLD2) is a file format which is supported by almost all programming languages and many programs (Mathematica for example). It is suitable for long-term storage in contrast to read/write which might change in future Julia versions.
Note that this doesn't show up in 0.7 since it is a package feature and not part of Base (or a stdlib).
From the julia docs, there is the write function:
write(io::IO, x)
write(filename::AbstractString, x)
Write the canonical binary representation of a value to the given I/O stream or file. Return the number of bytes written into the stream. See also print to write a text representation (with an encoding that may depend upon io).
You can write multiple values with the same write call. i.e. the following are equivalent:
write(io, x, y...)
write(io, x) + write(io, y...)
writing a text file:
write("text.txt","this is a test")
I'm using Julia 1.1 and I tried to use for loop do the following simple things:
i_index=1;
for index in (1:100)
i_index=i_index+1;
end
However, I got an error saying:
ERROR: UndefVarError: i_index not defined
I have tried several times and variations, but they all failed to work. Is this a bug? or why Julia can't do this simple iterative addition?
In the REPL:
i_index=1;
for index in (1:100)
global i_index;
i_index=i_index+1;
end
This is because of variable scope, see in Julia documentation. Note that the examples there pertain to the REPL.
Lets say I have a Julia module, MyModule, that defines a function called conv() for 1D convolution. I would like to be able to call this function via MyModule.conv() in a file that imports or uses the file. Example:
import MyModule.conv
MyModule.conv()
However, I cannot get this syntax to work; Julia still calls Base.conv() instead of MyModule.conv(). I have tried all the different styles of using and import but cannot get this syntax to work.
Is this functionality available in Julia? I feel like I have seen this be implemented in other Julia packages but cannot find an example that works.
EDIT
Current setup is as follows; there is no reference to conv() anywhere in ModA outside of the definition.
module ModA
function conv(a, b)
println("ModA.conv")
end
end
Then, in another file,
import ModA
conv(x, y) #still calls Base.conv()
SOLVED
This was entirely my fault. The import wasn't working because of an incorrect LOAD_PATH calling a different version of the file than I thought was being called (finding the first one in the LOAD_PATH, which was not the one that I was editing). Entirely my fault...
Do you mean something like this?
module ModA
conv() = println("Calling ModA.conv")
end
module ModB # Import just conv from ModA
using ..ModA.conv # Could have used import ..ModA.conv instead
conv() # if we want to be able to extend ModA.conv
end
module ModC # Import ModA and use qualified access to access ModA.conv
import ..ModA
ModA.conv()
end
You must make sure not to make any reference to conv inside ModA before you have defined your own function, or it will already have looked up Base.conv and associated the name conv with that.
I've installed Julia Version 0.3.10-pre+4 and am trying to run the examples contained in julia/examples, in particular plife.jl, which seems to be a parallel simulation of Conway's game of life. I have the error
julia> include("plife.jl")
plife (generic function with 1 method)
julia> plife(100,100)
ERROR: Window not defined
in plife at /x/faia/julia/examples/plife.jl:44
where the error is caused by the code
function plife(m, n)
w = Window("parallel life", n, m)
c = Canvas(w)
I have searched for a package that defines the Window function, but have failed to find it. Does anyone know how to run this example code?
That example seems to be reliant on an external package, it looks like it might be Tk.jl but there have been some renames since then.
It has actually been removed on the development branch of Julia, but still lingers in the 0.3 series. I've filed a PR to remove it.