English 中文(简体)
Julia - Modules and Packages
  • 时间:2024-11-03

Jupa Programming - Modules and Packages


Previous Page Next Page  

The modules in Jupa 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 pnes.

Instalpng Modules

Jupa’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.jupa
egistriesGeneral`
  Resolving package versions...
Updating `C:UsersLeekha.jupaenvironmentsv1.5Project.toml`
   [a93c6f00] + DataFrames v0.21.7
No Changes to `C:UsersLeekha.jupaenvironmentsv1.5Manifest.toml`

The above command will add DataFrames package to Jupa’s environment. The (@v1.5) in the prompt tells us that we are working in the default project, "v1.5", in ~/.jupa/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 Jupa 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


jupa> using DataFrames
[ Info: Precompipng DataFrames 

[a93c6f00-e57d-5684-b7b6-d8193f3e46c0]
jupa> 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 pke 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


jupa> 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 −


jupa> import first_module

jupa> foo1()
ERROR: foo1 not defined

jupa> 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 Jupa package environment to see all the packages you have installed.


(@v1.5) pkg> status
Status `C:UsersLeekha.jupaenvironmentsv1.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] IJupa 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 Jupa uses git for organizing as well controlpng 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
         ...
      symbopc.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 ...
            ...
Advertisements