scala-collections

scala implicit or explicit conversion from iterator to iterable

大兔子大兔子 提交于 2019-12-10 12:47:16
问题 Does Scala provide a built-in class, utility, syntax, or other mechanism for converting (by wrapping) an Iterator with an Iterable? For example, I have an Iterator[Foo] and I need an Iterable[Foo], so currently I am: val foo1: Iterator[Foo] = .... val foo2: Iterable[Foo] = new Iterable[Foo] { def elements = foo1 } This seems ugly and unnecessary. What's a better way? 回答1: Iterator has a toIterable method in Scala 2.8.0, but not in 2.7.7 or earlier. It's not implicit, but you could define your

Spark RDD equivalent to Scala collections partition

旧时模样 提交于 2019-12-10 12:35:03
问题 This is a minor issue with one of my spark jobs which doesn't seem to cause any issues -- yet annoys me every time I see it and fail to come up with a better solution. Say I have a Scala collection like this: val myStuff = List(Try(2/2), Try(2/0)) I can partition this list into successes and failures with partition: val (successes, failures) = myStuff.partition(_.isSuccess) Which is nice. The implementation of partition only traverses the source collection once to build the two new

how to remove every nth element from the Scala list?

随声附和 提交于 2019-12-10 11:43:27
问题 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? 回答1: Simply: list.zipWithIndex .filter { case (_, i) => (i + 1) % n != 0 } .map { case (e, _) => e } 回答2: 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

update the last element of List

吃可爱长大的小学妹 提交于 2019-12-10 09:23:47
问题 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 回答1: 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 回答2: you can use .updated(postion,value) val second=first.updated(first.length-1

Why do mutable and immutable ListMaps have different orders in Scala?

孤街浪徒 提交于 2019-12-10 02:45:10
问题 Why does the immutable version of the ListMap store in ascending order, while mutable version stores in descending order? Here is a test that you can use if you got scalatest-1.6.1.jar and junit-4.9.jar @Test def StackoverflowQuestion() { val map = Map("A" -> 5, "B" -> 12, "C" -> 2, "D" -> 9, "E" -> 18) val sortedIMMUTABLEMap = collection.immutable.ListMap[String, Int](map.toList.sortBy[Int](_._2): _*) println("head : " + sortedIMMUTABLEMap.head._2) println("last : " + sortedIMMUTABLEMap.last

What is the current element in a Scala DoubleLinkedList?

左心房为你撑大大i 提交于 2019-12-10 01:06:55
问题 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? 回答1: 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 ,

Scala collection type for filter

北战南征 提交于 2019-12-10 00:44:49
问题 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 {

What is the most succinct Scala way to reverse a Map?

筅森魡賤 提交于 2019-12-09 16:55:35
问题 What is the most succinct Scala way to reverse a Map? The Map may contain non-unique values. EDIT: The reversal of Map[A, B] should give Map[B, Set[A]] (or a MultiMap, that would be even better). 回答1: If you can lose duplicate keys: scala> val map = Map(1->"one", 2->"two", -2->"two") map: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,one), (2,two), (-2,two)) scala> map.map(_ swap) res0: scala.collection.immutable.Map[java.lang.String,Int] = Map((one,1), (two,-2)) If you don't

Why is a Range transformed to a Vector after map operation?

回眸只為那壹抹淺笑 提交于 2019-12-09 16:37:56
问题 Following Scala courses on Coursera, Martin Odersky showed an example code which is: 1 to 5 map ( i => i*i ) And he said the Range gets transformed to a Vector because they share the same interface ( IndexedSeq ) and the result could not be represented as a Range (it was more clear in its example since he generated a pair which is not representable as a Range ). I'm not sure to understand because I think he said previously that in a for expression the 1st generator will determine the kind of

Scala simple histogram

a 夏天 提交于 2019-12-09 02:49:24
问题 For a given Array[Double] , for instance val a = Array.tabulate(100){ _ => Random.nextDouble * 10 } what is a simple approach to calculate a histogram with n bins ? 回答1: A very similar preparation of values as in @om-nom-nom 's answer, yet the histogram method quite small by using partition , case class Distribution(nBins: Int, data: List[Double]) { require(data.length > nBins) val Epsilon = 0.000001 val (max,min) = (data.max,data.min) val binWidth = (max - min) / nBins + Epsilon val bounds =