Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
9
votes
4 answers

How to access and update a value in a mutable map of map of maps

I've a three-level data structure (indentation and line breaks for readability): scala> import scala.collection.mutable.Map import scala.collection.mutable.Map scala> val m = Map("normal" -> Map("home" -> Map("wins" -> 0, "scores" -> 0), …
user272735
  • 10,473
  • 9
  • 65
  • 96
9
votes
2 answers

Turn a side-effecting function returning Option into an Iterator

Assume we have a side-effecting "producer" function f: () => Option[T] which returns a Some when repeatedly called until a future point at which it will forever return None. (e.g. a wrapped Java API producing null at EOF might well have this sort of…
satyagraha
  • 623
  • 7
  • 11
9
votes
1 answer

When is it appropriate to use a TrieMap?

I have been reading some posts and was wondering if someone can present a situation on when a TrieMap would be preferable to using a HashMap. So essentially what architecture decision should motivate the use of a TrieMap?
toidiu
  • 965
  • 2
  • 12
  • 25
9
votes
3 answers

Insert character in Scala String

For any given String, for instance val s = "abde" how to insert a character c: Char at position 2, after b ? Update Which Scala collection to consider for multiple efficient insertions and deletions at random positions ? (Assuming that a String…
elm
  • 20,117
  • 14
  • 67
  • 113
9
votes
3 answers

Equality relations in Scala

I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
9
votes
1 answer

How to implement a generic algorithm for any Traversable in Scala?

I'm implementing a generic algorithm to return a collection based on two other collections. The problem can be simplified to def add[Repr <: Traversable[_]](coll1: Repr, coll2: Repr) = coll1 ++ coll2 The problem occurred when I applied the…
9
votes
1 answer

A bug of mutable.Set.foreach in scala?

I'm using scala 2.9.1, when I try this code: import scala.collection.mutable val a = mutable.Set(1,2,3,4,7,0,98,9,8) a.foreach(x => { println(x); a.remove(x) }) the result was something like 0 98 2 1 4 3 8 which did not list all the elements of a.…
user1923692
  • 111
  • 3
9
votes
3 answers

Count occurrences of each element in a List[List[T]] in Scala

Suppose you have val docs = List(List("one", "two"), List("two", "three")) where e.g. List("one", "two") represents a document containing terms "one" and "two", and you want to build a map with the document frequency for every term, i.e. in this…
Mirko N.
  • 10,537
  • 6
  • 38
  • 37
9
votes
5 answers

Tree collections in Scala

I want to implement a tree in Scala. My particular tree uses Swing Split panes to give multiple views of a geographical map. Any pane within a split pane can itself be further divided to give an extra view. Am I right in saying that neither TreeMap…
Rich Oliver
  • 6,001
  • 4
  • 34
  • 57
9
votes
3 answers

Java <-> Scala Collection conversions, Scala 2.10

Looking at the code in JavaConversions and JavaConverters, I am unsure which the "correct" way (with 2.10) to convert between Java and Scala collections (in either direction) is. There seem to be lots of @deprecated annotations. Has a definitive…
John Smith
  • 3,037
  • 5
  • 29
  • 31
8
votes
1 answer

Argument type of anonymous function

I'm having some trouble with this code. It's supposed to be an OperationTree with Elements BinaryOperations and UnaryOperations. The method eval does the evaluation and looks up the variables in a map. Here's the code 1 import…
xyz
  • 83
  • 1
  • 4
8
votes
3 answers

What is the difference between List.view and LazyList?

I am new to Scala and I just learned that LazyList was created to replace Stream, and at the same time they added the .view methods to all collections. So, I am wondering why was LazyList added to Scala collections library, when we can do…
pavel_orekhov
  • 1,657
  • 2
  • 15
  • 37
8
votes
2 answers

Operating on Scala collections in generic way

I wrote the function to find longest common subsequence (LCS). For example, for two sequences of chars BANANA and ATANA it returns AANA. Implementation is naive inefficient adaptation of recursive algorithm, but it is irrelevant for purpose of this…
8
votes
1 answer

How to implement Lazy Chain Pattern in Scala

I am trying to define a set of "LazyChains" that will be used to process incoming messages at a future time. I want the API of the LazyChains to be indistinguishable from the Scala collections APIs (ie: Seq, Stream, etc). This would allow me to…
driangle
  • 11,601
  • 5
  • 47
  • 54
8
votes
4 answers

Reassignment to a val in Scala

I am doing a training exercise in Scala and getting this val reassignment error. I don't see where I am reassigning a new value to a val class personTest { val alf = Person("Alf", 30, List(EmailAddress("alf.kristian@gmail.com"))) val fredrik =…
Mansur Ashraf
  • 1,337
  • 3
  • 9
  • 12