Is it always more performant to use withFilter instead of filter, when afterwards applying functions like map, flatmap etc.?
Why are only map, flatmap and foreach supported? (Expected functions like forall/exists as well)
I'm starting to explore Scala, and one of the things I'm intrigued by is the Option type and the promise of being able to eliminate null related errors.
However I haven't been able to work out how to transform a list (or other collection) of, say,…
Following on from my breathless confusion, what are some good resources which explain how the new Scala 2.8 collections library has been structured. I'm interested to find some information on how the following fit together:
The collection…
I'm doing a bit of Scala gymnastics where I have Seq[T] in which I try to find the "smallest" element. This is what I do right now:
val leastOrNone = seq.reduceOption { (best, current) =>
if (current.something < best.something) current
else…
Whilst working through the Scala exercises on Iterables, I encountered the following strange behaviour:
val xs = Set(5,4,3,2,1)
val ys = Set(1,2,3,4,5)
xs sameElements ys // true
val xs = Set(3,2,1)
val ys = Set(1,2,3)
xs sameElements ys …
There's something I don't understand about Scala's collection.mutable.Seq. It describes the interface for all mutable sequences, yet I don't see methods to append or prepend elements without creating a new sequence. Am I missing something obvious…
In Scala there is a Stream class that is very much like an iterator. The topic Difference between Iterator and Stream in Scala? offers some insights into the similarities and differences between the two.
Seeing how to use a stream is pretty simple…
class DefaultListMap[A, B <: List[B]] extends HashMap[A, B] {
override def default(key: A) = List[B]()
}
I wan't to create map A -> List[B]. In my case it is Long -> List[String] but when I get key from map that doesn't have value I would…
I have a java map: java.util.Map>
and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]]
I have used mapAsScalaMap but the result is not quite what I want, the result is:…
I have a Seq containing objects of a class that looks like this:
class A (val key: Int, ...)
Now I want to convert this Seq to a Map, using the key value of each object as the key, and the object itself as the value. So:
val seq: Seq[A] = ...
val…
In Java, I use LinkedHashMap for this purpose. The documentation of Java's LinkedHashMap is very clear that it has "predictable iteration order" and I need the same in Scala.
Scala has ListMap and LinkedHashMap, but the documentation on what they do…