scala-collections

Generics invariant covariant contravariant in scala

巧了我就是萌 提交于 2020-02-20 08:47:12
问题 This could be a very silly question, but I am not able to understand the difference even after scratching my head for a long time. I am going through the page of scala generics: https://docs.scala-lang.org/tour/generic-classes.html Here, it is said that Note: subtyping of generic types is invariant . This means that if we have a stack of characters of type Stack[Char] then it cannot be used as an integer stack of type Stack[Int]. This would be unsound because it would enable us to enter true

Performance of scala parallel collection processing

元气小坏坏 提交于 2020-02-01 04:37:46
问题 I have scenarios where I will need to process thousands of records at a time. Sometime, it might be in hundreds, may be upto 30000 records. I was thinking of using the scala's parallel collection. So just to understand the difference, I wrote a simple pgm like below: object Test extends App{ val list = (1 to 100000).toList Util.seqMap(list) Util.parMap(list) } object Util{ def seqMap(list:List[Int]) = { val start = System.currentTimeMillis list.map(x => x + 1).toList.sum val end = System

Different performance of object with same runtime class but different static type

Deadly 提交于 2020-01-31 18:01:39
问题 Consider the following jmh benchmark @State(Scope.Benchmark) @BenchmarkMode(Array(Mode.Throughput)) class So59893913 { def seq(xs: Seq[Int]) = xs.sum def range(xs: Range) = xs.sum val xs = 1 until 100000000 @Benchmark def _seq = seq(xs) @Benchmark def _range = range(xs) } Given xs references the same object of runtime class Range.Inclusive passed in as argument to seq and range methods, hence dynamic dispatch should invoke the same implementation of sum , despite differing declared static

Different performance of object with same runtime class but different static type

怎甘沉沦 提交于 2020-01-31 18:01:06
问题 Consider the following jmh benchmark @State(Scope.Benchmark) @BenchmarkMode(Array(Mode.Throughput)) class So59893913 { def seq(xs: Seq[Int]) = xs.sum def range(xs: Range) = xs.sum val xs = 1 until 100000000 @Benchmark def _seq = seq(xs) @Benchmark def _range = range(xs) } Given xs references the same object of runtime class Range.Inclusive passed in as argument to seq and range methods, hence dynamic dispatch should invoke the same implementation of sum , despite differing declared static

Hard Time in understanding the Scala code which returns indices of the two numbers

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-30 10:58:08
问题 I am having a hard time understanding m.get and m+(x._1 -> x._2) in below code ,can anyone let me know what does it do object Solution { def twoSum(nums: Array[Int], target: Int): Array[Int] = { nums.zipWithIndex.foldLeft(Map.empty[Int,Int])((m,x)=> { if(m.get(target - x._1)==None) m+(x._1 -> x._2) else return Array(m.getOrElse(target-x._1, -1), x._2) }) null } } This code returns indices of the two numbers such that they add up to a specific target. 回答1: Here are a few different (better?)

Split a row into two and dummy some columns

左心房为你撑大大i 提交于 2020-01-25 00:31:11
问题 I need to split a row and create a new row by changing the date columns and make the amt columns to zero as in the below example: Input: +---+-----------------------+-----------------------+-----+ |KEY|START_DATE |END_DATE |Amt | +---+-----------------------+-----------------------+-----+ |0 |2016-12-14T23:59:59.000|2017-10-29T23:59:58.000|100.0| |0 |2016-12-14T23:59:59.000|2017-10-29T23:59:58.000|200.0| |0 |2017-10-30T00:00:00.000|2017-11-02T23:59:59.000|67.5 |->Split row based on start &

Spark DataFrame not respecting schema and considering everything as String

情到浓时终转凉″ 提交于 2020-01-22 19:56:23
问题 I am facing a problem which I have failed to get over for ages now. I am on Spark 1.4 and Scala 2.10. I cannot upgrade at this moment (big distributed infrastructure) I have a file with few hundred columns, only 2 of which are string and rest all are Long. I want to convert this data into a Label/Features dataframe. I have been able to get it into LibSVM format. I just cannot get it into a Label/Features format. The reason being I am not being able to use the toDF() as shown here https:/

What is the proper way to remove elements from a scala mutable map using a predicate

蹲街弑〆低调 提交于 2020-01-22 13:27:52
问题 How to do that without creating any new collections? Is there something better than this? val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4) m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1)) println(m) P.S. in Scala 2.8 回答1: retain does what you want. In 2.7: val a = collection.mutable.Map(1->"one",2->"two",3->"three") a: scala.collection.mutable.Map[Int,java.lang.String] = Map(2 -> two, 1 -> one, 3 -> three) scala> a.retain((k,v) => v.length < 4) scala

Why is zipped faster than zip in Scala?

 ̄綄美尐妖づ 提交于 2020-01-22 04:40:05
问题 I have written some Scala code to perform an element-wise operation on a collection. Here I defined two methods that perform the same task. One method uses zip and the other uses zipped . def ES (arr :Array[Double], arr1 :Array[Double]) :Array[Double] = arr.zip(arr1).map(x => x._1 + x._2) def ES1(arr :Array[Double], arr1 :Array[Double]) :Array[Double] = (arr,arr1).zipped.map((x,y) => x + y) To compare these two methods in terms of speed, I wrote the following code: def fun (arr : Array[Double

Convert Java Map to Scala Map

不打扰是莪最后的温柔 提交于 2020-01-19 07:05:07
问题 I have a java map: java.util.Map<SomeObject, java.util.Collection<OtherObject>> and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]] I have used mapAsScalaMap but the result is not quite what I want, the result is: Map[SomeObject, java.util.Collection[OtherObject]] . How can I fix it to also convert the collection to a set? NOTE: actually my original problem was to convert google's ArrayListMultimap<SomeObject, OtherObject> to a MultiMap[SomeObject, OtherObject]