scala-collections

Mixing in generic traits in parameterized classes without duplicating type parameters

♀尐吖头ヾ 提交于 2019-12-05 01:36:06
问题 Let's assume I want to create a trait that I can mix in into any Traversable[T]. In the end, I want to be able to say things like: val m = Map("name" -> "foo") with MoreFilterOperations and have methods on MoreFilterOperations that are expressed in anything Traversable has to offer, such as: def filterFirstTwo(f: (T) => Boolean) = filter(f) take 2 However, the problem is clearly that T is not defined as a type parameter on MoreFilterOperations. Once I do that, it's doable of course, but then

Enriching Scala collections with a method

限于喜欢 提交于 2019-12-05 01:17:07
问题 How do I add a foreachWithIndex method on Scala collections? This is what I could come up with so far: implicit def iforeach[A, CC <: TraversableLike[A, CC]](coll: CC) = new { def foreachWithIndex[B](f: (A, Int) => B): Unit = { var i = 0 for (c <- coll) { f(c, i) i += 1 } } } This doesn't work: Vector(9, 11, 34).foreachWithIndex { (el, i) => println(el, i) } Raises the following error: error: value foreachWithIndex is not a member of scala.collection.immutable.Vector[Int] Vector(9, 11, 34)

Method taking Seq[T] to return String rather than Seq[Char]

随声附和 提交于 2019-12-05 00:58:53
I would like to implement method that takes arbitrary Seq[T] and returns Seq[T] as well. But when String is provided it should also return String . Passing String works due to some implicit conversion from String to WrappedString extends IndexedSeq[Char] , but I get Seq[Char] in return. Is it possible to get String back? val sx: Seq[Int] = firstAndLast(List(1, 2, 3, 4)) val s1: Seq[Char] = firstAndLast("Foo Bar") val s2: String = firstAndLast("Foo Bar") //incompatible types error def firstAndLast[T](seq: Seq[T]) = Seq(seq.head, seq.last) firstAndLast() implementation is irrelevant, it is only

What is the current element in a Scala DoubleLinkedList?

独自空忆成欢 提交于 2019-12-04 23:53:12
I'm looking at using a DoubleLinkedList . It's remove() method says "Removes the current node from the double linked list." but there are no other references to current in the page. What is the current node, how do I set it, and surely this can't be the only way of removing an item? A DoubleLinkedList is at the same time the list itself and a list node, similar to the :: for a regular List . You can navigate from one cell to the next or to the previous one with next and prev , respectively, and get the value of a cell with elem . scala> val list = collection.mutable.DoubleLinkedList(1,2,3,4,5)

Scala collection type for filter

↘锁芯ラ 提交于 2019-12-04 22:32:50
Assume you have a List(1,"1") it is typed List[Any], which is of course correct and expected. Now if I map the list like this scala> List(1, "1") map { | case x: Int => x | case y: String => y.toInt | } the resulting type is List[Int] which is expected as well. My question is if there is an equivalent to map for filter because the following example will result in a List[Any]. Is this possible? I assume this could be solved at compile time and possibly not runtime? scala> List(1, "1") filter { | case x: Int => true | case _ => false | } Daniel C. Sobral Scala 2.9: scala> List(1, "1") collect {

How to use priority queues in Scala?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 18:11:09
问题 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

Efficiency/scalability of parallel collections in Scala (graphs)

人走茶凉 提交于 2019-12-04 17:03:10
So I've been working with parallel collections in Scala for a graph project I'm working on, I've got the basics of the graph class defined, it is currently using a scala.collection.mutable.HashMap where the key is Int and the value is ListBuffer[Int] (adjacency list). (EDIT: This has since been change to ArrayBuffer[Int] I had done a similar thing a few months ago in C++, with a std::vector<int, std::vector<int> > . What I'm trying to do now is run a metric between all pairs of vertices in the graph, so in C++ I did something like this: // myVec = std::vector<int> of vertices for (std::vector

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

拈花ヽ惹草 提交于 2019-12-04 16:38:55
问题 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) 回答1: 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

java.util.Iterator to Scala list?

走远了吗. 提交于 2019-12-04 15:59:30
问题 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]

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

本秂侑毒 提交于 2019-12-04 15:47:00
问题 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