I don't understand Julia. From running in the command line to workflow

My code

I’m coming from languages like C, python and mathematica (I only really use mathematica for some data handling and scripting)

I have quite a few things I’m struggling with to be honest.
The only way I know how to run this code is typing “julia simulation.jl {args}” into cmd. I haven’t really been able to get this to accept arguments in REPL. I’ve searched online and there seems to be some hacky ways of redefining the ARGS objects before you run it to get it to accept them, but I can’t see how this is even close to the intended way.

I’ve been doing everything from VSCode, but I need to share this file with other people so that they can run and test it (this is an assignment so I need the professor to be able to run it without much hassle from his side).

I’ve seen many people recommend wrapping everything in functions, and I think I’m doing that but I can never be too sure.

I’m just at a loss on how to manage all this. Learning about functions online is an alright experience as long as it’s self-contained. But I’m finding that learning about anything on a broader (or “meta”) scope is very difficult and there’s not many sources that actually give a good overview in my opinion

1 Like

Welcome to Julia Discourse.

  1. Your code repository does not appear to be public, so we cannot take a look
  2. Posting here is a good way to get answers to specific questions.
  3. If you want something interactive, consider the Julia Slack or Zulip
  4. The primary documentation is here:
    Julia Documentation · The Julia Language
  5. Try Julia Academy:
    https://juliaacademy.com/

Regarding your args issue, I recommend the following structure.

function main(args::Vector{String} = ARGS)
    nargs = length(args)
    arg1 = 1
    arg2 = 2
    if length(args) > 0
        arg1 =parse(Int, args[1])
    end
    if length(args) > 1
        arg2 = parse(Int, args[2))
    end
    simulation(arg1, arg2)
end
function simulation(arg1, arg2,)
    # your actual simulation
end
if abspath(PROGRAM_FILE) == @__FILE__
    main()
end

This allows you to run the file from the command line or load the file via include. You can then either invoke main(String["1", "2"]) or simulation(1,2).

3 Likes

I believe that I have changed the visibility of my project now.

1 Like

If the professor has Julia they would run it with the same Julia command as you do.
If they do not have Julia, this might be problematic of course.

In general for something reproducible where you want to interactively develop something maybe also Pluto.jl might be worth taking a look?

You may find this website useful: Modern Julia Workflows

Usually in Julia you would share your simulation as a package and ask the person you share it with to install or activate it then run some function defined in your package.

Install Option:

pkg> add https://github.com/myuser/MyAwesomePackage

Download and Activate Option:

pkg> activate /path/to/MyAwesomePackage
pkg> instantiate

Running:

julia> using MyAwesomePackage
julia> runSimulation()

You can also define a function named __init__() in your package that will run as part of the using step. I tend to print some user instructions with it.

5 Likes