Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print whole result in interactive Scala console

When I type something into the Scala interactive console, the console prints the result of the statement. If the result is too long, the console crops it (scroll right to see it):

scala> Array.fill[Byte](5)(0)
res1: Array[Byte] = Array(0, 0, 0, 0, 0)

scala> Array.fill[Byte](500)(0)
res2: Array[Byte] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...

scala> "a"*5000
res3: String = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

How can I print the same or equivalent output, for any given object (not just a collection or array) without the cropping occurring?

like image 944
Sampo Avatar asked Sep 24 '14 07:09

Sampo


People also ask

How do you print in Scala?

Method # 1: Using the “println” Command The “println” command in the Scala programming language is used to print a line while introducing a new line at the end. In this way, if you want to print more than one line, each of them will be printed on a separate line.

What is console in Scala?

Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala. io.

How do you input to Scala?

Using a thread to poll the input-readLine: // keystop1.sc // In Scala- or SBT console/Quick-REPL: :load keystop1.sc // As Script: scala -savecompiled keystop1.sc @volatile var isRunning = true @volatile var isPause = false val tInput: Thread = new Thread { override def run: Unit = { var status = "" while (isRunning) { ...

What is println in Scala?

Println. In Scala we often have console programs that perform computations. The programs must write data to the console—or their results will remain unknown. Method notes. With print, println and printf we report to the screen. And with methods from scala.io.StdIn we read data from the console. We build interactive programs. Println example.

What is the use of console in Scala?

Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala.io.StdIn. It is even helpful in constructing interactive programs.

How do I read a line in Scala console?

Scala Console: println, printf and readLine Use console functions like println and printf. Read lines from the console. Println. In Scala we often have console programs that perform computations. The programs must write data to the console—or their results will remain unknown. Method notes. With print, println and printf we report to the screen.

How do I start the Scala REPL?

To start the Scala REPL, type scala at your operating system command line: Welcome to Scala version 2.10.0 Type in expressions to have them evaluated. Type :help for more information. scala> _ Welcome, you’re now using the Scala REPL. Inside the REPL environment, you can try all sorts of different experiments and expressions:


3 Answers

The result is not "cropped", simply println is invoking java.lang.Arrays.toString() (since scala.Array is a Java array).

Specifically, Arrays defines a toString overload that works with Object, which calls the toString implementation of java.lang.Object on every element. Such implementation prints the reference of the object, so you end up with

[Lscala.Tuple2;@4de71ca9

which is an Array containing the reference 4de71ca9 to a scala.Tuple2 object.

That has been discussed in this ticket years ago.


In the specific case of arrays, you can simply do

println(x.mkString("\n"))

or

x foreach println

or

println(x.deep)

Update

To answer your last edit, you can set the maximum lenght of the strings printed by the REPL

scala> :power
** Power User mode enabled - BEEP WHIR GYVE **
** :phase has been set to 'typer'.          **
** scala.tools.nsc._ has been imported      **
** global._, definitions._ also imported    **
** Try  :help, :vals, power.<tab>           **

scala> vals.isettings.maxPrintString = Int.MaxValue
vals.isettings.maxPrintString: Int = 2147483647
like image 192
Gabriele Petronella Avatar answered Oct 16 '22 15:10

Gabriele Petronella


try

x map println

or

x foreach println
like image 21
Govind Singh Avatar answered Oct 16 '22 14:10

Govind Singh


try this

scala> :power
Power mode enabled. :phase is at typer.
import scala.tools.nsc._, intp.global._, definitions._
Try :help or completions for vals._ and power._

scala> vals.isettings.maxPrintString
res9: Int = 800

scala> vals.isettings.maxPrintString = 10000
vals.isettings.maxPrintString: Int = 10000
like image 3
Pengfei.X Avatar answered Oct 16 '22 16:10

Pengfei.X