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…
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),…
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
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 ?
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…
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…
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?
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…
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…
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",…
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…
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:…
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…
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…