Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
6
votes
1 answer

scala : creating temp directory and file

I am trying to create a temp directory and file underneath it. Here is my code snippet: var tempPath = System.getProperty("java.io.tmpdir") val myDir = new File(tempPath.concat(scala.util.Random.nextString(10).toString)) myDir.mkdir() val tempFile…
Pratap D
  • 311
  • 3
  • 7
  • 14
6
votes
3 answers

Invert a Map (String -> List) in Scala

I have a Map[String, List[String]] and I want to invert it. For example, if I have something like "1" -> List("a","b","c") "2" -> List("a","j","k") "3" -> List("a","c") The result should be "a" -> List("1","2","3") "b" -> List("1") "c"…
Kuranes
  • 197
  • 1
  • 12
6
votes
2 answers

Scala: good way to keep pairs of strings

What is a neat way to hold pairs of strings which are not necessarily key-values (might have duplicate keys), for a small collection? List[List[String]] works obviously but looks dirty. Cheers Parsa
parsa
  • 2,628
  • 3
  • 34
  • 44
6
votes
1 answer

Scala find missing values in a range

For a given range, for instance val range = (1 to 5).toArray val ready = Array(2,4) the missing values (not ready) are val missing = range.toSet diff ready.toSet Set(5, 1, 3) The real use case includes thousands of range instances with (possibly)…
elm
  • 20,117
  • 14
  • 67
  • 113
6
votes
1 answer

How can you can write generic Scala enhancement methods that bind collection type as well as element type?

If you are like me, you occasionally want to write enhanced methods for Scala collections or sequences, but you'd like to bind the collection type as well as the element type, not just upcast to Seq[T].
6
votes
1 answer

Scala's monadic chaining of Try

Consider the following chaining of function f, g and h using monadic for-comprehensions. for { x <- List ( 11, 22, 33, 44, 55 ) y <- f ( x ) z <- g ( y ) a <- h ( z ) } yield a If f, g and h all have the signature: Int =>…
Martin Berger
  • 1,120
  • 9
  • 19
6
votes
3 answers

spray Collection ToResponseMarshallable

I am trying to return a List from my complete directive in spray-routing. complete { List("hello") } However, I am getting an error - Expression of type List[String] doesn't conform to expected type ToResponseMarshallable I am getting the same…
adefor
  • 299
  • 2
  • 9
6
votes
1 answer

Scala Enumeration ValueSet.isEmpty slow

I am using Scala Enumeration ValueSets in a fairly high-throughput setting - creating, testing, union'ing and intersecting about 10M sets/second/core. I didn't expect this to be a big deal, because I had read somewhere that they were backed by…
experquisite
  • 879
  • 5
  • 14
6
votes
1 answer

Scala type inference: can't infer IndexedSeq[T] from Array[T]

In Scala 2.11.2, the following minimal example compiles only when using type ascription on the Array[String]: object Foo { def fromList(list: List[String]): Foo = new Foo(list.toArray : Array[String]) } class Foo(source:…
Chris
  • 2,057
  • 2
  • 16
  • 20
6
votes
3 answers

Why does Scala's indexOf (in List etc) return Int instead of Option[Int]?

I want to write really nice looking idiomatic Scala code list indexOf foo getOrElse Int.MaxValue but now I have to settle for idiotic Java looking code val result = list indexOf foo; if (result < 0) Int.MaxValue else result. Is there a good reason…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
6
votes
3 answers

Given an element in a sequence, how to get the previous element?

Let's say I have a Scala list List("apple", "orange", "banana", "chinese gooseberry")*. I want to search this list and return either the previous item in the list in relation to an item I already have. For example: getPrevious(fruit: String, fruits:…
sam-w
  • 7,478
  • 1
  • 47
  • 77
6
votes
2 answers

Create and populate two-dimensional array in Scala

What's the recommended way of creating a pre-populated two-dimensional array in Scala? I've got the following code: val map = for { x <- (1 to size).toList } yield for { y <- (1 to size).toList } yield (x, y) How do I make an array…
asteinlein
  • 63
  • 1
  • 3
6
votes
5 answers

How to fix the pattern-matching exhaustive warning?

Some scala code: val list = List(Some("aaa"), Some("bbb"), None, ...) list.filter(_!=None).map { case Some(x) => x + "!!!" // I don't want to handle `None` case since they are not possible // case None } When I run it, the compiler…
Freewind
  • 193,756
  • 157
  • 432
  • 708
6
votes
2 answers

best scala idiom for find & return

This is something I encounter frequently, but I don't know the elegant way of doing. I have a collection of Foo objects. Foo has a method bar() that may return null or a Bar object. I want to scan the collection, calling each object's bar() method…
IttayD
  • 28,271
  • 28
  • 124
  • 178
6
votes
1 answer

Applying map method to BitSet in Scala

With mutable BitSet, I tried to add the value 3 to all of its component, but I got this error. val x = BitSet() x.add(10); x.add(20) x.map(_ + 3) This is the message. :12: error: ambiguous implicit values: both method newCanBuildFrom in…
prosseek
  • 182,215
  • 215
  • 566
  • 871