Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
18
votes
1 answer

Why does Option not extend the Iterable trait directly?

Option is implicitly convertible to an Iterable - but why does it not just just implement Iterable directly: def iterator = new Iterator[A] { var end = !isDefined def next() = { val n = if (end) throw new NoSuchElementException() else get …
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
18
votes
3 answers

Scala: "map" vs "foreach" - is there any reason to use "foreach" in practice?

In Scala collections, if one wants to iterate over a collection (without returning results, i.e. doing a side effect on every element of collection), it can be done either with final def foreach(f: (A) ⇒ Unit): Unit or final def map[B](f: (A) ⇒ B):…
GreyCat
  • 16,622
  • 18
  • 74
  • 112
18
votes
2 answers

Scala create List[Int]

How can I quickly create a List[Int] that has 1 to 100 in it? I tried List(0 to 100), but it returns List[Range.Inclusive] Thanks
Lydon Ch
  • 8,637
  • 20
  • 79
  • 132
18
votes
1 answer

Scala: Parallel collection in object initializer causes a program to hang

I've just noticed a disturbing behavior. Let's say I have a standalone program consisting of a sole object: object ParCollectionInInitializerTest { def doSomething { println("Doing something") } for (i <- (1 to 2).par) { println("Inside…
Lukasz Gieron
  • 276
  • 2
  • 12
17
votes
4 answers

Maximum Length for scala queue

I'm curious if Scala has some gem hidden in its collection classes that I can use. Basically I'm looking for something like a FIFO queue, but that has an upper-limit on its size such that when the limit is hit, the oldest (first) element is removed…
John S
  • 1,695
  • 2
  • 14
  • 25
17
votes
1 answer

Parallelize Scala's Iterator

Please note: This is not a duplicate question, since this question specifies on all methods Iterator has, not just map and flatMap. Therefore Future.traverse are not a good answer. Let's say I have this simple statement: (1 to…
Rok Kralj
  • 46,826
  • 10
  • 71
  • 80
17
votes
2 answers

How to use priority queues in Scala?

I am trying to implement A* search in Scala (version 2.10), but I've ran into a brick wall - I can't figure out how to use Scala's Priority Queue. I have a set of squares, represented by (Int, Int)s, and I need to insert them with priorities…
Antimony
  • 37,781
  • 10
  • 100
  • 107
17
votes
5 answers

How to convert a nested scala collection to a nested Java collection

I'm having compilation issues between Scala and Java. My Java code needs a java.util.Map> My scala code has a Map[Double, Vector[Foo]] I get the compilation error: error: type mismatch; found :…
Adam K
  • 233
  • 2
  • 7
16
votes
2 answers

Why does Iterator have a contains method but Iterable does not, in Scala 2.8?

I would like to call 'contains' on my Iterables :-)
adam77
  • 2,797
  • 3
  • 21
  • 29
16
votes
4 answers

Parallel iterator in Scala

Is it somehow possible, using Scala's parallel collections to parallelize an Iterator without evaluating it completely beforehand? Here I am talking about parallelizing the functional transformations on an Iterator, namely map and flatMap. I think…
ziggystar
  • 28,410
  • 9
  • 72
  • 124
15
votes
3 answers

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

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

How to make method return the same generic as the input?

I want to split a string delimited by commas and use the result as either a Seq or a Set: def splitByComma(commaDelimited: String): Array[String] = commaDelimited.trim.split(',') def splitByCommaAsSet(commaDelimited: String): Set[String] =…
15
votes
7 answers

Scala: How to create a Map[K,V] from a Set[K] and a function from K to V?

What is the best way to create a Map[K,V] from a Set[K] and function from K to V? For example, suppose I have scala> val s = Set(2, 3, 5) s: scala.collection.immutable.Set[Int] = Set(2, 3, 5) and scala> def func(i: Int) = "" + i + i func: (i:…
aioobe
  • 413,195
  • 112
  • 811
  • 826
15
votes
4 answers

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

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.…
Oleg Galako
  • 1,036
  • 1
  • 10
  • 16
15
votes
3 answers

Check if a key exists in play.api.libs.json.Json

contains like functionality for play.api.libs.json.Json val data=Map("id" -> "240190", "password" -> "password","email" -> "email@domain.com") data.contains("email")//true val info=Json.obj("id" -> "240190", "password" -> "password","email" ->…
Govind Singh
  • 15,282
  • 14
  • 72
  • 106