Currently, I am learning Scala and reading this book Programming in Scala and which says, " Unlike an array or list, a tuple can hold objects with different types." For example, the following tuple contain Int, String and Float.
val tup = (1,…
why the methods transform (in-place mutation version of map) and retain (in-place mutation version of filter) are defined on only mutable.Map but not on mutable.Buffer and mutable.Set? shouldnt all mutable collections support these methods?
What is the most succinct Scala way to reverse a Map? The Map may contain non-unique values.
EDIT:
The reversal of Map[A, B] should give Map[B, Set[A]] (or a MultiMap, that would be even better).
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…
Given a collection in Scala, I'd like to traverse this collection and for each object I'd like to emit (yield) from 0 to multiple elements that should be joined together into a new collection.
For example, I expect something like this:
val input =…
Suppose I want to add functionality like map to a Scala List, something along the lines of list mapmap f, which applies the function f to each element of list twice. (A more serious example might be implementing a parallel or distributed map, but I…
When I input Seq(1,2,3) in REPL, it returns me List(1,2,3)
scala> Seq(1,2,3)
res8: Seq[Int] = List(1, 2, 3)
Therefore, I thought the List(1,2,3) may be of type List[Int]. And I tried to specify the type for the variable who are assigned to…
I want to filter a List, and I only want to keep a string if the string contains .jpg,.jpeg or .png:
scala> var list = List[String]("a1.png","a2.amr","a3.png","a4.jpg","a5.jpeg","a6.mp4","a7.amr","a9.mov","a10.wmv")
list: List[String] =…
Here is an example:
scala> val xs = List(1,2,3).toIterator.toSeq
xs: Seq[Int] = Stream(1, ?)
A sequence is a materialized collection (it's a List by default), so I expected that toSeq would return a List, not a Stream
The implementation is in…
In Scala, it's possible to check if an item is a member of a Set using "Contains":
object Main extends App {
val the_set = Set(1, 2, 3, 4)
if( the_set contains 3 ) println("The set contains 3!")
}
However, I'd like to do a similar…
I need to check if a Traversable (which I already know to be nonEmpty) has a single element or more.
I could use size, but (tell me if I'm wrong) I suspect that this could be O(n), and traverse the collection to compute it.
I could check if…
I have an array, something like that:
val a = Array("a", "c", "c", "z", "c", "b", "a")
and I want to get a map with keys of all different values of this array and values with a collection of relevant indexes for each such group, i.e. for a given…
The following code results in a StackOverflowError on the last line.
object StackTest extends App{
@tailrec def incrementValues(acc: Map[String, Int], inc: Int): Map[String, Int] = {
if(inc == 0) acc
else…
For any given collection of Map, for instance,
val in = Array( Map("a" -> 1, "b" -> 2),
Map("a" -> 11, "c" -> 4),
Map("b" -> 7, "c" -> 10))
how to use aggregate on in.par so as to merge the maps into
Map ( "a" ->…
How to do pattern matching on a Map in Scala ?
A (non working) attempt includes,
Map("a"->1, "b"->2, "c"->3) match {
case Map(a,b,_*) => a
}
which errs with
value Map is not a case class, nor does it have an unapply/unapplySeq member
…