Strange behaviour with StdIn.readLine in Scala? - scala

I came across some strange behaviors of StdIn.readLine in Scala. Here's the code excerpt:
while (true) {
print("prompt:>")
var lbl = ""
lbl = scala.io.StdIn.readLine()
println("echo" + lbl)
}
Strangely, when running, the readLine() happens before printing out the "prompt:>".
I have:
fork in run := true
connectInput in run := true
in my build.sbt
Anyone knows what's happening?

The output is line buffered. You need to System.out.flush after printing the prompt to make it show up in the console. Otherwise, it waits for the buffer to fill (or a new line to appear in the output).

Writing to files (and the terminal is considered one) is costly, so for performance reasons the standard output is buffered. This means that the write will actually only occur once the buffer becomes full, or once a line return gets put into the buffer. You can force the buffer to flush using System.out.flush.
For your problem however, you might want to use scala.io.StdIn.readLine("prompt:>") directly: it's made exactly for what you're trying to do.

I had this problem when running codes in the console, i.e Ctrl+shift+X on a piece of code in IntelliJ to have them run in the console. I added those line you indicated in the build.sbt and for feeding in the input into the console I use Ctrl+Enter after typing the input. Just for your info, my Scala code is:
import scala.io.StdIn.{readLine}
val input = readLine("enter some word ")
println(s"the word is $input ")

Related

How do you do multiple line entry in the command line REPL of SuperCollider?

Just as the title says, How do you do multiple line entry in the command line REPL of SuperCollider? For example, in Haskell, you use :{ and :} to open and close multiple line entry in ghci.
If you're using sclang with the -i flag (meaning e.g. sclang -i something), the keycode to execute previously entered text is esc, followed by a newline. For example:
~a = {
"test".postln;
}^[
~a.();^[
outputs: test
This works file if you're driving sclang from an IDE or another scripting context (this is used for sclang's unit tests). If you're using the repl, it appears that there ISN'T a way to do multiline entries - the repl uses readline which doesn't have multiline support out of the box. This should probably be filed as a bug.

In Scala REPL, how to discard partly written multi-line command?

On Unix Shell terminal, you can discard a typed command just by typing ctrl-c.
Is there a way to discard a typed command on Scala REPL?
Ctrl-C still works but it would exit the Scala REPL, which is probably not what you prefer.
A couple of cases:
If you're in the middle of a single-line command, you can hit Up button then Down to an empty command line.
If you're in the middle of a multi-line command and the command is incomplete, you can simply hit return a couple of times to let the REPL interpret the consecutive blank lines as intention to start a new command. But, in case you worry about whatever you've typed might get executed, safest way would still be to hit Ctrl-C.
When you're writing an unfinished block you can enter two blank lines and the REPL will then skip your command and start a new one.
You may then still use up buttons to retrieve the lines you want to continue with in the new command.

How do you cope with emacs halting on receiving big input?

I am developing project in clojure using emacs cider under windows. And sometimes I have a problem that after accidently forgotten println function or on printing contents of big file Emacs stops responding (cursor and all key combinations doesn't work) and retire into oneself for processing that information to show it in repl. The only way to continue I know is to close program and open project files from scratch. And it is so simple to get in this trap.
Are there any other better solutions or configuration restrictions?
Though this suggestion will not solve your problem completely, it can help you a little.
First, set *print-length* to some value to limit the number of items of each collection to be printed.
(set! *print-length* 10)
And use cider-connect instead of cider-jack-in. You should run lein repl in a separate console window, then run cider-connect to connect to the repl. Then you can evaluate some expressions in the console window.
It would be good if there's an option to limit the contents to be printed by number of characters, however, I couldn't find it.

How to make my scala program quit and return to the REPL?

So I made this scala file and it works great when I load it into the REPL. What I want to do though is when the user inputs "Q", it exits the program and returns to the REPL. I already have readLine set up with a case match that says:
case "Q" =>
I just don't know what to put after it to make the program quit.
Thanks
You can use System.exit(0) provided you fork a new console / REPL. If you run via SBT, then fork in console := true will accomplish that. If you're launching a REPL from within your code and using run in instead of console, then you'd want fork in run.
If you want to run a stand-alone REPL, then start your program and eventually have it exit back to the REPL, then you'll need to simply stop your read loop and return out of the entry-point method you called to start it up.
Given how little code you included, it's hard to say much more than this.

Oddball issue arising with use of TextWrangler to edit Python 2.7 script which tests multithreading and Queue modules

I'm writing a script in Python 2.7 exploring the use of the multithreading and Queue modules. the script just defines two functions, one of which will be started in a thread instance to fill a Queue, and a second one which will be started in a second thread instance to pull something off the Queue. However, I cannot even get this script to execute, as it chokes on one of the function definitions even before it executes. The code snippet with the problem is:
def listenThread(counter):
while queue.empty() != True:
try:
outcome = queue.get()
print outcome
counter -=1
print counter
except:
return 'queue.get() command loop failing.'
The error message I am getting is:
$ python threading-and-queue-test.py
File "threading-and-queue-test.py", line 34
except:
^
IndentationError: unindent does not match any outer indentation level
$
I've searched SO and checked the docs for correct formation of the try-except construct, and it seems OK. I had earlier versions of the code (without the try-except ) working, so I am getting suspicious about the text editor, TextWrangler v4.0.1.
Can anyone suggest a debug approach or code correction?
Thanks!
Red
As you have already suspected, it is most likely an indentation error. Your text editor is probably mixing the use of tabs and spaces. Replace all tabs with spaces (or vice versa though the standard in python is 4 spaces) and you should see the error disappear.
For those who find this question later like I did it's easy to fix TextWrangler's settings and stop this issue altogether; Go to "Preferences" -> "Editor Defaults" -> "Auto-expand tabs" then set tabs to 4 spaces. Restart TextWrangler for changes to take affect. For existing documents that continue giving you a problem read this.