scala-collections

Can I create a collection in Scala that uses different equals/hashCode/compare implementations?

China☆狼群 提交于 2019-12-07 05:32:01
问题 I'm looking for as simple way to create an identity set. I just want to be able to keep track of whether or not I've "seen" a particular object while traversing a graph. I can't use a regular Set because Set uses "==" (the equals method in Scala) to compare elements. What I want is a Set that uses "eq." Is there any way to create a Set in Scala that uses some application-specified method for testing equality rather than calling equals on the set elements? I looked for some kind of "wrapEquals

how to remove every nth element from the Scala list?

痴心易碎 提交于 2019-12-06 15:47:59
Is there any methods available to remove every n th element from the Scala List ? I hope we can do this inside filter method and return another list by writing a logic. But is this efficient way to do it? Simply: list.zipWithIndex .filter { case (_, i) => (i + 1) % n != 0 } .map { case (e, _) => e } Simplest so far, I think def removeNth[A](myList: List[A], n: Int): List[A] = myList.zipWithIndex collect { case (x,i) if (i + 1) % n != 0 => x } collect is an oft-forgotten gem that takes a partial function as its second argument, maps elements with that function and ignores those that are not in

Scala: how to traverse stream/iterator collecting results into several different collections

为君一笑 提交于 2019-12-06 11:00:58
问题 I'm going through log file that is too big to fit into memory and collecting 2 type of expressions, what is better functional alternative to my iterative snippet below? def streamData(file: File, errorPat: Regex, loginPat: Regex): List[(String, String)]={ val lines : Iterator[String] = io.Source.fromFile(file).getLines() val logins: mutable.Map[String, String] = new mutable.HashMap[String, String]() val errors: mutable.ListBuffer[(String, String)] = mutable.ListBuffer.empty for (line <- lines

Scala for-comprehension returning an ordered map

╄→尐↘猪︶ㄣ 提交于 2019-12-06 03:41:25
How can I use a for-comprehension that returns something I can assign to an ordered Map? This is a simplification of the code I have: class Bar class Foo(val name: String, val bar: Bar) val myList: java.util.List[Foo] = ... val result: ListMap[String, Bar] = for { foo <- myList } yield (foo.name, foo.bar) I need to make sure my result is an ordered Map, in the order tuples are returned from the for-comprehension. With the above, I get the error: error: type mismatch; found : scala.collection.mutable.Buffer[(String,Bar)] required: scala.collection.immutable.ListMap[String,Bar] foo <- myList

Scala Collections inconsistencies

风流意气都作罢 提交于 2019-12-06 03:02:31
问题 Why is there a lack of consistency between Sets and Lists in Scala Collections API? For example, there is immutable Set, but also a mutable one. If I want to use the latter, I can simply do this: val set = Set[A]() set += new A However, there is no mutable List, per se. If I want to write a similar code snippet using Lists, which data structure to use? LinkedList sounds as a good candidate, because it is mutable, but has no += method defined. ListBuffer seems to satisfy the requirements, but

Create a custom scala collection where map defaults to returning the custom collection?

一世执手 提交于 2019-12-06 01:59:43
问题 The trait TraversableLike[+A, +Repr] allows one to make a collection where some functions will return a Repr , while others continue to return the type parameter That on the function. Is there a way to define a CustomCollection[A] where functions like map , ++ , and others will default That as Repr if not inferred otherwise? Here is a code snippet that hopefully describes what I would like: case class CustomCollection[A](list: List[A]) extends TraversableLike[A, CustomCollection[A]] {

How does the type inferencer work on reduceLeft?

Deadly 提交于 2019-12-05 19:22:50
Further to my other question about reduceLeft , the signature of reduceLeft on Seq is def reduceLeft [B >: A] (f: (B, A) ⇒ B): B and we can call it with expressions such as List(1,2,3,4) reduceLeft (_ + _) In this example A is Int , so reduceLeft expects a Function2[B >: Int, Int, B] . Regardless of how reduceLeft works (which is irrelevant), how does the type inferencer know that B has a + method, when it could be of type Any ? I think the section 6.26.4 Local Type Inference of the spec sort of explains what's going on. The compiler will look for an optimal type. When the type parameter is

Why is upcasting necessary in this Scala code?

懵懂的女人 提交于 2019-12-05 16:35:56
问题 This compiles: import scala.collection._ trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]] extends SortedSetLike[A, This] { this: This => def bar: This = (this: SortedSetLike[A,This]).empty } But if the upcast is removed it fails to compile: import scala.collection._ trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]] extends SortedSetLike[A, This] { this: This => def bar: This = this.empty } Why? From the extends clause we know that Foo is a SortedSetLike[A, This] ,

Preferred Scala collection for progressively removing random items?

二次信任 提交于 2019-12-05 14:02:56
I have an algoritm which takes many iterations, each of which scores items in a collection and removes the one with the highest score. I could populate a Vector with the initial population, continually replacing it as a var , or choose a mutable collection as a val . Which of the mutable collections would best fit the bill? You could consider a DoubleLinkedList , which has a convenient remove() method to remove the current list cell. I think a Map (or its close relative, the Set ) might do well. It doesn't have indexed access, but that doesn't seem to be what you want. If you go for a TreeMap

update the last element of List

给你一囗甜甜゛ 提交于 2019-12-05 12:40:31
I have a List val first = List("A","B","C","D") and I want to create a new list from it but change the last element only: val newLastVal = "E" val second = List("A","B","C","E") can't figure this one out! Thanks in advance you can also use .init or .dropRight(1) to remove last element and then can add new item to list val second=first.init:+newLastVal //preferable OR val second=first.dropRight(1):+newLastVal you can use .updated(postion,value) val second=first.updated(first.length-1,newLastVal) 来源: https://stackoverflow.com/questions/24838826/update-the-last-element-of-list