简体   繁体   中英

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 }

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.

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM