Read first X bytes of input stream in Scala - scala

What is the most compact and scala-like way to read the first X bytes of an input stream to a buffer?

Wrap your java.io.InputStream in a scala.io.BufferedSource to get access the usual iterator operations:
val bufSrc = scala.io.Source.fromInputStream(inputStream)
val chars = bufSrc.take(X) // X being the number of bytes
Note that BufferedSource.take gives you an iterable of Char.

How about
Source.fromInputStream(inputStream).reader.read(..)
There are several read methods to accomplish what you require

Related

Reading float values from a csv file in Scala?

I want to send (integer) values from a csv file to a (Chisel) class here.
I just can't read the values from a csv file - I have already tried all code snippets scattered around the internet. (csv file is in the format below ->)
1321437196.0,
-2132416838.0,
1345437196.0
Code I am using:
val bufferedSource = io.Source.fromFile("sourceFile.csv")
val rows = Array.ofDim[Int](3)
var count = 0
for (line <- bufferedSource.getLines) {
rows(count) = line.split(",").map(_.trim).toString.toInt
count += 1
}
bufferedSource.close
println(rows.mkString(" "))
Output:
[Ljava.lang.String;#51f9ef45
[Ljava.lang.String;#2f42c90a
[Ljava.lang.String;#6d9bd75d
I have understood the error message and tried all various snippets mentioned here(Printing array in Scala , Scala - printing arrays) , but I just can't see where I am going wrong here. Just to point out, I don't want a Double value here but want a converted Signed Integer , hence that is why toInt.
Thanks will appreciate help with this!
"1.0".toInt won't work. Need to go from String to Float to Int.
val bufferedSource = io.Source.fromFile("sourceFile.csv")
val rows = bufferedSource.getLines
.map(_.split(",").head.trim.toFloat.toInt)
.toArray
bufferedSource.close
rows //res1: Array[Int] = Array(1321437184, -2132416896, 1345437184)
Change
line.split(",").map(_.trim).toString.toInt
to
line.split(",")(0).trim.toInt
val bufferedSource = io.Source.fromFile("sourceFile.csv")
val rows = bufferedSource.getLines
.map(_.split(",").headOption.mkString.toInt)
bufferedSource.close

apply a function in printf on scala

I receive in the method printfields a vector[String] which I am printing as follows:
def printFields(fields: Vector[String]): Unit =
{
printf(fields.map(_ => "%s").mkString("",",","\n"),fields: _*)
println(fields)
}
now this give me as output the following:
39,39,35,30
Vector(39, 39, 35,30)
28,28,35,30
Vector(28, 28, 35,30)
Now, Each number correspond to an Id, I need to apply a function to each number that appear here in order to print the element that correspond, in other words, make something like:
printf(fields.map(_ => "%s").mkString("",",","\n"),con.convI2N((fields: _*).toInt))
I try with converting the function to an Iterator, but give me Strings like
39
39
35,30
The last String can not be converted toInt, then this is not an option,
Someone can help me?
Thank you so much
What about converting the Vector[String] to a Vector[Int] as preliminary operation?
fields.map(_.split(',')).flatten.map(_.toInt)
This is just an hint, it is not the safer way, you should check that every String in your Vector is actually an Int or a sequence of comma-separated Ints.

How mimic the function map.getORelse to a CSV file

I have a CSV file that represent a map[String,Int], then I am reading the file as follows:
def convI2N (vkey:Int):String={
val in = new Scanner("dictionaryNV.csv")
loop.breakable{
while (in.hasNext) {
val nodekey = in.next(',')
val value = in.next('\n')
if (value == vkey.toString){
n=nodekey
loop.break()}
}}
in.close
n
}
the function give the String given the Int. The problem here is that I must browse the whole file, and the file is to big, then the procedure is too slow. Someone tell me that this is O(n) complexity time, and recomend me to pass to O(log n). I suppose that the function map.getOrElse is O(log n).
Someone can help me to find a way to get a best performance of this code?
As additional comment, the dictionaryNV file is sorted by the Int values
maybe I can divide the file by lines, or set of lines. The CSV has like 167000 Tuples [String,Int]
or in another way how you make some kind of binary search through the csv in scala?
If you are calling confI2N function many times then definitely the job will be slow because each time you have to scan the big file. So if the function is called many times then it is recommended to store them in temporary variable as properties or hashmap or collection of tuple2 and change the other code that is eating the memory.
You can try following way which should be faster than scanner way
Assuming that your csv file is comma separated as
key1,value1
key2,value2
Using Source.fromFile can be your solution as
def convI2N (vkey:Int):String={
var n = "not found"
val filtered = Source.fromFile("<your path to dictionaryNV.csv>")
.getLines()
.map(line => line.split(","))
.filter(sline => sline(0).equalsIgnoreCase(vkey.toString))
for(str <- filtered){
n = str(0)
}
n
}

Scala:Splitting a line and count the number of words

I am new to scala and learning scala...
val pair=("99","ABC",88)
pair.toString().split(",").foreach { x => println(x)}
This gives the splitted line. But How do I count the number of splitted words .
I am trying as below:
pair.toString().split(",").count { x => ??? }
I am not sure how can I get the count of splitted line. ie 3 ..
Any help appreciated....
Tuples are equipped with product functions such as productElement, productPrefix, productArity and productIteratorfor processing its elements.
Note that
pair.productArity
res0: Int = 3
and that
pair.productIterator foreach println
99
ABC
88
pair.toString().split(",").size will give you the number of elements. OTOH, you have a Tuple3, so its size will only ever be three. Asking for a size function on a tuple is rather redundant, their sizes are fixed by their type.
Plus, if any of the elements contain a comma, your function will break.

How to convert a single character into Scala's Char object?

What is the simplest and the most concise method of converting a single character into Scala's Char object?
I have found the following solution, but somehow it seems unsatisfying to me, as I believe it should have been possible to resolve this problem in a more elegant Scala-way without using unnecessary conversions and array-specific operations:
scala> "A".toCharArray.head
res0: Char = A
There are a huge number of ways of doing this. Here are a few:
'A' // Why not just write a char to begin with?
"A"(0) // Think of "A" like an array--this is fast
"A".charAt(0) // This is what you'd do in Java--also fast
"A".head // Think of "A" like a list--a little slower
"A".headOption // Produces Option[Char], useful if the string might be empty
If you use Scala much, the .head version is the cleanest and clearest; no messy stuff with numbers that might get off by one or which have to be thought about. But if you really need to do this a lot, head runs through a generic interface that has to box the char, while .charAt(0) and (0) do not, so the latter are about 3x faster.
You can use scala's charAt.
scala> var a = "this"
a: String = this
scala> a.charAt(0)
res3: Char = t
Additionally, the following is valid and may be what you are looking for:
scala> "a".charAt(0)
res4: Char = a