Is it possible to break 1 line of code into multiple in atom IDE, just like in Rstudio - julia

I am new to using Julia and the atom IDE, and I was wondering if it was possible to somehow just press enter and have the computer run 1 line of code spread over multiple lines, and still have it recognize that it is still the same 1 line of code, just like in Rstudio? (I want this for readability purposes only)
what I mean is something like:
println("Hello
world")
and be able to highlight and run this script without receiving an error message.

Yes. The method differs based on what the line of code contains, or specifically, where you want to break the line.
For a string like you've posted in the question, you have to precede the newline with a \ character, to inform Julia that you're using this newline only for readability, and don't want it to be included in the actual string. (Note: I'll be illustrating these with the command-line REPL that has the julia> prompt, but the same principles apply in the Atom/VS Code based IDE setups too).
julia> println("Hello \
world")
Hello world
Outside of strings, if the current line isn't complete, Julia automatically looks for the continuation in the next line. This means that to break a line into multiple ones, you have to leave all but the final line incomplete:
julia> 1 +
2 +
3 *
4
15
julia> DEPOT_PATH |>
first |>
readdir
16-element Vector{String}:
"artifacts"
"bin"
⋮
julia> 1,
2,
3
(1, 2, 3)
In some cases when you don't have a convenient binary operator to leave hanging like the above, you may have to start your line with an opening parenthesis, so that Julia will know that it's a continuous statement until the closing parenthesis is encountered.

You also have multi-line Strings denoted by """:
julia> println("""This
is
a multi-line \
text""")
This
is
a multi-line text

Related

Variable output in Pluto Notebook

I'm in the process of working on a coding project in a pluto notebook. It's early days but I'm curious why the value of 1000 doesn't show beside quadorder in the screenshot above. It seems to output for everything else!
It is the ; in your code that triggers that. However, I don't think it is supposed to be expected behavior. It is supposed to be a comment, but the parser probably sees the ; and thinks that it is part of the code.
If you put it between quoting marks then this doesn't happen.
I.e. this should work as expected:
quadorder = 1000 # like python Julia doesn't require ';'
Otherwise, it would usually also work if you just don't put it at the end of the line.
The Julia REPL evaluates the semicolon wrongly and it is a know issue github.com/JuliaLang/julia/issues/28743.
You can actually trick it into some weird behavior that suppresses output. For example this returns no output:
julia> ";#"
julia> a = ["; #", "hi", 3]
julia> a = "223" #;
The reason is that the REPL parser looks for the last semicolon in the line and if there is any # or if it is at the end of the line (whitespaces don't matter), then it suppresses any output.

Julia print statement not working in certain cases

I've written a prime-generating function generatePrimes (full code here) that takes input bound::Int64 and returns a Vector{Int64} of all primes up to bound. After the function definition, I have the following code:
println("Generating primes...")
println("Last prime: ", generatePrimes(10^7)[end])
println("Primes generated.")
which prints, unexpectedly,
Generating primes...
9999991
Primes generated.
This output misses the "Last prime: " segment of the second print statement. The output does work as expected for smaller inputs; any input at least up to 10^6, but somehow fails for 10^7. I've tried several workarounds for this (e.g. assigning the returned value or converting it to a string before calling it in a print statement, combining the print statements, et cetera) and discovered some other weird behaviour: if the "Last prime", is removed from the second print statement, for input 10^7, the last prime doesn't print at all and all I get is a blank line between the first and third print statements. These issues are probably related, and I can't seem to find anything online about why some print statements wouldn't work in Julia.
Thanks so much for any clarification!
Edit: Per DNF's suggestion, following are some reductions to this issue:
Removing the first and last print statements doesn't change anything -- a blank line is always printed in the case I outlined and each of the cases below.
println(generatePrimes(10^7)[end]) # output: empty line
Calling the function and storing the last index in a variable before calling println doesn't change anything either; the cases below work exactly the same either way.
lastPrime::Int = generatePrimes(10^7)[end]
println(lastPrime) # output: empty line
If I call the function in whatever form immediately before a println, an empty line is printed regardless of what's inside the println.
lastPrime::Int = generatePrimes(10^7)[end]
println("This doesn't print") # output: empty line
println("This does print") # output: This does print
If I call the function (or print the pre-generated-and-stored function result) inside a println, anything before the function call (that's also inside the println) isn't printed. The 9999991 and anything else there may be after the function call is printed only if there is something else inside the println before the function call.
# Example 1
println(generatePrimes(10^7)[end]) # output: empty line
# Example 2
println("This first part doesn't print", generatePrimes(10^7)[end]) # output: 9999991
# Example 3
println("This first part doesn't print", generatePrimes(10^7)[end], " prints") # output: 9999991 prints
# Example 4
println(generatePrimes(10^7)[end], "prime doesn't print") # output: prime doesn't print
I could probably list twenty different variations of this same thing, but that probably wouldn't make things any clearer. In every single case version of this issue I've seen so far, the issue only manifests if there's that function call somewhere; println prints large integers just fine. That said, please let me know if anyone feels like they need more info. Thanks so much!
Most likely you are running this code from Atom Juno which recently has some issues with buffering standard output (already reported by others and I also sometimes have this problem).
One thing you can try to do is to flush your standard output
flush(stdout)
Like with any unstable bug restarting Atom Juno also seems to help.
I had the same issue. For me, changing the terminal renderer (File -> Settings -> Packages -> julia-client -> Terminal Options) from webgl to canvas (see pic below) seems to solve the issue.
change terminal renderer
I've also encountered this problem many times. (First time, it was triggered after using the debugger. It is probably unrelated but I have been using Julia+Juno for 2 weeks prior to this issue.)
In my case, the code before the println statement needed to have multiple dictionary assignation (with new keys) in order to trigger the behavior.
I also confirmed that the same code ran in Command Prompt (with same Julia interpreter) prints fine. Any hints about how to further investigate this will be appreciated.
I temporarily solve this issue by printing to stderr, thinking that this stream has more stringent flush mechanism: println(stderr, "hello!")

When defined in a script, Julia functions don't print output

I have this Julia script:
function f(x,y)
x+y
end
f(3, 4)
When I run this in the live terminal (via copy/paste), I get the desired result 7. But if I run the script, the output from the function is suppressed. Why is that?
Julia, unlike Matlab, doesn't automatically print values (the REPL does since that's what it's for: REPL = "read, eval, print loop"). You have to explicitly print the value using print or show, e.g. show(f(3, 4)). In this case, print and show do the same thing, but in general they have somewhat different meanings:
print([io::IO], xs...)
Write to io (or to the default output stream stdout if io is not given) a canonical (un-decorated) text representation. The representation used by print includes minimal formatting and tries to avoid Julia-specific details.
versus
show(x)
Write an informative text representation of a value to the current output stream. New types should overload show(io::IO, x) where the first argument is a stream. The representation used by show generally includes Julia-specific formatting and type information.
Note that there is also the #show macro, which prints the expression that is evaluated followed by its value, like so:
julia> #show f(3, 4);
f(3, 4) = 7

Count the number of statements per line in C/C++

Given a C program (could be a C++, although for now, i'd stick to C), I want to count the number of statements for every line of code (excluding comments etc of course)
I have been writing a parser to do that - but clearly i keep encountering code that kinda fails.
so if a code line has "i= 0; i++; i--;" on a single line, i want my parser to return 3 for this line. If i have "if (x) { x++}; else x--;" on a single line, then it should return 3 (if, x++, x--). Are there tools that already do this (does pycparser provide an option to return the number of statements in a given line?)

Command to reproduce the evaluation command in jupyter notebook

I'm using jupyter notebook. Consider ar = np.array([[2,3],[5,6]]).
Then evaluating print ar displays
[[2 3]
[5 6]]
while just evaluating ar displays
array([[2, 3],
[5, 6]])
My question is: 1) What command lies actually behind this evaluation in the notebook, how could I reproduce it in a normal IDLE python script?
2) What does the second evaluation mean, which is some form of elaborate priting; does is show me the type of the object + its contents? Shouldn't it actually be ndarray instead of array?
1) I think it is more complicated than a single command. I think the code that parses which "interactivity mode" (e.g. display all, just the last line [default]) to run is here. Remember that IPython is like it's own REPL shell and there are lots of different interwoven mechanisms like this one. You could trace back through their source code and try to understand all the mechanisms involved if you wanted to, but I don't think it's a one line solution.
2) This is printing the representation of ar, repr(ar), versus the readable string form of ar. See https://stackoverflow.com/a/2626364/7458681 The reason it is array and not ndarray is that it is not the type of the object that is being printed but instead the function required in order to be able to recreate the object such that eval(repr(ar)) == ar.

Resources