FAQ Database Discussion Community


Can I do a lazy take with a long parameter?

scala,scala-collections
Stream.take takes an int as a parameter. I want to define a take that takes a long instead- is this possible without using takeWhile to manage a counter?

Performance of scala parallel collection processing

scala,parallel-processing,scala-collections
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...

Scala large listing of cases in pattern matching

scala,pattern-matching,scala-collections
For a long listing of cases which return a value within a limited domain, how to reduce the otherwise growing number of case declarations ? For instance consider "abc" match { case "a" => 1 case "ab" => 1 case "aw" => 2 case "hs" => 2 case "abc" =>...

Pattern Matching Array of Bytes

scala,pattern-matching,scala-collections
This fails to compile: Array[Byte](...) match { case Array(0xFE.toByte, 0xFF.toByte, tail @ _* ) => tail } I can, however, compile this: val FE = 0xFE.toByte val FF = 0xFF.toByte bytes match { Array( FE, FF, tail @ _* ) => tail } Why does the first case fail to...

Is it a good practice to use Set instead of Seq every times a sequence without duplicates is needed?

scala,scala-collections
I discovered Set few days ago and I'm know using it every times I need a sequence without duplicates, even if I'm sure there won't be any. Because of this, I often need to use the toSet method in my code. Thus, I now wondering if it is a good...

Scala string replacement of entire words that comply with a pattern

string,scala,scala-collections,scala-string
In a string, how to replace words that start with a given pattern ? For instance replace each word that starts with "th", with "123", val in = "this is the example, that we think of" val out = "123 is 123 example, 123 we 123 of" Namely how to...

Cannot construct a collection of type …Inclusive[Long] with elements of type Long based on a collection of type …Inclusive[Long]

scala,scala-collections,typechecking
I'm not sure I understand why the following happens. Compiles and works: With Ints without converting to a List import scala.util.Random val xs = 1 to 10 Random.shuffle(xs) With Longs after converting to a List import scala.util.Random val xs = 1L to 10L Random.shuffle(xs.toList) //<-- I had to materialize it...

Scala Map: is it possible to update a value of Int with a var of Int?

scala,scala-collections
This feels like a dumb question, but here goes: Consider the following to update a value of Int in a Map with a var of Int var score: Int = _ val data = Map(( ("things", "stuff") -> 0), (("uwot", "stuff") -> 0), (("isee", "stuff") -> 0)) data.map(element => {...

Why does this compile under Java 7 but not under Java 8?

java,scala,compiler-errors,java-8,scala-collections
This seems to compile fine with Java 7, and any version of the Scala libraries: public static void main(String[] args) { scala.collection.immutable.Set<String> set = new scala.collection.immutable.HashSet<String>(); Iterator<String> iterator = set.iterator(); } It also compiles fine with Java 8 and Scala 2.11.5+. But with Java 8 and Scala 2.11.4, Eclipse complains:...

Compare each element of Scala list against rest of elements in the list

list,scala,foreach,scala-collections
I have a list of Custom objects which will be compared against rest of the objects in the same list. My current approach: Currently i take one element from a the list using foreach loop,run another foreach loop to look through the list for elements that are similar to the...

Scala : simulate Lotto number generator in Range 1 to 45

scala,scala-collections
The following is an imperative solution to a simulation of a Number Lottery from the Range 1 to 45, each time we generate a number n1 the number is removed from the set of possible numbers. Is it possible to achieve the same in a more functional way ? i.e...

Trying to declare an array of a certain size in Scala

arrays,scala,scala-collections
var arr = Array[Int](arr_size) println(arr_size + " " + arr.size) arr_size is 30 but arr.size is 1? Why is this? I am trying to declare an empty array that I can fill in later at designated indexes....

Scala SortedMap - iteratorFrom & co missing counterpart?

scala,iterator,scala-collections,sortedmap
Retrieve entries belonging to greater keys I recently stumbled upon a SO question asking how to retrieve keys greater than a given key in a SortedMap. AfaIk, these SortedMap methods are of note: from(from: A): SortedMap[A, B] Creates a ranged projection of this collection with no upper-bound iteratorFrom(start: A): Iterator[(A,...

scala variable arguments :_*

scala,scala-collections
could someone bring more shed on following piece of scala code which is not fully clear to me? I have following function defined def ids(ids: String*) = { _builder.ids(ids: _*) this } Then I am trying to pass variable argument list to this function as follows: def searchIds(kind: KindOfThing, adIds:String*)...

Scala List method `::` vs `+:`

scala,scala-collections
Can't see the real difference between :: and +: method of Scala List. I used REPL to test and can't see diff from the result. Anyone can give me how can I use that properly?...

Pass additional arguments to the function passed in map()

scala,scala-collections
I want to pass a function to map() which need to use additional data (for example data passed as arguments to the scala program). For example: I have a list: val myList = List(1,2,3,4,5) and a multiplier value is passed as command line argument. val multiplier = args(0) // In...

Spark RDD equivalent to Scala collections partition

scala,apache-spark,scala-collections
This is a minor issue with one of my spark jobs which doesn't seem to cause any issues -- yet annoys me every time I see it and fail to come up with a better solution. Say I have a Scala collection like this: val myStuff = List(Try(2/2), Try(2/0)) I...

Subtracting elements at specified indices in array

arrays,scala,scala-collections
I am beginner to functional programming and Scala. I have an Array of arrays which contain Double numerals. I want to subtract elements (basically two arrays, see example) and I am unable to find online how to do this. For example, consider val instance = Array(Array(2.1, 3.4, 5.6), Array(4.4, 7.8,...

Omit input data of map function in Scala

scala,apache-spark,scala-collections,scala-2.10
I am learning Spark source code, and get confused on the following code: /** * Return a new RDD containing the distinct elements in this RDD. */ def distinct(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] = map(x => (x, null)).reduceByKey((x, y) => x, numPartitions).map(_._1) What is the input data for...

scala Spark get the top words in each row of Array

scala,apache-spark,scala-collections,rdd
I'm unable to get the top word in Array of int and Strings . See the below Array and required output: Consider n is an RDD and suggest me the Required functions for the getting output . scala> n.take(10) res3: Array[(Int, String)] = Array((4,Hi how are you ,how), (2,hello good...

Scala: Convert a csv string to Array

arrays,string,scala,split,scala-collections
I tried to convert a scala string to array by splitting it by ,.For example: var string = "a,b,c,d," array = string.split(",") arr: Array[String] = Array(a, b, c, d) But the output I am getting is Array(a, b, c, d), instead of Array(a, b, c, d, ). The last comma...

Passing a function foreach key of an Array

scala,apache-spark,scala-collections,spark-graphx
I have an array like that : val pairs: Array[(Int, ((VertexId, Seq[Int]), Int))] which generates this output : (11,((11,ArraySeq(2, 5, 4, 5)),1)) (11,((12,ArraySeq(7, 7, 8, 2)),1)) (11,((13,ArraySeq(5, 9, 8, 7)),1)) (1,((1,ArraySeq(1, 2, 3, 4)),1)) (1,((4,ArraySeq(1, 5, 1, 1)),1)) I want to build a Graph for each pairs._1. That means for...