”;
The modules in Julia programming language are used to group together the related functions and other definitions. The structure of a module is given below −
module ModuleName end
We can define and put functions, type definitions, and so on in between above two lines.
Installing Modules
Julia’s package manager can be used to download and install a particular package. To enter the package manage from REPL, type ] (right bracket). Once entering the package manager, you need to type the following command −
(@v1.5) pkg> add DataFrames Updating registry at `C:UsersLeekha.juliaregistriesGeneral` Resolving package versions... Updating `C:UsersLeekha.juliaenvironmentsv1.5Project.toml` [a93c6f00] + DataFrames v0.21.7 No Changes to `C:UsersLeekha.juliaenvironmentsv1.5Manifest.toml`
The above command will add DataFrames package to Julia’s environment. The (@v1.5) in the prompt tells us that we are working in the default project, “v1.5”, in ~/.julia/environments/.
Using Modules
Once installed, it is time to start using the functions and definitions from the installed module. For this we need to tell Julia programming language to make code available for our current session. Use using statement which will accept the names of one or more installed modules.
Example
julia> using DataFrames [ Info: Precompiling DataFrames [a93c6f00-e57d-5684-b7b6-d8193f3e46c0] julia> empty_df = DataFrame(X = 1:10, Y = 21:30) 10×2 DataFrame │ Row │ X │ Y │ │ │ Int64 │ Int64 │ ├─────┼───────┼───────┤ │ 1 │ 1 │ 21 │ │ 2 │ 2 │ 22 │ │ 3 │ 3 │ 23 │ │ 4 │ 4 │ 24 │ │ 5 │ 5 │ 25 │ │ 6 │ 6 │ 26 │ │ 7 │ 7 │ 27 │ │ 8 │ 8 │ 28 │ │ 9 │ 9 │ 29 │ │ 10 │ 10 │ 30 │
Import
Like using, import can also be used for modules. The only difference is that import lets you decide how you would like to access the functions inside the module. In the below example, we have two different functions in a module. Let us see how we can import them −
Example
julia> module first_module export foo1 function foo1() println("this is first function") end function foo2() println("this is second function") end end Main.first_module
Now we need to use import to import this module −
julia> import first_module julia> foo1() ERROR: foo1 not defined julia> first_module.foo1() "this is first function"
You can notice that the function foo1() can only be accessed if it is used with module prefix. It is because the first_module was loaded using import command rather than using command.
Include
What if you want to use the code from other files that are not contained in the modules? For this you can use include() function which will evaluate the contents of the file in the context of the current module. It will search the relative path of the source file from which it is called.
Packages
Use status command in Julia package environment to see all the packages you have installed.
(@v1.5) pkg> status Status `C:UsersLeekha.juliaenvironmentsv1.5Project.toml` [336ed68f] CSV v0.7.7 [a93c6f00] DataFrames v0.21.7 [864edb3b] DataStructures v0.18.6 [7806a523] DecisionTree v0.10.10 [38e38edf] GLM v1.3.10 [28b8d3ca] GR v0.52.0 [86223c79] Graphs v0.10.3 [7073ff75] IJulia v1.21.3 [682c06a0] JSON v0.21.1 [91a5bcdd] Plots v1.6.8 [d330b81b] PyPlot v2.9.0 [ce6b1742] RDatasets v0.6.10 [3646fa90] ScikitLearn v0.6.2 [f3b207a7] StatsPlots v0.14.13 [b8865327] UnicodePlots v1.3.0 [112f6efa] VegaLite v1.0.0
Structure of a package
As we know that Julia uses git for organizing as well controlling the packages. All the packages are stored with .ji prefix. Let us see the structure of Calculus package −
Calculus.jl/ src/ Calculus.jl module Calculus import Base.ctranspose export derivative, check_gradient, ... include("derivative.jl") include("check_derivative.jl") include("integrate.jl") end derivative.jl function derivative() ... end ... check_derivative.jl function check_derivative(f::...) ... end ... integrate.jl function adaptive_simpsons_inner(f::Funct ... end ... symbolic.jl export processExpr, BasicVariable, ... import Base.show, ... type BasicVariable <: AbstractVariable ... end function process(x::Expr) ... end ... test/ runtests.jl using Calculus using Base.Test tests = ["finite_difference", ... for t in tests include("$(t).jl") end ... finite_difference.jl @test ... ...
”;