scala-collections

scala.collection.breakOut vs views

谁说我不能喝 提交于 2020-01-01 07:58:49
问题 This SO answer describes how scala.collection.breakOut can be used to prevent creating wasteful intermediate collections. For example, here we create an intermediate Seq[(String,String)] : val m = List("A", "B", "C").map(x => x -> x).toMap By using breakOut we can prevent the creation of this intermediate Seq : val m: Map[String,String] = List("A", "B", "C").map(x => x -> x)(breakOut) Views solve the same problem and in addition access elements lazily: val m = (List("A", "B", "C").view map (x

Scala String Equality Question from Programming Interview

我怕爱的太早我们不能终老 提交于 2020-01-01 04:02:10
问题 Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows: You have two strings consisting of alphabetic characters as well as a special character representing the backspace symbol. Let's call this backspace character '/'. When you get to the keyboard, you type this sequence of characters, including the backspace/delete character. The solution you are to

How to implement generic average function in scala?

家住魔仙堡 提交于 2019-12-31 19:23:44
问题 It seems easy problem for any specific kind of Number i.e. Double/Integer but it is hard to write in general case. implicit def iterebleWithAvg(data:Iterable[Double]) = new { def avg:Double = data.sum / data.size } How to implement this for any kind of Number(Int,Float,Double,BigDecemial)? 回答1: You have to pass an implicit Numeric which will allow summation and conversion to Double: def average[T]( ts: Iterable[T] )( implicit num: Numeric[T] ) = { num.toDouble( ts.sum ) / ts.size } The

How to implement generic average function in scala?

一世执手 提交于 2019-12-31 19:23:11
问题 It seems easy problem for any specific kind of Number i.e. Double/Integer but it is hard to write in general case. implicit def iterebleWithAvg(data:Iterable[Double]) = new { def avg:Double = data.sum / data.size } How to implement this for any kind of Number(Int,Float,Double,BigDecemial)? 回答1: You have to pass an implicit Numeric which will allow summation and conversion to Double: def average[T]( ts: Iterable[T] )( implicit num: Numeric[T] ) = { num.toDouble( ts.sum ) / ts.size } The

How to implement generic average function in scala?

流过昼夜 提交于 2019-12-31 19:22:23
问题 It seems easy problem for any specific kind of Number i.e. Double/Integer but it is hard to write in general case. implicit def iterebleWithAvg(data:Iterable[Double]) = new { def avg:Double = data.sum / data.size } How to implement this for any kind of Number(Int,Float,Double,BigDecemial)? 回答1: You have to pass an implicit Numeric which will allow summation and conversion to Double: def average[T]( ts: Iterable[T] )( implicit num: Numeric[T] ) = { num.toDouble( ts.sum ) / ts.size } The

Why is ClassManifest needed with Array but not List?

允我心安 提交于 2019-12-30 01:09:05
问题 Define the following code: import scala.collection.JavaConversions._ val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator def func(a:Any):String = a.toString def test[T:ClassManifest](iter:java.util.Iterator[Any], func:Any=>T):Array[T] = iter.map(i=>func(i)).toArray def testFunc = test(iter, func) Here, I need to use ClassManifest for it to compile correctly, otherwise I get the error: scala> def test[T](iter:java.util.Iterator[Any], func:Any=>T):Array[T] = | iter.map(i=>func(i))

What is the correct way to get a subarray in Scala?

ε祈祈猫儿з 提交于 2019-12-29 11:33:56
问题 I am trying to get a subarray in scala, and I am a little confused on what the proper way of doing it is. What I would like the most would be something like how you can do it in python: x = [3, 2, 1] x[0:2] but I am fairly certain you cannot do this. The most obvious way to do it would be to use the Java Arrays util library. import java.util.Arrays val start = Array(1, 2, 3) Arrays.copyOfRange(start, 0, 2) But it always makes me feel a little dirty to use Java libraries in Scala. The most

Inconsistent behaviour for xs.sliding(n) if n is less than size?

老子叫甜甜 提交于 2019-12-29 08:27:16
问题 According to scaladoc, sliding() returns... "An iterator producing iterable collections of size size , except the last and the only element will be truncated if there are fewer elements than size ." For me, intuitivelly, sliding(n) would return a sliding window of n elements if available . With the current implementation, I need to perform an extra check to make sure I don't get a list of 1 or 2 elements. scala> val xs = List(1, 2) xs: List[Int] = List(1, 2) scala> xs.sliding(3).toList res2:

How to find duplicates in a list?

烈酒焚心 提交于 2019-12-28 13:41:28
问题 I have a list of unsorted integers and I want to find those elements which have duplicates. val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102) I can find the distinct elements of the set with dup.distinct, so I wrote my answer as follows. val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102) val distinct = dup.distinct val elementsWithCounts = distinct.map( (a:Int) => (a, dup.count( (b:Int) => a == b )) ) val duplicatesRemoved = elementsWithCounts.filter( (pair: Pair[Int,Int]) => { pair._2 <= 1 }

How to read files from resources folder in Scala?

空扰寡人 提交于 2019-12-28 03:17:12
问题 I have a folder structure like below: - main -- java -- resources -- scalaresources --- commandFiles and in that folders I have my files that I have to read. Here is the code: def readData(runtype: String, snmphost: String, comstring: String, specificType: String): Unit = { val realOrInvFile = "/commandFiles/snmpcmds." +runtype.trim // these files are under commandFiles folder, which I have to read. try { if (specificType.equalsIgnoreCase("Cisco")) { val specificDeviceFile: String = "