Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
10
votes
7 answers

Scala performance question

In the article written by Daniel Korzekwa, he said that the performance of following code: list.map(e => e*2).filter(e => e>10) is much worse than the iterative solution written using Java. Can anyone explain why? And what is the best solution for…
nanda
  • 24,458
  • 13
  • 71
  • 90
10
votes
2 answers

How to find the size of an object in scala?

Is there a way to find out the memory/size occupied by an object in scala? Thanks in advance for replying me.
Pratap D
  • 311
  • 3
  • 7
  • 14
10
votes
3 answers

How to write a zipWith method that returns the same type of collection as those passed to it?

I have reached this far: implicit def collectionExtras[A](xs: Iterable[A]) = new { def zipWith[B, C, That](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: CanBuildFrom[Iterable[A], C, That]) = { val builder = cbf(xs.repr) val (i, j) =…
Jim
  • 101
  • 3
10
votes
2 answers

Scala Immutable MultiMap

In Scala I would like to be able to write val petMap = ImmutableMultiMap(Alice->Cat, Bob->Dog, Alice->Hamster) The underlying Map[Owner,Set[Pet]] should have both Map and Set immutable. Here's a first draft for ImmutibleMultiMap with companion…
10
votes
1 answer

Find and Replace item in Scala collection

Let's say I have a list: val list = List(1, 2, 3, 4, 5) I want to replace all/first items that satisfy a predicate, I know the following way to do it: (e.g. replace any number that is even with -1) val filteredList = list.zipWithIndex.filter(_._2 %…
Omid
  • 1,959
  • 25
  • 42
10
votes
11 answers

How do you rotate (circular shift) of a Scala collection

I can do this quite easily, and cleanly, using a for loop. For instance, if I wanted to traverse a Seq from every element back to itself I would do the following: val seq = Seq(1,2,3,4,5) for (i <- seq.indices) { for (j <- seq.indices) { …
Nadir Muzaffar
  • 4,772
  • 2
  • 32
  • 48
10
votes
6 answers

Play Framework REST with basic authentication and SSL

I am new to this authentication area. I searched a lot but was not able to find a way to authenticate the REST calls made to the Play server. What are the various ways and best practice?
10
votes
4 answers

In Scala find files that match a wildcard String

How to obtain an Array[io.BufferedSource] to all files that match a wildcard in a given directory ? Namely, how to define a method io.Source.fromDir such that val txtFiles: Array[io.BufferedSource] = io.Source.fromDir("myDir/*.txt") // ??? Noticed…
elm
  • 20,117
  • 14
  • 67
  • 113
10
votes
1 answer

Simple example of extending a Scala collection

I'm looking for a very simple example of subclassing a Scala collection. I'm not so much interested in full explanations of how and why it all works; plenty of those are available here and elsewhere on the Internet. I'd like to know the simple way…
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
10
votes
1 answer

Mixing Parallel Collections with Akka

How well to scala parallel collection operations get along with the concurrency/parallelism used by Akka Actors (and Futures) with respect to efficient scheduling on the system? Actors' and Futures' execution is handled by an ExecutionContext…
vossad01
  • 11,552
  • 8
  • 56
  • 109
10
votes
5 answers

Why is there an Option.get method

Why is the method get defined in Option and not in Some? One could apply pattern matching or use foreach, map, flatMap, getOrElse which is preferred anyway without the danger of runtime exceptions if None.get is called. Are there really cases where…
Manuel Schmidt
  • 2,429
  • 1
  • 19
  • 32
10
votes
2 answers

Is Array[String] not a subclass of Seq[String] in Scala?

I wrote a method that accepts objects of all subclasses of Seq[String]. Unfortunately it won't accept an object of the type Array[String]. Is Array[String] not a subclass of Seq[String]? scala> def test[T <: Seq[String]](x: T) = {} test: [T <:…
Björn Jacobs
  • 4,033
  • 4
  • 28
  • 47
9
votes
1 answer

scala - Confusing "diverging implicit expansion" error when using "sortBy"

I wonder why List(3,2,1).toIndexedSeq.sortBy(x=>x) doesn't work: scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong :8: error: missing parameter type List(3,2,1).toIndexedSeq.sortBy(x=>x) …
Tom Dong
  • 521
  • 4
  • 10
9
votes
3 answers

Why do mutable and immutable ListMaps have different orders in Scala?

Why does the immutable version of the ListMap store in ascending order, while mutable version stores in descending order? Here is a test that you can use if you got scalatest-1.6.1.jar and junit-4.9.jar @Test def StackoverflowQuestion() { …
Zasz
  • 12,330
  • 9
  • 43
  • 63
9
votes
3 answers

Lazily evaluated indexed sequence type

I need to build a sequence of objects that are loaded from an external resource. This loading being an expensive operation needs to be delayed until the time the objects are needed. After the collection is built, I need an indexed access to the…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365