How do you instantiate a scala.util.parsing.input.Reader to read from a file? The API mentions in passing something about PagedSeq and java.io.Reader, but it's not clear at all how to accomplish that.
You create a FileInputStream, pass that to an InputStreamReader and pass that to the apply method of the StreamReader companion object, which returns a StreamReader, a subtype of Reader.
scala> import scala.util.parsing.input.{StreamReader,Reader}
import scala.util.parsing.input.{StreamReader, Reader}
scala> import java.io._
import java.io._
scala> StreamReader(new InputStreamReader(new FileInputStream("test")))
res0: scala.util.parsing.input.StreamReader = scala.util.parsing.input.StreamReader#1e5a0cb
Related
I have the following list from my configuration:
val markets = Configuration.getStringList("markets");
To create a sequence out of it I write this code:
JavaConverters.asScalaIteratorConverter(markets.iterator()).asScala.toSeq
I wish I could do it in a less verbose way, such as:
markets.toSeq
And then from that list I get the sequence. I will have more configuration in the near future; is there a solution that provides this kind of simplicity?
I want a sequence regardless of the configuration library I am using. I don't want to have the stated verbose solution with the JavaConverters.
JavaConversions is deprecated since Scala 2.12.0. Use JavaConverters; you can import scala.collection.JavaConverters._ to make it less verbose:
import scala.collection.JavaConverters._
val javaList = java.util.Arrays.asList("one", "two")
val scalaSeq = javaList.asScala.toSeq
Yes. Just import implicit conversions:
import java.util
import scala.collection.JavaConversions._
val jlist = new util.ArrayList[String]()
jlist.toSeq
Why does not my Scala program recognize java.io.File?
scala> val in = new java.util.Scanner(java.io.File("~/test.py"))
<console>:13: error: object java.io.File is not a value
val in = new java.util.Scanner(java.io.File("~/test.py"))
^
When you write code like this java.io.File("~/test.py"), it means that you're calling apply method of java.io.File class. But File API doesn't provide such method.
If you want to create a new File instance, you should use constuctor:
new java.io.File("~/test.py")
I am trying to execute something like this
scala> import scala.sys.process._
scala> Process("cat temp.txt")!
I will be doing this say in a Play Framework REST handler. I want this to return a future object so that I can map/flatMap on it and do further processing when the shell is done executing. How do I do that?
I think all you need is this.
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
import scala.sys.process._
val fs = Future("cat temp.txt".!!) // Future[String] = Future(<not completed>)
The file contents becomes one long string but you can split() it in the map() operation.
If I import scala.collection._ and create a queue:
import scala.collection._
var queue = new Queue[Component]();
I get the following error:
error: not found: type Queue
However, if I also add
import scala.collection.mutable.Queue
The error will disappear.
Why is this happening?
Shouldn't scala.collection._ contain scala.collection.mutable.Queue?
You have to know how the Scala collections library is structured. It splits collections based on whether they are mutable or immutable.
Queue lives in the scala.collection.mutable package and scala.collection.immutable package. You have to specify which one you want e.g.
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> var q = new Queue[Int]()
q: scala.collection.mutable.Queue[Int] = Queue()
scala> import scala.collection.immutable._
import scala.collection.immutable._
scala> var q = Queue[Int]()
q: scala.collection.immutable.Queue[Int] = Queue()
After import scala.collection._ you can use mutable.Queue; you could write Queue if there was a scala.collection.Queue (or one of scala.Queue, java.lang.Queue, and scala.Predef.Queue, since all of their members are imported by default), but there isn't.
It should be easy to see why it works this way: otherwise, the compiler (or anyone reading your code) would have no idea where they should look for the type: do you want scala.collection.Queue, scala.collection.mutable.Queue, or scala.collection.some.subpackage.from.a.library.Queue?
syntax.IdOps seems to have no "companion" object to import its implicits (see, selfless pattern), so it's hard to use that in REPL for example:
scala> val selfish = new scalaz.syntax.ToIdOps{} //I don't want to do this, it feels wrong
selfish: scalaz.syntax.ToIdOps = $anon$1#1adfe356
scala> import selfish._
import selfish._
Is there a way to import it?
https://github.com/scalaz/scalaz/blob/v7.1.2/core/src/main/scala/scalaz/syntax/Syntax.scala#L117
You can use scalaz.syntax.id instead of new scalaz.syntax.ToIdOps{}
import scalaz.syntax.id._