scala-collections

How to use priority queues in Scala?

梦想与她 提交于 2019-12-03 11:02:22
I am trying to implement A* search in Scala (version 2.10), but I've ran into a brick wall - I can't figure out how to use Scala's Priority Queue. It seems like a simple task, but searching on Google didn't turn up anything (except for a single code sample that stopped working back in version 2.8) I have a set of squares, represented by (Int, Int) s, and I need to insert them with priorities represented by Int s. In Python it's pretty simple, since you just have a list of key, value pairs and use the heapq functions to sort it. But it appears that Scala's tuples aren't even comparable. So how

Scala String Equality Question from Programming Interview

…衆ロ難τιáo~ 提交于 2019-12-03 10:58:27
Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows: You have two strings consisting of alphabetic characters as well as a special character representing the backspace symbol. Let's call this backspace character '/'. When you get to the keyboard, you type this sequence of characters, including the backspace/delete character. The solution you are to implement must check if the two sequences of characters produce the same output. For example, "abc", "aa

Scala Nil equivalent for Set

一世执手 提交于 2019-12-03 10:45:21
问题 Is there an equivalent of Nil for Set in scala? I tried using Nil as a value for Set , but I got an error (expected since the type of Nil is List ) Thanks 回答1: 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 sets are invariant in their

Combining multiple Lists of arbitrary length

旧城冷巷雨未停 提交于 2019-12-03 10:44:56
I am looking for an approach to join multiple Lists in the following manner: ListA a b c ListB 1 2 3 4 ListC + # * § % .. .. .. Resulting List: a 1 + b 2 # c 3 * 4 § % In Words: The elements in sequential order, starting at first list combined into the resulting list. An arbitrary amount of input lists could be there varying in length. I used multiple approaches with variants of zip, sliding iterators but none worked and especially took care of varying list lengths. There has to be an elegant way in scala ;) val lists = List(ListA, ListB, ListC) lists.flatMap(_.zipWithIndex).sortBy(_._2).map(_

java.util.Iterator to Scala list?

試著忘記壹切 提交于 2019-12-03 10:03:44
I have the following code: private lazy val keys: List[String] = obj.getKeys().asScala.toList obj.getKeys returns a java.util.Iterator<java.lang.String> Calling asScala , via JavaConverers (which is imported) according to the docs.. java.util.Iterator <==> scala.collection.Iterator scala.collection.Iterator defines def toList: List[A] So based on this I believed this should work, however here is the compilation error: [scalac] <file>.scala:11: error: type mismatch; [scalac] found : List[?0] where type ?0 [scalac] required: List[String] [scalac] private lazy val keys : List[String] = obj

In Scala 2.8 collections, why was the Traversable type added above Iterable?

筅森魡賤 提交于 2019-12-03 09:58:18
I know that to be Traversable , you need only have a foreach method. Iterable requires an iterator method. Both the Scala 2.8 collections SID and the "Fighting Bitrot with Types" paper are basically silent on the subject of why Traversable was added. The SID only says "David McIver... proposed Traversable as a generalization of Iterable." I have vaguely gathered from discussions on IRC that it has to do with reclaiming resources when traversal of a collection terminates? The following is probably related to my question. There are some odd-looking function definitions in TraversableLike.scala ,

How can I find the index of the maximum value in a List in Scala?

半城伤御伤魂 提交于 2019-12-03 09:52:41
For a Scala List[Int] I can call the method max to find the maximum element value. How can I find the index of the maximum element? This is what I am doing now: val max = list.max val index = list.indexOf(max) One way to do this is to zip the list with its indices, find the resulting pair with the largest first element, and return the second element of that pair: scala> List(0, 43, 1, 34, 10).zipWithIndex.maxBy(_._1)._2 res0: Int = 1 This isn't the most efficient way to solve the problem, but it's idiomatic and clear. Since Seq is a function in Scala, the following code works: list.indices

How to get a random element from a Set in Scala

旧城冷巷雨未停 提交于 2019-12-03 09:49:15
For any given set, for instance, val fruits = Set("apple", "grape", "pear", "banana") how to get a random element from fruits ? Many Thanks. convert into Vector and get random element from it scala> val fruits = Set("apple", "grape", "pear", "banana") fruits: scala.collection.immutable.Set[String] = Set(apple, grape, pear, banana) scala> import scala.util.Random import scala.util.Random scala> val rnd=new Random rnd: scala.util.Random = scala.util.Random@31a9253 scala> fruits.toVector(rnd.nextInt(fruits.size)) res8: String = apple So, every answer posted before has complexity O(n) in terms of

Is there such a thing as bidirectional maps in Scala?

房东的猫 提交于 2019-12-03 09:44:11
I'd like to link 2 columns of unique identifiers and be able to get a first column value by a second column value as well as a second column value by a first column value. Something like Map(1 <-> "one", 2 <-> "two", 3 <-> "three") Is there such a facility in Scala? Actually I need even more: 3 columns to select any in a triplet by another in a triplet (individual values will never be met more than once in the entire map). But a 2-column bidirectional map can help too. Steve Guava has a bimap that you can use along with import scala.collection.JavaConversions._ Peter Schmitz My BiMap approach:

How to write a zipWith method that returns the same type of collection as those passed to it?

帅比萌擦擦* 提交于 2019-12-03 09:26:41
问题 I have reached this far: implicit def collectionExtras[A](xs: Iterable[A]) = new { def zipWith[B, C, That](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: CanBuildFrom[Iterable[A], C, That]) = { val builder = cbf(xs.repr) val (i, j) = (xs.iterator, ys.iterator) while(i.hasNext && j.hasNext) { builder += f(i.next, j.next) } builder.result } } // collectionExtras: [A](xs: Iterable[A])java.lang.Object{def zipWith[B,C,That](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: scala.collection.generic