functionmodulejupyter-notebookjuliaijulia-notebook

Creating module in Julia using jupyter notebook


I am new to the IJulia platform. I am using jupyter notebook (Anaconda). I was trying to create a julia module in jupyter notebook. My functions are in different cells. Is it necesssery that all the functions should be inside of the delimiter module ModuleName......end (in a single cell) ? or can I put the "end" after all the function cells ? Kindly giude me.


Solution

  • When you execute a cell in the notebook, the containing code will be passed on to the IJulia kernel for evaluation. Therefore, the code within a cell has to be a correct julia statement. So when you open a module block with module ModuleName, you have to close the block within the same cell, which also means, that all function definitions have to be within that cell. If you really want to keep you module definition in the Notebook, and you really want to separate your individual functions into different cells, you could go ahead and define you functions in the module without any methods like so:

    module MyModule
        function f end
        function g end
    end
    

    and then, in different cells, add your method definitions like so:

    function MyModule.f(x)
        println("Hello World", x)
    end
    

    or:

    MyModule.g(a,b) = 4*a + b
    

    although I am not sure if doing so would be considered good style... Depending on the amount of code, I would usually move my module code into its own package, and then load it into the notebook, usually with Revise to get hot-reloading of my package code.