Google guava vs Scala collection framework comparison

Google Guava is a fantastic library, there’s no doubt about it. However, it’s implemented in Java and suffers from all the restrictions that that implies: No immutable collection interface in the standard library No lambda literals (closures), so there’s some heavy boilerplate around the SAM types needed for e.g. predicates lots of duplication in type … Read more

Scala Nil equivalent for Set

Set.empty is that set; although you can’t get at it directly, it turns out that it is just a private object in the Set companion object (called, obviously enough, EmptySet). All that Set.empty does is return that set with a cast to the correct type. It is done this way, instead of with Nil, because … Read more

Conversion from scala parallel collection to regular collection

As you go explicitly from sequential to parallel collection via .par, you go back to sequential via .seq. Since sets and maps have parallel implementations, toMap and toSet calls leave the collection in the current domain. The example of reduce works because it, well, reduces the collection (the outer ParSeq disappears, leaving you with the … Read more

Cleaner tuple groupBy

Here’s a pimp that adds a toMultiMap method to traversables. Would it solve your problem? import collection._ import mutable.Builder import generic.CanBuildFrom class TraversableOnceExt[CC, A](coll: CC, asTraversable: CC => TraversableOnce[A]) { def toMultiMap[T, U, That](implicit ev: A <:< (T, U), cbf: CanBuildFrom[CC, U, That]): immutable.Map[T, That] = toMultiMapBy(ev) def toMultiMapBy[T, U, That](f: A => (T, U))(implicit … Read more

how to sort a scala.collection.Map[java.lang.String, Int] by its values?

Depending on what the expected output collection type is (SortedMaps are sorted on the keys), you could use something like this: Map(“foo”->3, “raise”->1, “the”->2, “bar”->4).toList sortBy {_._2} Result would be the list of key/value pairs sorted by the value: List[(java.lang.String, Int)] = List((raise,1), (the,2), (foo,3), (bar,4)) There is a Map type that retains the original … Read more

Semantics of Scala Traversable, Iterable, Sequence, Stream and View?

Traversable is the top of the collections hierarchy. Its main method is ‘foreach’ so it allows to do something for each element of the collection. An Iterable can create an Iterator, based on which foreach can be implemented. This defines some order of the elements, although that order might change for every Iterator. Seq(uence) is … Read more

Which scala mutable list to use?

Depends what you need. DoubleLinkedList is a linked list which allows you to traverse back-and-forth through the list of nodes. Use its prev and next references to go to the previous or the next node, respectively. LinkedList is a singly linked list, so there are not prev pointers – if you only traverse to the … Read more

scala parallel collections degree of parallelism

With the newest trunk, using the JVM 1.6 or newer, use the: collection.parallel.ForkJoinTasks.defaultForkJoinPool.setParallelism(parlevel: Int) This may be a subject to changes in the future, though. A more unified approach to configuring all Scala task parallel APIs is planned for the next releases. Note, however, that while this will determine the number of processors the query … Read more

tech