scala-collections

scala - Confusing “diverging implicit expansion” error when using “sortBy”

ぃ、小莉子 提交于 2019-12-04 03:03:47
I wonder why List(3,2,1).toIndexedSeq.sortBy(x=>x) doesn't work: scala> List(3,2,1).toIndexedSeq.sortBy(x=>x) // Wrong <console>:8: error: missing parameter type List(3,2,1).toIndexedSeq.sortBy(x=>x) ^ <console>:8: error: diverging implicit expansion for type scala.math.Ordering[B] starting with method Tuple9 in object Ordering List(3,2,1).toIndexedSeq.sortBy(x=>x) ^ scala> Vector(3,2,1).sortBy(x=>x) // OK res: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3) scala> Vector(3,2,1).asInstanceOf[IndexedSeq[Int]].sortBy(x=>x) // OK res: IndexedSeq[Int] = Vector(1, 2, 3) scala> List(3,2,1)

Scala: Contains in mutable and immutable sets

让人想犯罪 __ 提交于 2019-12-04 02:02:06
I've discovered a strange behavior for mutable sets which I cannot understand: I have a object which I want to add to a set. The equals method for the class is overridden. When I add two different objects to the set, which produces the same output for equals method, I get a different behavior between mutable and immutable sets for the contains method. Here is the code snippet: class Test(text:String){ override def equals(obj:Any) = obj match { case t: Test => if (t.text == this.text) true else false case _ => false } override def toString = text } val mutableSet:scala.collection.mutable.Set

Scala Buffer: Size or Length?

女生的网名这么多〃 提交于 2019-12-04 00:07:43
问题 I am using a mutable Buffer and need to find out how many elements it has. Both size and length methods are defined, inherited from separate traits. Is there any actual performance difference, or can they be considered exact synonyms? 回答1: They are synonyms, mostly a result of Java's decision of having size for collections and length for Array and String . One will always be defined in terms of the other, and you can easily see which is which by looking at the source code, the link for which

scala.collection.breakOut vs views

依然范特西╮ 提交于 2019-12-03 23:55:57
This SO answer describes how scala.collection.breakOut can be used to prevent creating wasteful intermediate collections. For example, here we create an intermediate Seq[(String,String)] : val m = List("A", "B", "C").map(x => x -> x).toMap By using breakOut we can prevent the creation of this intermediate Seq : val m: Map[String,String] = List("A", "B", "C").map(x => x -> x)(breakOut) Views solve the same problem and in addition access elements lazily: val m = (List("A", "B", "C").view map (x => x -> x)).toMap I am assuming the creation of the View wrappers is fairly cheap, so my question is:

Conversion from scala parallel collection to regular collection

旧时模样 提交于 2019-12-03 23:33:59
I'm trying to convert back from a parallel collection to a regular map. According to the api, if I call toMap on any appropriately defined parallel collection, it's supposed to return a standard Map, but it's returning ParMap over a flattened collection of iterables. I have a val task: Stream[Future[Iterable[Tuple2[String, String]]]] And from which I get: val res: ParSeq[Iterable[Tuple2[String, String]]] = tasks.par.map(f => f.apply()) Finally: val finalresult = res.flatten.toMap Unfortunately, the type of finalresult is ParMap[String, String] . On the other hand, if I call it like: tasks.par

Apply function to one element only in list or array in Scala

故事扮演 提交于 2019-12-03 17:14:12
For any given list or array, for instance val list = (1 to 3).toList val array = (1 to 3).toArray and a given function that maps from and onto the collection type, for instance def f(v: Int): Int = v + 10 how to apply f to the ith element of list or array so that list.myApply(f, ith = 2) res: List(1,12,3) and also array.myApply(f, ith = 2) res: Array(1,12,3) tl;dr import scala.collection.SeqLike import scala.collection.generic.CanBuildFrom implicit class Seq_[A, Repr, S : ({type L[X] = X => SeqLike[A, Repr]})#L](seq: S) { def myApply[B >: A, That](f: A => B, ith: Int) (implicit bf:

Mixing in generic traits in parameterized classes without duplicating type parameters

丶灬走出姿态 提交于 2019-12-03 16:45:34
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 my code would read: val m = Map("name" -> "foo") with MoreFilterOperations[(String,String)] or if I

How do I implement a collection in Scala 2.8?

爱⌒轻易说出口 提交于 2019-12-03 16:21:59
问题 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

Scala Map pattern matching

人走茶凉 提交于 2019-12-03 14:27:50
How to do pattern matching on a Map in Scala ? A (non working) attempt includes, Map("a"->1, "b"->2, "c"->3) match { case Map(a,b,_*) => a } which errs with value Map is not a case class, nor does it have an unapply/unapplySeq member case Map(a,b,_*) => a The error is indicative enough, yet how to enrich Map with an unapply method for pattern matching ? Many Thanks Update Following @Paul's comment, a neater use case may be like this, Map("a"->1, "b"->2, "c"->3) match { case Map("b"->2,_*) => "222" } namely, in this case, if map contains key b that maps onto value 2 . Most easy way is tramsform

Merge Sets of Sets that contain common elements in Scala

不羁的心 提交于 2019-12-03 12:16:28
问题 I want to implement a function in Scala, that, given a Set of Sets of Ints will merge any containing Set that contains one or more common elements. So for example, given: def mergeSets(sets: Set[Set[Int]]): Set[Set[Int]] = ??? val sets = Set(Set(1,2), Set(2,3), Set(3,7), Set(8,10)) val mergedSets = mergeSets(sets) mergedSets will contain Set(Set(1,2,3,7), Set(8,10)) What would be a nice, efficient and functional if possible, way to do this in Scala? 回答1: The most efficient way to do this will