Julia - Dual command line - julia

Is there a way to have one Julia instance running, but having fx every input on the left and every output on the left? This is because sometimes it gets more difficult to keep track of what I've done in the REPL, but I do need the array to be printed after fx some calculation
julia> a = [1,2,3,4,5] | 5-element Array{Int64,1}:
| 1
| 2
| 3
| 4
| 5

Yes, atom/juno is the way to get this functionality

You can run a file line-by-line in Atom using the Juno IDE and print the output to the REPL. This will do what you would like.

Related

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

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

"I got stuck while parsing the function definition:": How to properly define an Elm function?

I'm experiment/practicing Elm for the first time and am running into difficulty defining a function. The following:
isDivisible : Int -> Int -> Int -> Bool
isDivisible n x y =
((modBy n x) == 0) && ((modBy n y) == 0)
isDivisible 4 4 2
Results in:
-- PROBLEM IN DEFINITION --------------------------------------- Jump To Problem
I got stuck while parsing the `isDivisible` definition:
5| isDivisible 4 4 2
^
I am not sure what is going wrong exactly, so here is a valid definition (with
an optional type annotation) for reference:
greet : String -> String
greet name =
"Hello " ++ name ++ "!"
Try to use that format!
The Elm interpreter that CodeWars uses lets this pass through; it just says that the output is incorrect for some inputs (like 4 4 2). There are no Google results for "I got stuck while parsing the * definition:" (even though the parser is open-source, go figure). This is my first time in a functional language. What's wrong?
I'm not sure why your isDivisible function takes three numbers, but the syntax error you're seeing does not refer to its definition, but to your call:
isDivisible 4 4 2
In Elm, all your expressions (like the above) need to live within functions. You cannot simply write them at the top level of your file. They need to be used in a context where Elm knows what to do with them.
Elm programs start executing from the main function. The main function might return different things depending on what you want to do, but the simplest use case is to return some HTML.
module Main exposing (main)
import Html exposing (text)
main =
text "Hello World"
Run in ellie
If you compile and open that in your browser, you'll see the text "Hello World" on the screen. Notice we placed our code under the main function, instead of writing them in the file directly.
With that in mind, you could do something like the following to show the output of your call:
main =
if isDivisible 4 4 2 then
text "It is divisible"
else
text "It is NOT divisible"
Run in ellie
If you just want to see the output of your call in the console instead, you can use the Debug.log function, like this:
main =
let
_ =
Debug.log "Is divisible?" (isDivisible 4 4 2)
in
text "Hello World"
Run in Ellie (see LOGS)

N-Queens example program strange output

I try the code from the squeen.icl example. When I try it with BoardSize :== 11, there is no problem. But when I change it to 12, the output is [. Why? How to fix that?
module squeen
import StdEnv
BoardSize :== 12
Queens::Int [Int] [[Int]] -> [[Int]]
Queens row board boards
| row>BoardSize = [board : boards]
| otherwise = TryCols BoardSize row board boards
TryCols::Int Int [Int] [[Int]] -> [[Int]]
TryCols 0 row board boards = boards
TryCols col row board boards
| Save col 1 board = TryCols (col-1) row board queens
| otherwise = TryCols (col-1) row board boards
where queens = Queens (row+1) [col : board] boards
Save::!Int !Int [Int] -> Bool
Save c1 rdiff [] = True
Save c1 rdiff [c2:cols]
| cdiff==0 || cdiff==rdiff || cdiff==0-rdiff = False
| otherwise = Save c1 (rdiff+1) cols
where cdiff = c1 - c2
Start::(Int,[Int])
Start = (length solutions, hd solutions)
where solutions = Queens 1 [] []
This is because you're running out of space on the heap. By default, the heap of Clean programs is set to 2M. You can change this, of course. When using clm from the command line, you can add -h 4M to its command line or to the command line of the clean program itself. If you're using the Clean IDE, you can change the heap size through Project Options, Application.
The reason that ( is still printed (which is what I get, rather than [), is the following. A Clean program will output as much of its output as possible, rather than waiting until the whole output is known. This means, for example, that a simple line as Start = [0..] will spam your terminal, not wait until the whole infinite list is in memory and then print it. In the case of squeen.icl, Clean sees that the result of Start will be a tuple, and therefore prints the opening brace directly. However, when trying to compute the elements of the tuple (length solutions and hd solutions), the heap fills up, making the program terminate.
I don't know what it looks like when you get a full heap on Windows, but on Linux(/Mac), it looks like this:
$ clm squeen -o squeen && ./squeen -h 2M
Linking squeen
Heap full.
Execution: 0.13 Garbage collection: 0.03 Total: 0.16
($
Note that the tuple opening brace is on the last line. So, when using a terminal it is quite easy to spot this error.
Interestingly, since length exploits tail recursion, the first element of the tuple can be computed, even with a small heap (you can try this by replacing the second element with []). Also the second element of the tuple can be computed on a small heap (replace the first element with 0).
The point is that the length is computed before the head, since it has to be printed the first. While with a normal length call parts of the list are garbage collected (after iterating over the first 100 elements, they can be discarded, allowing for smaller heap usage), the hd call makes sure that the first element of the list is not discarded. If the first element is not discarded, than neither can the second be, the third, etc. Hence, the whole list is kept in memory, while this is not actually necessary. Flipping the length and hd calls solve the issue:
Start :: ([Int], Int)
Start = (hd solutions, length solutions)
where solutions = Queens 1 [] []
Now, after hd has been called, there is no reason to keep the whole list in memory, so length can discard elements it has iterated over, and the heap doesn't fill up.

How to prevent REPL from truncating output?

The Julia REPL truncates output based on the window size of my terminal. For example, in a 10 line tall window, I get the following output:
julia> collect(1:50)
50-element Array{Int64,1}:
1
2
3
⋮
49
50
julia>
How can I prevent the REPL from truncating output like this?
At the moment, using Julia Version 1.4.1, the correct way would be to use #show. The semicolon suppresses the printing of the truncated version:
julia> #show collect(1:50);
collect(1:50) = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,3
0,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
One way, is with the command repr (it shows the result as a string):
julia> repr(collect(1:50))
"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]"
Another way, is with the command show:
julia> show(collect(1:50))
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,3
0,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50]
tested with Julia Version 0.4.3

Trying to create an asynchronous example using Core.Async.Pipe, but Pipe.write seems to block awaiting a Pipe.read

I've created the following toy example that counts in a loop and writes the value to an Async.Pipe:
open Sys
open Unix
open Async.Std
let (r,w) = Pipe.create ()
let rec readloop r =
Pipe.read r >>=
function
| `Eof -> return ()
| `Ok v -> return (printf "Got %d\n" v) >>=
fun () -> after (Core.Time.Span.of_sec 0.5) >>=
fun () -> readloop r
let countup hi w =
let rec loop i =
printf "i=%d\n" i ;
if (i < hi &&( not (Pipe.is_closed w))) then
Pipe.write w i >>>
fun () -> loop (i+1)
else Pipe.close w
in
loop 0
let () =
countup 10 w;
ignore(readloop r);;
Core.Never_returns.never_returns (Scheduler.go ())
Notice the readloop function is recursive - it just continuously reads values from the Pipe as they are available. However, I've added a delay there of 0.5 sec between each read. The countup function is kind of similar but it loops and does a write to the same Pipe.
When I run this I get:
i=0
i=1
Got 0
i=2
Got 1
i=3
Got 2
i=4
Got 3
i=5
Got 4
i=6
Got 5
i=7
Got 6
i=8
Got 7
i=9
Got 8
i=10
Got 9
Aside from the first three lines of output above, all the rest of the output lines seem to need to wait the half second. So it seems that the Pipe is blocked after a write until there is a read from the Pipe. (Pipe.write w data appears to block waiting for a Pipe.read r )
What I thought should happen (since this is an Async Pipe of some sort) is that values would be queued up in the Pipe until the reads take place, something like:
i=0
Got 0 (* now reader side waits for 1/2 second before reading again *)
i=1 (* meanwhile writer side keeps running *)
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9 (* up till here, all output happens pretty much simultaneously *)
Got 1 (* 1/2 second between these messages *)
Got 2
Got 3
Got 4
Got 5
Got 6
Got 7
Got 8
Got 9
I'm wondering if there is a way to get the behavior using Async?
My real usecase is that I've got a Tcp socket open (as a client) and if I were using threads after some setup between the client and the server I would start a thread that just sits and reads data coming in from the socket from the server and put that data into a queue of messages that can be examined in the main thread of the program when it's ready. However, instead of using threads I want to use Core.Async to achieve the same thing: Read data from the socket as it comes in from the server and when data is available, examine the message and do something based on it's content. There could be other things going on as well, so this is simulated by the "wait half a second" in the code above. I thought Pipe would queue up the messages so that they could be read when the reader side was ready, but that doesn't seem to be the case.
Indeed, pipe is a queue, but by default its length is set to 0. So that, when you're pushbacking, a producer will stop immediately and wait. You can control the size with a set_size_budget function.

Resources