score:4
it can be thought of as:
first we map:
val a = map("a" -> list(1 -> 11,1 -> 111), "b" -> list(2 -> 22,2 -> 222)).map(e => e._2)
// list(list((1, 11), (1, 111)), list((2, 22), (2, 222)))
then we flatten:
val b = a.flatten
// list((1, 11), (1, 111), (2, 22), (2, 222))
then we convert back to a map:
b.tomap
// map(1 -> 111, 2 -> 222)
since a map cannot have 2 values for 1 key, the value is overwritten.
really whats going on is that the flatmap is being converted into a loop like so:
for (x <- m0) b ++= f(x)
where:
m0 is our original map
b is a collection builder that has to build a
map
, aka,mapbuilder
f is our function being passed into the flatmap (it returns a
list[(int, int)]
)x is an element in our original map
the ++=
function takes the list we got from calling f(x)
, and calls +=
on every element, to add it to our map. for a map
, +=
just calls the original +
operator for a map
, which updates the value if the key already exists.
finally we call result
on our builder which just returns us our map
.
Source: stackoverflow.com
Related Query
- Correct interpretation of Scala Map's flatmap expression
- Scala flatMap method on Option type cannot be resolved. What is the correct way to do this?
- How do I view the type of a scala expression in IntelliJ
- Where does the "flatmap that s***" idiomatic expression in Scala come from?
- How are Scala collections able to return the correct collection type from a map operation?
- Scala maps -> operator
- Scala foldLeft on Maps
- Matching against a regular expression in Scala
- How do I find the correct Maven archetype project for developing with Scala in Eclipse?
- Scala add new column to dataframe by expression
- Compare two Maps in Scala
- Using find function for maps in scala
- Using collect on maps in Scala
- Illegal start of simple expression in Scala
- Removing empty strings from maps in scala
- Scala equivalent to C#'s Expression API
- Does the order of alternatives in a Scala match expression matter in terms of performance?
- Accessing Scala Parser regular expression match data
- Scala expression to replace a file extension in a string
- How to use Scala Cats Validated the correct way?
- Nested Default Maps in Scala
- What are the differences between mapcat in Clojure and flatMap in Scala in terms of what they operate on?
- Is it correct to define all classes as cases in Scala just to have all their arguments made properties automatically?
- Using Scala Case Classes as De-facto Maps
- Adaptive Maps in Scala (or Java) Preserving Insertion Order
- Scala XML.loadString vs literal expression
- Why does this Scala for expression using tuples fail to compile?
- @throws in Scala does not allow calling Java to catch correct exception type
- How to compile/eval a Scala expression at runtime?
- Use flatMap on List[Either] in Scala
More Query from same tag
- Spark dataframe random sampling based on frequency occurrence in dataframe
- Always read latest folder from s3 bucket in spark
- Tail-Recursion for Towers of Hanoi in Scala
- Scala Slick Escape User Input Strings
- Logging with akka. app design
- Why are Scala's `Lists` implemented as linked lists
- Scala: Generic method composing
- scala.io.source.fromFile with sbt subProjects reading from root resource folder
- How to run different main classed based upon setting?
- Is the Scalaz disjunction \/ meant to replace the standard library's Either?
- KafkaEmbedded throwing NoSuchMethodError: org.apache.kafka.common.config.ConfigDef$ValidString
- Filter Spark Dataframe with a variable
- UDF scala return [max,index]
- Having two upper bounds in Scala?
- Is a Path Dependent Type a subtype?
- Multiple actor systems for an application
- scala, unable to prepend list
- How to serialize a simple scala class to a CSV file?
- Flawed Understanding of Futures in For Comprehension
- How to log exception stacktrace into debug level?
- Applying aggregate function to every column of certain type
- Spark Split RDD into chunks and concatenate
- Why does a class body affect implicit resolution?
- Objects being "turned" to null randomly
- Akka-http logging request identifier
- slick joins with function inputs
- How to make the code more generic
- Scala: Storing objects with variable type
- Expose only field subset in REST JSON Service. Play framework, scala
- How save object in Mongodb with scala