Questions tagged [scala-collections]

Collection library for Scala Programming Language

Related Tags:

1639 questions
12
votes
4 answers

Why is ClassManifest needed with Array but not List?

Define the following code: import scala.collection.JavaConversions._ val iter:java.util.Iterator[Any] = Array[Any](1, 2, 3).iterator def func(a:Any):String = a.toString def test[T:ClassManifest](iter:java.util.Iterator[Any], func:Any=>T):Array[T]…
Jus12
  • 17,824
  • 28
  • 99
  • 157
12
votes
1 answer

Why is upcasting necessary in this Scala code?

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…
Robin Green
  • 32,079
  • 16
  • 104
  • 187
12
votes
1 answer

Spark RDD equivalent to Scala collections partition

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),…
jdeastwood
  • 155
  • 5
12
votes
3 answers

Check for the presence of a sub-list

Can you provide me a performing (possibly idiomatic) way to check if a list A is a sublist of a given list B? E.g. isSubList(List(1,2), List(1,2,3,4)) // => true isSubList(List(1,2), List(5,6,7,8)) // => false
Aslan986
  • 9,984
  • 11
  • 44
  • 75
12
votes
5 answers

Scala simple histogram

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 ?
elm
  • 20,117
  • 14
  • 67
  • 113
12
votes
2 answers

Return type of Scala for/yield

I'm reading through Scala for the Impatient and I've come across something that's got me scratching my head. The following returns a String: scala> for ( c<-"Hello"; i <- 0 to 1) yield (c+i).toChar res68: String = HIeflmlmop But this returns a…
chb
  • 1,727
  • 7
  • 25
  • 47
12
votes
1 answer

Is there a Scala equivalent of EnumSet/EnumMap?

In Java we have two nice classes: EnumSet for sets of enums and EnumMap for a map whose keys are enums. EnumSet is represented as a 64-bit word (or an array of 64-bit words) and EnumMap as an array of values, both indexed by the ordinal numbers of…
Petr
  • 62,528
  • 13
  • 153
  • 317
12
votes
1 answer

Scala: List of pairs to pair of lists

I have a list of pairs: val pairs = List("a" -> 1, "b" -> 2, "c" -> 3) I'd like to convert it to a pair of lists: List("a", "b", "c") -> List(1, 2, 3) Basically, I want the opposite of zip() Any elegant way of doing so?
Electric Monk
  • 2,192
  • 2
  • 23
  • 33
12
votes
3 answers

Generic, type-safe way to flatten arbitrarily nested collections in Scala?

On occasion I take some time to play with Scala, whose mix of features appeals to me despite an inability to use it in my own work (thus far). For kicks I decided to try the first few 99 Haskell Problems in the most generic way possible — operating…
James Cunningham
  • 775
  • 4
  • 13
12
votes
1 answer

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

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…
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
12
votes
3 answers

Aggregate list values in Scala

Starting with a list of objects containing two parameters notional and currency, how can I aggregate the total notional per currency? Given: case class Trade(name: String, amount: Int, currency: String) val trades = List( Trade("T150310",…
parkr
  • 3,188
  • 5
  • 35
  • 39
11
votes
4 answers

How to functionally merge overlapping number-ranges from a List

I have a number of range-objects which I need to merge so that all overlapping ranges disappear: case class Range(from:Int, to:Int) val rangelist = List(Range(3, 40), Range(1, 45), Range(2, 50), etc) Here is the ranges: 3 40 1 45 2 50…
recalcitrant
  • 939
  • 11
  • 23
11
votes
5 answers

Lazy Cartesian product of several Seqs in Scala

I implemented a simple method to generate Cartesian product on several Seqs like this: object RichSeq { implicit def toRichSeq[T](s: Seq[T]) = new RichSeq[T](s) } class RichSeq[T](s: Seq[T]) { import RichSeq._ def cartesian(ss:…
F0RR
  • 1,590
  • 4
  • 16
  • 30
11
votes
3 answers

Using scala vararg methods in java

Why do all scala vararg methods, when used from java, seem to accept a Seq of variables, and can't be used as java native vararg methods. Is this a bug? For instance, Buffer has method def append(elems: A*): Unit. But in java it has another…
F0RR
  • 1,590
  • 4
  • 16
  • 30
11
votes
1 answer

Scala 2.13 what to use instead of MutableList?

I'm upgrading a software from Scala 2.12.8 to Scala 2.13, and figure out that the collection MutableList (scala.collection.mutable.MutableList) was removed according to many guides (like this one). This guide, for example, say that this was a…
Fernando Rezk
  • 314
  • 1
  • 18