Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
8
votes
1 answer

how scala folding works?

I am trying to understand the following code but can't. it is supposed to create a child actor for an Event if it does not exist, otherwise says that the Event exist as it as an associated child actor. context.child(name).fold(create())(_ =>…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
8
votes
2 answers

Removing an element from MutableList in Scala

I have a MutableList and I want to remove an element from it but I cannot find the appropriate method. There is a method to remove element from ListBuffer like this: val x = ListBuffer(1, 2, 3, 4, 5, 6, 7, 8, 9) x -= 5 I am unable to find an…
M.Ahsen Taqi
  • 965
  • 11
  • 35
8
votes
1 answer

Inheritance and type parameters of Traversable

I'm studying the source code of the Scala 2.8 collection classes. I have questions about the hierarchy of scala.collection.Traversable. Look at the following declarations: package scala.collection trait Traversable[+A] extends…
Jesper
  • 202,709
  • 46
  • 318
  • 350
8
votes
4 answers

Is it possible to remove elements from PriorityQueue?

Is it possible to remove elements from PriorityQueue? Documentation: http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.PriorityQueue http://www.scala-lang.org/api/current/index.html#scala.collection.Iterator I have a PQ w…
Adrian
  • 5,603
  • 8
  • 53
  • 85
8
votes
3 answers

short way to breakOut to specific collection type?

scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> m.map{case (a, b) => (a+ 1, a+2, a+3)} res42: scala.collection.immutable.Iterable[(Int, Int, Int)] = List((2,3,4)) What I want is for the result type to be…
IttayD
  • 28,271
  • 28
  • 124
  • 178
8
votes
1 answer

Difference between scan and scanLeft in Scala

Which are the differences between scan and scanLeft ? For instance, (1 to 3).scan(10)(_-_) res: Vector(10, 9, 7, 4) (1 to 3).scanLeft(10)(_-_) res: Vector(10, 9, 7, 4) deliver the same result, clearly in contrast to (1 to…
elm
  • 20,117
  • 14
  • 67
  • 113
8
votes
1 answer

How do I convert a java.util.Map to scala.collection.immutable.Map in Java?

I find lots of people trying to do this, and asking about this but the question is always answered in terms of scala code. I need to call an API that is expecting a scala.collection.immutable.Map but I have a java.util.Map, how can I cleanly convert…
NSA
  • 5,689
  • 8
  • 37
  • 48
8
votes
6 answers

Scala filter on a list by index

I wanted to write it functionally, and the best I could do was: list.zipWithIndex.filter((tt:Tuple2[Thing,Int])=>(tt._2%3==0)).unzip._1 to get elements 0, 3, 6,... Is there a more readable Scala idiom for this?
user833970
  • 2,729
  • 3
  • 26
  • 41
8
votes
3 answers

When should I use Scala's Array instead of one of the other collections?

This is more a question of style and preference but here goes: when should I use scala.Array? I use List all the time and occasionally run into Seq, Map and the like, but I've never used nor seen Array in the wild. Is it just there for Java…
pr1001
  • 21,727
  • 17
  • 79
  • 125
8
votes
2 answers

Idiomatic Scala: Semantic difference between Like and Types?

As title of this question suggests: my question is more about form (idiomatic convention) than function. Put Succinctly: What is the semantic difference between MyCollectionLike and MyCollection? As examples: What is the difference between…
Ryan Delucchi
  • 7,718
  • 13
  • 48
  • 60
8
votes
2 answers

Scala's TreeSet vs Java's TreeSet - poll?

If I want to remove the highest entry in log(n) time in Java's TreeSet, I use treeSet.pollFirst() - what is the equivalent for Scala's mutable.TreeSet class? Anyway, what I really want is a heap-like priority queue data structure that lets me…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
8
votes
1 answer

Constructing Scala parallel views with X.par.view vs X.view.par?

According to the paper on parallel collections and searching on the internet, parallel collections are supposed to work with views, but I am not clear on the difference…
8
votes
2 answers

Traversable => Java Iterator

I have a Traversable, and I want to make it into a Java Iterator. My problem is that I want everything to be lazily done. If I do .toIterator on the traversable, it eagerly produces the result, copies it into a List, and returns an iterator over the…
Andres
  • 1,454
  • 11
  • 12
8
votes
3 answers

Why is a Range transformed to a Vector after map operation?

Following Scala courses on Coursera, Martin Odersky showed an example code which is: 1 to 5 map ( i => i*i ) And he said the Range gets transformed to a Vector because they share the same interface (IndexedSeq) and the result could not be…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
8
votes
2 answers

Scala best way of turning a Collection into a Map-by-key? (2nd variant)

(This is a variant to this Q&A) Say I have this: List( "foo", "bar", "spam" ) I want to create a Map for which the key is the length of the String and the value is a Collection of all the Strings that have that length. In other words, given the…
Sean Crotty