Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
7
votes
4 answers

Difference between Tuple and List[Any] in Scala?

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,…
Ra Ka
  • 2,995
  • 3
  • 23
  • 31
7
votes
1 answer

scala 2.8 collections inconsistency?

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?
7
votes
3 answers

What is the most succinct Scala way to reverse a Map?

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).
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
7
votes
1 answer

Spark DataFrame not respecting schema and considering everything as String

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…
7
votes
6 answers

Scala: transform a collection, yielding 0..many elements on each iteration

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 =…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
7
votes
2 answers

Can I "pimp my library" with an analogue of TraversableLike.map that has nicely variant types?

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…
Scott Morrison
  • 3,100
  • 24
  • 39
7
votes
1 answer

The difference and conversion between Seq[Int] and List[Int]

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…
7
votes
3 answers

filter a List according to multiple contains

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] =…
Govind Singh
  • 15,282
  • 14
  • 72
  • 106
7
votes
1 answer

Why is TraversableOnce.toSeq returning a Stream?

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…
Adrian
  • 3,762
  • 2
  • 31
  • 40
7
votes
2 answers

Scala equivalent of Python's "in" operator for sets?

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…
bnsmith
  • 1,667
  • 19
  • 30
7
votes
4 answers

Efficient way to check if a traversable has more than 1 element in Scala

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…
Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
7
votes
4 answers

Scala collections: array groupBy and return array indexes for each group

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…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
7
votes
1 answer

Scala Map.mapValues stackoverflowerror

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…
Pengin
  • 4,692
  • 6
  • 36
  • 62
7
votes
1 answer

Merging Maps using `aggregate`

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" ->…
elm
  • 20,117
  • 14
  • 67
  • 113
7
votes
2 answers

Scala Map pattern matching

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 …
elm
  • 20,117
  • 14
  • 67
  • 113