Julia Community 🟣

ndx
ndx

Posted on

Do you have a datatype, but keep googling the functions to work with it? Try This!!!

Someone posted this in the Julia reddit page Pains of Julia compared to Python. In that article, he listed out 7 things he loved in Python but couldn't find in Julia. I found interest in number 7, because my first few weeks in Julia gave me same pain (which would later be the reason I loved the language more). It states, reading from the article:

  • 7. Lack of OOP. Julia is strictly a functional programming language - there are no objects. So, if you want to perform operation on a datatype, you have to remember! (or google) the name of the function. For example, if you have a graph datatype named my_graph, in a language with OOP (such as python), you can type my_graph. and your IDE can suggest functions available for the my_graph object, such as my_graph.topo_sort(). In Julia, you will have to remember (or google) topo_sort function is available to process graph datatype and will have to type topo_sort(my_graph). This is the part I am having the hardest time getting adjusted to and has been a major cause of slow down.

I won't explain if Julia is OOP or not, but focus on the other part that has to do with remembering functions to work with in the datatype. Now, I'm sure a lot of user had same pain and I'd love to know how you conquered it. Meanwhile, let me share my own experience of how I solved the puzzle in a way that got me used to the langauge, where I don't need much googling, but rather explanations on specific implementations of some methods I might not understand, which the Julia community on slack and discourse are happy to help with.

Honestly, if you haven't been told yet, then hear it clearly now: Julia would be a bone in your throat if you hate using the REPL it provided. A bone in your throat, because you would know what to do a lot of the time, but don't know how to do it in Julia.

You can solve this using the steps I stated in the reddit answer I gave to the thread.

Well now lets see its implementation here on Julia forum.

The mandatory step is to download Anaconda, VS Code and set it up for Jupyter Notebook and Julia. Just google the workarounds for setting this steps up and ask questions politely (in any one of Julia forums) if you're stuck, then in 1 hour you should be done.

Hopefully you're done with the steps above and now it's time to work into the magic that helped me get familiar with Julia and understand most of its methods.

  • Open Up Your VS Code

Image description

  • From the image's description, press ^ and \ to toggle the terminal or use ⌘ + J keys to toggle panel. The screen should now look this way.

Image description

  • Right click on Terminal and then from the dialog-box opened, click on Move Panel Right.

Image description

If you have done so, then you screen should now look this way.

Image description

  • Now copy the execution path of Julia on your local computer and paste it into the terminal on VS Code. An easy way to get the execution path is to run the Julia REPL and then copy the execution path from the command line. The execution path as seen in the picture below is the text circled on yellow borders.

Image description

If you've done so then your VS Code terminal should look like the image below.

Image description

We all know how to set up the terminal for VS Code now and have it run our Julia REPL, making us have it all in one environment.

  • Open a new Julia file from the code editor in VS Code (use ⌘ + N keys) and click on open a different editor as shown in the image below.

Image description

  • Select the Jupyter Notebook from the drop-down menu as shown in the image below.

Image description

  • Your VS Code editor should now look like this.

Image description

  • Now go into the cells of the notebook and type in this function as shown below.
function methods_inside(modules...)
    for m in modules
        println($(uppercase(m)))
        println.(names(m))
        println()
    end
end
Enter fullscreen mode Exit fullscreen mode

Now, at this point you should know the Core, Base and InteractiveUtils modules are considered the standard libraries of the Julia programming language; meaning you can't do any meaningful work in Julia without them. So all methods exported by the standard libraries are the methods provided by Julia.

The names method used in the function defined above is used to get an array of the names exported by a Module, excluding deprecated names.

So we will pass those 3 modules of the standard libraries as arguments to our function we created. Type the code below into the next cell of your notebook.

methods_inside(Core, Base, InteractiveUtils)
Enter fullscreen mode Exit fullscreen mode
  • If you have done the above, your editor should now look like this.

Image description

  • Click on in a text editor to get the whole text, because text files are truncated to fit into the screen by the default settings in VS Code (you can change this if you want). Your VS Code editor should now like this.

Image description

You now have all the methods in the standard libraries in a text editor. You can scroll down and then see the methods listed in Base module and InteractiveUtils as well. Your laser-like focus should be on the Base first and then work your way back to the Core and then the InteractiveUtils section of the methods (its not a rule, that's how I did mine and it worked for me, so change it to fit your needs).

The question next is: what do we do with this? Well the answer is what I did with it (as that is where my knowledge is limited to), as I can't tell you how you would use it.

  • Save the text file that has all the methods with a .txt file extension (so it can be opened up in VS Code), then ALWAYS keep it open in your VS Code environment when working with Julia as well as the Julia REPL on the terminal in the right hand side, this would make your workflow so swift.

Image description

I saved mine with All Methods.txt. So each time I open up VS Code for working with Julia, I could easily lookup methods and then use the help?> entered by typing ? on the Julia REPL to quickly find out their meanings all in one environment.

I know this might sound stupid, but I usually do a very quick glance through the All Methods.txt file before writing my own methods for things, a lot of the time Julia has defined what I want executed. An example is checking if all the elements in an interable is distinct from each other. One would think a simple for...end loop is an easy way to solve this. Of course, it is, but glancing through the file, I see the allunique method, which sounds like what I want. Then I look up its documentation on the help session in the REPL and immediately I know its what I want. Would google had helped me? Probably yes/no, but whatever the answer may be, using this approach keeps making me familiar to the standard library of the language.

Image description

Another method is checking the perfect square less than or equal to a number. One would easily jump into coding, but trust me, as simple as this function sounds, Julia has provided it for you.

Image description

Using the built-in functions, means your code would be more Julian, making its intents readable among the Julia community and run faster as well. So please use it.

On my study and free-time as well, I also explore new methods. Julia is so new, so a lot of questions which can help reveal new methods or ways of doing things have not been asked yet on stackoverflow or whatever, so googling won't help now. Your best bet is to learn them yourself and write more syntactic Julian code thereby making your code more scientific and program run more faster.

How did you solve yours, I'd love to know?

Top comments (0)