Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enter a multi-line command in the Scala REPL?

I would like to enter something like the following match instruction, but formatted across multiple lines. Is that possible in the Scala REPL?

myString match { case patt(a) => true case _ => false }
like image 257
vromancas Avatar asked Feb 18 '15 20:02

vromancas


3 Answers

One way to enter into multi-line mode in the Scala REPL is to hit enter right after the opening curly brace "{", then hit enter after each line until the last closing curly brace has been entered "}". Hitting enter after it will exit the multi-line mode

myScript match { <enter> //enter multi-line mode
  | case scriptStart(a) => true <enter>
  | case _ => false <enter>
  |} <enter> //exit multi-line mode
like image 113
vromancas Avatar answered Nov 15 '22 19:11

vromancas


When cascading transformations, it is as simple as ending each line with a dot. For example:

val wordcount = sc.
  textFile("MYFILE.txt").
  flatMap( x => x.split(" ") ).
  map( w => (w,1) ).
  reduceByKey( (a,b) => a+b )
like image 44
Jorge M. Londoño P. Avatar answered Nov 15 '22 20:11

Jorge M. Londoño P.


If you're just typing it in as-is, the REPL should detect the opening brace when you return, so that it won't try to parse and execute the code until it finds the closing brace.

You could also use paste mode by typing :pa or :paste. This will allow you to enter as much as you want in any format (two blank lines will automatically quit from it). Then when finished entering in code, you can press Ctrl + D to evaluate.

like image 22
Michael Zajac Avatar answered Nov 15 '22 19:11

Michael Zajac