score:0
val m3 = (map1.toseq ++ map2.toseq). // combine the maps
groupby (x=>x._1). //group by the original keys
map{case (k,lst)=> (k, lst.map(x=> x._2))}. //strip the keys from the grouped sequences
filter{case (_, lst) => lst.forall(i => lst.head.getclass == i.getclass)}. //filter out hetergeneous seqs
tomap // make a map
score:0
without forall:
(map1.tolist ++ map2.tolist).groupby(_._1).mapvalues(_.map(_._2))
.filter(_._2.map(_.getclass).toset.tail.isempty)
map(third -> list(orange(), orange()), second -> list(banana(3), banana(4)))
this version requires a little more (but still linear inside filter
) cpu and memory than version with forall
, so you should use it only for small collections.
score:1
scala> val r: map[string, seq[fruit]] = (map1.tolist ++ map2.tolist).
groupby(x => x._1).
mapvalues(lst => lst.map(x => x._2)).
.filter {
case (key, lst) => lst.forall(x =>
x.getclass == lst.head.getclass)
}
r: map[string, seq[fruit]] = map(third -> list(orange(), orange()),
second -> list(banana(3), banana(4)))
Source: stackoverflow.com
Related Query
- Scala pattern match on the type of two bind variables
- Find out if two variables inherit from the same parameterized type in Scala
- Is there a way to expand the scope of an existential type quantifier in Scala to convince the type checker that two variables have the same type?
- scala: type equality of two variables
- Scala equality with type checking?
- Using Scala Implicitly for Type Equality
- Type equality in Scala
- Functor Instance for Type Constructor with Two Parameters in Scala
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- Comparing two Byte[]'s for equality in Scala (checking binary image data)
- Type equality in the Scala 2.10 Reflection API
- Check equality for two Spark DataFrames in Scala
- How can two coupled Scala generic type constructors refer to each other as type parameters?
- Scala Cannot prove type equality
- Scala - how come using a super-type with two generic parameters cause the scala type checker to treat the child-type differently?
- Show tooltip on variables with type in Intellij with Scala
- Scala merge two maps with different value type
- Scala - Subtracting two natural numbers at the type level
- Scala - Use different equality comparisons/hashing for the same type
- Scala type inference and getting the more specific of two type parameters as method result
- Add two date variables in Scala
- For loop with two variables in scala
- Use two lists of variables to add scala columns
- Appending two columns of type Array[struct] into a single column in a Scala spark DataFrame
- Scala : Merge two immutable maps on key and get new immutable map with same type
- Scala play implicit path binder for two path variables
- Scala typer stage says two uses of type parameter are different
- Reduce two Scala methods, that only differ in one Object Type
- Scala type equality and path-dependent types
- How can two different class extend same class return same type in Scala
More Query from same tag
- Jackson / No serializer found for class
- Scala / Play2 - dynamic constraints based on checkbox value
- Where to put complex logic of Scala case class default value
- How can I pretty print a wrappedarray in Zeppelin/Spark/Scala?
- How to return multiple variants of HList from a function?
- Type mismatch: scala.xml.NodeBuffer vs Seq[scala.xml.Node]
- Pattern matching syntax in Scala/Unfiltered
- Scala compiler throws annotation argument needs to be a constant error on constant argument
- Calling slow future returning function in scala for each item in a Seq
- `doto` for Scala
- Play framework stack trace logging and logging configuration
- How to set the type of array with dataset in spark scala
- How to override a case class attribute and avoid duplicated storage?
- How to build a Phantom CassandraConnection from datastax ClusterBuilder
- Parsing a list of named JSON objects with Play
- Consumer interrupted with WakeupException after timeout. Message: null. Current value of akka.kafka.consumer.wakeup-timeout is 3000 milliseconds
- Why does test report "[info] No tests were executed" for ScalaTest with a single specification in specs2?
- How to consisely replace a value inside Option?
- Can we consume JMS messages from a Topic through Spark Streaming?
- "close" a Stream?
- How do I transform json using scala lift?
- How to functionally merge overlapping number-ranges from a List
- How do I configure a Scala case class in Spring?
- Properly use .cache() and .unpersist()
- Scala combinator parser, what does >> mean?
- Getting error while converting DynamicFrame to a Spark DataFrame using toDF
- convert `com.ning.http.client.ListenableFuture[Any]` into `scala.concurrent.Future[Any]`
- Lower type bound on Scala field in mutable, covariant class?
- Replace class implementation with Scala Macro
- from null to 0 in Scala