scala-collections

Merge Maps in scala dataframe

南楼画角 提交于 2021-02-08 08:32:30
问题 I have a dataframe with columns col1,col2,col3. col1,col2 are strings. col3 is a Map[String,String] defined below |-- col3: map (nullable = true) | |-- key: string | |-- value: string (valueContainsNull = true) I have grouped by col1,col2 and aggregated using collect_list to get an Array of Maps and stored in col4. df.groupBy($"col1", $"col2").agg(collect_list($"col3").as("col4")) |-- col4: array (nullable = true) | |-- element: map (containsNull = true) | | |-- key: string | | |-- value:

When should I use Scala's Array instead of one of the other collections?

做~自己de王妃 提交于 2021-02-07 12:28:44
问题 This is more a question of style and preference but here goes: when should I use scala.Array? I use List all the time and occasionally run into Seq, Map and the like, but I've never used nor seen Array in the wild. Is it just there for Java compatibility? Am I missing a common use-case? 回答1: First of all, let's make a disclaimer here. Scala 2.7's Array tries to be a Java Array and a Scala Collection at the same time. It mostly succeeds, but fail at both for some corner cases. Unfortunately,

How to make method return the same generic as the input?

泪湿孤枕 提交于 2021-02-06 10:12:45
问题 I want to split a string delimited by commas and use the result as either a Seq or a Set : def splitByComma(commaDelimited: String): Array[String] = commaDelimited.trim.split(',') def splitByCommaAsSet(commaDelimited: String): Set[String] = splitByComma(commaDelimited).toSet def splitByCommaAsSeq(commaDelimited: String): Seq[String] = splitByComma(commaDelimited).toSeq val foods = "sushi,taco,burrito" val foodSet = splitByCommaAsSet(foods) // foodSet: scala.collection.immutable.Set[String] =

How to make method return the same generic as the input?

百般思念 提交于 2021-02-06 10:12:01
问题 I want to split a string delimited by commas and use the result as either a Seq or a Set : def splitByComma(commaDelimited: String): Array[String] = commaDelimited.trim.split(',') def splitByCommaAsSet(commaDelimited: String): Set[String] = splitByComma(commaDelimited).toSet def splitByCommaAsSeq(commaDelimited: String): Seq[String] = splitByComma(commaDelimited).toSeq val foods = "sushi,taco,burrito" val foodSet = splitByCommaAsSet(foods) // foodSet: scala.collection.immutable.Set[String] =

Difference between size and sizeIs

谁说胖子不能爱 提交于 2021-02-05 05:45:05
问题 What is the semantic difference between size and sizeIs? For example, List(1,2,3).sizeIs > 1 // true List(1,2,3).size > 1 // true Luis mentions in a comment that ...on 2.13+ one can use sizeIs > 1 which will be more efficient than size > 1 as the first one does not compute all the size before returning Add size comparison methods to IterableOps #6950 seems to be the pull request that introduced it. Reading the scaladoc Returns a value class containing operations for comparing the size of this

Cannot breakout of foreach loop in scala

依然范特西╮ 提交于 2021-01-29 08:22:42
问题 i have this below code > .foreach("${plist}", "newshole") { > exec( > http("get the user id") > .get("${newshole}/jcr:content.1.json") > .headers(headers_2) > .check(bodyString.saveAs("Res1")) > ) > exec(session => { > var mynewshole = session("Res1").as[String] > if (!mynewshole.contains("testingInProgress")) { > println("Doesn't contain: " + mynewshole) > (http("post the user id") > .post("${newshole}/jcr:content") > .headers(headers_2) > .formParam("testingInProgress", session.userId)) >

What's the difference between LazyList and Stream in Scala?

我的梦境 提交于 2021-01-02 06:11:07
问题 I noticed that Stream is deprecated in Scala 2.13 and they suggest using LazyList . They also say "Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)". What does it exactly mean ? Why did they deprecate Stream ? 回答1: NthPortal, a contributor to LazyList , states in Update and improve LazyList docs #7842 The key difference between LazyList and Stream - and its key feature - is that whether or not it is lazy is evaluated lazily. I'm not sure how best to convey

What's the difference between LazyList and Stream in Scala?

孤街浪徒 提交于 2021-01-02 06:08:26
问题 I noticed that Stream is deprecated in Scala 2.13 and they suggest using LazyList . They also say "Use LazyList (which is fully lazy) instead of Stream (which has a lazy tail only)". What does it exactly mean ? Why did they deprecate Stream ? 回答1: NthPortal, a contributor to LazyList , states in Update and improve LazyList docs #7842 The key difference between LazyList and Stream - and its key feature - is that whether or not it is lazy is evaluated lazily. I'm not sure how best to convey