scala-collections

Why `List[B]` is not a subtype of `Seq[L]` when `class B extends A` and `L <: A`?

谁说胖子不能爱 提交于 2019-12-12 16:34:18
问题 Having: class A class B extends A It is correct to write: val foo: Seq[A] = List[B](new B) What do I miss while having an error in? def bar[L <: A](): Seq[L] = List[B](new B) Error: [error] found : List[B] [error] required: Seq[L] [error] def t[L <: A](): Seq[L] = List[B](new B) 回答1: The signature of your bar method is essentially saying, tell me some subtype of A and I'll give you a sequence of things of that type. There are potentially a lot of subtypes of A that B is not a subtype of (i.e.

Default arguments to Scala's main function?

断了今生、忘了曾经 提交于 2019-12-12 15:41:46
问题 How do I get this to work, I've tried with and without new keyword: object Main extends App { override def main (args : Array[String] = new Array("default argument")) { println("args(0) = " + args(0)) } } http://ideone.com/5AyTxy (I have tried other [1, 2] methods but I think that its val is causing issues) Docs: http://docs.scala-lang.org/sips/completed/named-and-default-arguments.html#default_arguments PS: I should confirm that I want this to be bound to the args name; rather than to

Why is Seq.newBuilder returning a ListBuffer?

岁酱吖の 提交于 2019-12-12 13:16:54
问题 Looking at val sb = Seq.newBuilder[Int] println(sb.getClass.getName) sb += 1 sb += 2 val s = sb.result() println(s.getClass.getName) the output is scala.collection.mutable.ListBuffer scala.collection.immutable.$colon$colon using Scala 2.10.1. I would expect Seq.newBuilder to return a VectorBuilder for example. This is returned by CanBuildFrom , if the result is explicitly typed to a Seq : def build[T, C <: Iterable[T]](x: T, y: T) (implicit cbf: CanBuildFrom[Nothing, T, C]): C = { val b = cbf

Difference between Tuple and List[Any] in Scala?

∥☆過路亽.° 提交于 2019-12-12 12:35:58
问题 Currently, I am learning Scala and reading this book Programming in Scala and which says, " Unlike an array or list, a tuple can hold objects with different types." For example, the following tuple contain Int, String and Float. val tup = (1, "hello", 4.4) Again, the book say, "If you want to have any type of element in list/array, then you can use Any Datatype." val list = List[Any](1, "hello", 4.4) So, what is the difference between above these 2 approaches? what are the benefit of one over

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

≯℡__Kan透↙ 提交于 2019-12-12 09:34:35
问题 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 IndexedSeq[Char] , but I get Seq[Char] in return. Is it possible to get String back? val sx: Seq[Int] = firstAndLast(List(1, 2, 3, 4)) val s1: Seq[Char] = firstAndLast("Foo Bar") val s2: String = firstAndLast("Foo Bar") //incompatible types error def

Efficiency/scalability of parallel collections in Scala (graphs)

孤者浪人 提交于 2019-12-12 09:13:00
问题 So I've been working with parallel collections in Scala for a graph project I'm working on, I've got the basics of the graph class defined, it is currently using a scala.collection.mutable.HashMap where the key is Int and the value is ListBuffer[Int] (adjacency list). (EDIT: This has since been change to ArrayBuffer[Int] I had done a similar thing a few months ago in C++, with a std::vector<int, std::vector<int> > . What I'm trying to do now is run a metric between all pairs of vertices in

Scala Map pattern matching

懵懂的女人 提交于 2019-12-12 08:08:42
问题 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,_*) =>

Can I do a lazy take with a long parameter?

偶尔善良 提交于 2019-12-12 05:41:37
问题 Stream.take takes an int as a parameter. I want to define a take that takes a long instead- is this possible without using takeWhile to manage a counter? 回答1: Just so you know that it's at least doable (but is it a good idea?), you can enrich Stream with an alternate version of take : implicit class StreamOps[A](val stream: Stream[A]) extends AnyVal { def take(n: Long): Stream[A] = { if (n <= 0 || stream.isEmpty) Stream.empty else if (n == 1) Stream.cons(stream.head, Stream.empty) else Stream

create a map from list in Scala

三世轮回 提交于 2019-12-12 04:13:05
问题 I need to create a HashMap of directory-to-file in scala while I list all files in the directory. How can I achieve this in scala? val directoryToFile = awsClient.listFiles(uploadPath).collect { case path if !path.endsWith("/") => { path match { // do some regex matching to get directory & file names case regex(dir, date) => { // NEED TO CREATE A HASH MAP OF dir -> date. How??? } case _ => None } } } The method listFiles(path: String) returns a Seq[String] of absolute path of all files in the

Scala deserialize JSON to Collection

社会主义新天地 提交于 2019-12-12 00:49:38
问题 My JSON File containes below details { "category":"age, gender,post_code" } My scala code is below one val filename = args.head println(s"Reading ${args.head} ...") val json = Source.fromFile(filename) val mapper = new ObjectMapper() with ScalaObjectMapper mapper.registerModule(DefaultScalaModule) val parsedJson = mapper.readValue[Map[String, Any]](json.reader()) val data = parsedJson.get("category").toSeq It's returning Seq(Any) = example List(age, gender,post_code) but I need Seq(String)