scala-collections

The difference and conversion between Seq[Int] and List[Int]

这一生的挚爱 提交于 2020-08-19 12:00:30
问题 When I input Seq(1,2,3) in REPL, it returns me List(1,2,3) scala> Seq(1,2,3) res8: Seq[Int] = List(1, 2, 3) Therefore, I thought the List(1,2,3) may be of type List[Int] . And I tried to specify the type for the variable who are assigned to Seq(1,2,3) , but unexpectedly, the REPL complains like this: scala> val a:List[Int]=Seq(1,2,3) <console>:20: error: type mismatch; found : Seq[Int] required: List[Int] val a:List[Int]=Seq(1,2,3) Does anyone have ideas about what Seq[Int] = List(1, 2, 3)

The difference and conversion between Seq[Int] and List[Int]

走远了吗. 提交于 2020-08-19 11:55:48
问题 When I input Seq(1,2,3) in REPL, it returns me List(1,2,3) scala> Seq(1,2,3) res8: Seq[Int] = List(1, 2, 3) Therefore, I thought the List(1,2,3) may be of type List[Int] . And I tried to specify the type for the variable who are assigned to Seq(1,2,3) , but unexpectedly, the REPL complains like this: scala> val a:List[Int]=Seq(1,2,3) <console>:20: error: type mismatch; found : Seq[Int] required: List[Int] val a:List[Int]=Seq(1,2,3) Does anyone have ideas about what Seq[Int] = List(1, 2, 3)

The difference and conversion between Seq[Int] and List[Int]

妖精的绣舞 提交于 2020-08-19 11:54:51
问题 When I input Seq(1,2,3) in REPL, it returns me List(1,2,3) scala> Seq(1,2,3) res8: Seq[Int] = List(1, 2, 3) Therefore, I thought the List(1,2,3) may be of type List[Int] . And I tried to specify the type for the variable who are assigned to Seq(1,2,3) , but unexpectedly, the REPL complains like this: scala> val a:List[Int]=Seq(1,2,3) <console>:20: error: type mismatch; found : Seq[Int] required: List[Int] val a:List[Int]=Seq(1,2,3) Does anyone have ideas about what Seq[Int] = List(1, 2, 3)

Scala Convert Set to Map

眉间皱痕 提交于 2020-08-02 06:09:10
问题 How do I convert a Set("a","b","c") to a Map("a"->1,"b"->2,"c"->3)? I think it should work with toMap. 回答1: zipWithIndex is probably what you are looking for. It will take your collection of letters and make a new collection of Tuples, matching value with position in the collection. You have an extra requirement though - it looks like your positions start with 1, rather than 0, so you'll need to transform those Tuples: Set("a","b","c") .zipWithIndex //(a,0), (b,1), (c,2) .map{case(v,i) => (v,

What is the difference between List.view and LazyList?

安稳与你 提交于 2020-05-14 19:26:56
问题 I am new to Scala and I just learned that LazyList was created to replace Stream , and at the same time they added the .view methods to all collections. So, I am wondering why was LazyList added to Scala collections library, when we can do List.view ? I just looked at the Scaladoc, and it seems that the only difference is that LazyList has memoization, while View does not. Am I right or wrong? 回答1: Stream elements are realized lazily except for the 1st (head) element. That was seen as a

What is the actual class (not abstract and not trait) for Map and Set?

一曲冷凌霜 提交于 2020-05-13 04:34:53
问题 In Scala, map and set literals can be created through, for instance, val m = Map(1->"a") And the type of the reference m and the literal are both Map[Int, String] . However, scala documents show that Map is actually a trait, with abstract members that need to be implemented in order to instantiate: scala.collection.Map, scala.collection.immutable.Map, scala.collection.mutable.Map So my question is: what is the actual, concrete class that the literal Map is based on? Same question above is

Need a scala functional code for validating ipv4 and ipv6

拈花ヽ惹草 提交于 2020-03-28 06:59:08
问题 I was trying to construct functional program for parsing IP address. I am seeing an error. I wanted a simpler code which differentiates ipv4 to ipv6. Here is the JAVA code. import java.util.regex.Pattern; class Solution { String chunkIPv4 = "([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"; Pattern pattenIPv4 = Pattern.compile("^(" + chunkIPv4 + "\\.){3}" + chunkIPv4 + "$"); String chunkIPv6 = "([0-9a-fA-F]{1,4})"; Pattern pattenIPv6 = Pattern.compile("^(" + chunkIPv6 + "\\:){7}" +

Type based collection partitioning in Scala

不羁的心 提交于 2020-03-18 03:25:28
问题 Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given basket of fruits will return separate baskets of apples and pears: def segregateBasket(fruitBasket: Set[Fruit]): (Set[Apple], Set[Pear]) I've attempted a couple of approaches, but none of them seems to be fitting the bill perfectly. Below are my attempts:

Type based collection partitioning in Scala

可紊 提交于 2020-03-18 03:22:02
问题 Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given basket of fruits will return separate baskets of apples and pears: def segregateBasket(fruitBasket: Set[Fruit]): (Set[Apple], Set[Pear]) I've attempted a couple of approaches, but none of them seems to be fitting the bill perfectly. Below are my attempts:

Type based collection partitioning in Scala

老子叫甜甜 提交于 2020-03-18 03:21:13
问题 Given the following data model: sealed trait Fruit case class Apple(id: Int, sweetness: Int) extends Fruit case class Pear(id: Int, color: String) extends Fruit I've been looking to implement a segregate basket function which for the given basket of fruits will return separate baskets of apples and pears: def segregateBasket(fruitBasket: Set[Fruit]): (Set[Apple], Set[Pear]) I've attempted a couple of approaches, but none of them seems to be fitting the bill perfectly. Below are my attempts: