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…
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"…
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
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)…
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].
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 =>…
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…
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…
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:…
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…
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:…
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…
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…
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…
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…