scala-collections

idiomatic “get or else update” for immutable.Map?

折月煮酒 提交于 2019-12-03 08:48:47
问题 What is the idiomatic way of a getOrElseUpdate for immutable.Map instances?. I use the snippet below, but it seems verbose and inefficient var map = Map[Key, Value]() def foo(key: Key) = { val value = map.getOrElse(key, new Value) map += key -> value value } 回答1: Let me summarise your problem: You want to call a method on a immutable data structure You want it to return some value and reassign a var Because the data structure is immutable, you’ll need to return a new immutable data structure,

How to append or prepend on a Scala mutable.Seq

回眸只為那壹抹淺笑 提交于 2019-12-03 08:06:16
问题 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 here? There are :+ and +: for append and prepend, respectively, but they create new collections — in order to be consistent with the behavior of immutable sequences, I assume. This is fine, but why is there no method like += and +=: , like

Scala performance question

夙愿已清 提交于 2019-12-03 07:32:30
问题 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 such code in Scala (I hope it's not a Java iterative version which is Scala-fied)? 回答1: The reason that particular code is slow is because it's working on primitives but it's using generic operations, so the primitives have to be boxed. (This could

Aggregate list values in Scala

醉酒当歌 提交于 2019-12-03 07:28:40
问题 Starting with a list of objects containing two parameters notional and currency, how can I aggregate the total notional per currency? Given: case class Trade(name: String, amount: Int, currency: String) val trades = List( Trade("T150310", 10000000, "GBP"), Trade("T150311", 10000000, "JPY"), Trade("T150312", 10000000, "USD"), Trade("T150313", 100, "JPY"), Trade("T150314", 1000, "GBP"), Trade("T150315", 10000, "USD") ) How can I get: Map(JPY -> 10000100, USD -> 10010000, GBP -> 10001000) 回答1: I

What is the fastest way to sum a collection in Scala

微笑、不失礼 提交于 2019-12-03 06:49:59
问题 I've tried different collections in Scala to sum it's elements and they are much slower than Java sums it's arrays (with for cycle). Is there a way for Scala to be as fast as Java arrays? I've heard that in scala 2.8 arrays will be same as in java, but they are much slower in practice 回答1: Indexing into arrays in a while loop is as fast in Scala as in Java. (Scala's "for" loop is not the low-level construct that Java's is, so that won't work the way you want.) Thus if in Java you see for (int

How do I implement a collection in Scala 2.8?

末鹿安然 提交于 2019-12-03 06:26:01
In trying to write an API I'm struggling with Scala's collections in 2.8(.0-beta1). Basically what I need is to write something that: adds functionality to immutable sets of a certain type where all methods like filter and map return a collection of the same type without having to override everything (which is why I went for 2.8 in the first place) where all collections you gain through those methods are constructed with the same parameters the original collection had (similar to how SortedSet hands through an ordering via implicits) which is still a trait in itself, independent of any set

How to use Java Collections.shuffle() on a Scala array?

牧云@^-^@ 提交于 2019-12-03 06:03:18
I have an array that I want to permutate randomly. In Java, there is a method Collections.shuffle() that can shuffle the elements of a List randomly. It can be used on an array too: String[] array = new String[]{"a", "b", "c"}; // Shuffle the array; works because the list returned by Arrays.asList() is backed by the array Collections.shuffle(Arrays.asList(array)); I tried using this on a Scala array, but the Scala interpreter responds with a lengthy answer: scala> val a = Array("a", "b", "c") a: Array[java.lang.String] = Array(a, b, c) scala> java.util.Collections.shuffle(java.util.Arrays

How to convert from from java.util.Map to a Scala Map

雨燕双飞 提交于 2019-12-03 05:26:59
A Java API returns a java.util.Map<java.lang.String,java.lang.Boolean> ;. I would like to put that into a Map[String,Boolean] So imagine we have: var scalaMap : Map[String,Boolean] = Map.empty val javaMap = new JavaClass().map() // Returns java.util.Map<java.lang.String,java.lang.Boolean> You can't do Map.empty ++ javaMap , because the ++ method does not know about Java maps. I tried: scalaMap = Map.empty ++ new collection.jcl.MapWrapper[String,Boolean] { override def underlying = javaMap } and: scalaMap = Map.empty ++ new collection.jcl.MapWrapper[java.lang.String,java.lang.Boolean] {

Min/max with Option[T] for possibly empty Seq?

烂漫一生 提交于 2019-12-03 04:04:12
问题 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 best } It works fine, but I'm not quite satisfied - it's a bit long for such a simple thing, and I don't care much for "if"s. Using minBy would be much more elegant: val least = seq.minBy(_.something) ... but min and minBy throw exceptions when the

Compare two Maps in Scala

自作多情 提交于 2019-12-03 03:49:21
Is there any pre-defined function that I can use to compare two Maps based on the key and give me the difference? Right now, I iterate Map1 and foreach key, I check if there is an element in Map2 and I pattern match to find the difference. Is there a much elegant way to do this? Consider the difference between the maps converted into sets of tuples, (m1.toSet diff m2.toSet).toMap Try: val diff = (m1.keySet -- m2.keySet) ++ (m2.keySet -- m1.keySet) diff contains the elements that are in m1 and not in m2 and that are in m2 and not in m1 . This solution looks like right way: scala> val x = Map(1