score:5
Accepted answer
combine the seqs first. it's cheaper to use an iterator, which won't create an intermediate collection.
scala> val as = seq(1,2,3) ; val bs = seq.empty[int]
as: seq[int] = list(1, 2, 3)
bs: seq[int] = list()
scala> (as ++ bs).mkstring("|")
res0: string = 1|2|3
scala> (as.iterator ++ bs).mkstring("|")
res1: string = 1|2|3
that is,
case block(fields, children) => (fields.iterator ++ children).map(_.format).mkstring("|")
score:3
trait cda {
def format: string = this match {
case f: field => f.value
case block(fields, children) => fields.map(f => f.format).mkstring("|") + {if (!children.isempty) {"|" + children.map(b => b.format).mkstring("|")} else ""}
case record(keys, blocks) => blocks.map(b => b.format).mkstring("|")
}
}
Source: stackoverflow.com
Related Query
- Pattern matching and recursion issue
- Scala - Pattern Matching and For loop issue
- Scala - Find duplicates in list using pattern matching and recursion
- Pattern matching with conjunctions (PatternA AND PatternB)
- Haskell, Scala, Clojure, what to choose for high performance pattern matching and concurrency
- Case Class and Implicit Arguments and Pattern Matching
- Pattern matching and infinite streams
- Scala Get First and Last elements of List using Pattern Matching
- Case classes, pattern matching and curried constructors in Scala
- Case classes, pattern matching and varargs
- Scala XML Pattern matching and Attributes
- How to prevent this kind of bug - pattern matching and Nil
- Difference between type inference of method and class type parameters in pattern matching
- Implementing ifTrue, ifFalse, ifSome, ifNone, etc. in Scala to avoid if(...) and simple pattern matching
- Pattern matching on List[T] and Set[T] in Scala vs. Haskell: effects of type erasure
- Regex and Pattern Matching in Scala Part II
- How does Scala Cons pattern matching determine the head and the tail of a List?
- macros, splice, and pattern matching
- Scala pattern matching and type inference
- Case Classes and Pattern Matching on List
- What is the difference of the unapply function in pattern matching and in a partial function
- What's the relationship between pattern matching and type system?
- Why does this snippet with pattern matching and higher kinded types no longer compile in Scala 2.12?
- Why compilation error in a for comprehension and pattern matching for case classes
- Scala 2.8.0.RC2 compiler issue on pattern matching statement?
- Type Bounds and Pattern Matching in Scala
- Pattern matching in Scala and variables
- Context bounds and pattern matching in scala
- Pattern matching on base class and all derived classes in Scala
- Difference of pattern matching for "::(a,b)" and "List(a,b)"
More Query from same tag
- Unable to use a class from a package in /lib directory
- Scala : Define implicit functions in an object or in a class
- Scala pattern match repeated regex groups
- Why am I getting "could not find implicit value for parameter messages: play.api.i18n.Messages"
- Scala - List[MyObject] -> Map[Long, List[Long]]
- Why Spark DataSet loses all its schema and just returning byte[]?
- scalajs-react: Idiomatic way of processing ajax response
- Spark streaming checkpoint
- How to Drop a Duplicates Column in dataframe while retaining the Unique Column including the only one column among duplicates
- Accessing scala.None from Java
- add scala code to java file
- How to save output of multiple queries under single JSON file in appended mode using spark scala
- Best way to manage custom expressions in scala
- How can I create a factory method in scala that returns a class whose generic parameter is constrained to be a ClassManifest?
- Scala build tool and test framework that play nice together?
- In twitter future, how to set function of asynchronize return directly
- is there any scala network library with actor model
- sbt compile time warning: non-variable type argument String in type pattern List[String]
- Two objects having same hashcode means they are referring to same object ..right?
- How to access the data from streaming query in "memory" table for subsequent batch queries?
- String companion object in scala
- Scala, Play Framework Slick issue - could not find implicit value for parameter rconv
- How to construct case classes conditionally based on their parameters?
- Compilation error when declaring Functor for Either even after using type projections
- How to read an ODS document?
- Async response processing with HTTP4S
- infinite loop shutting down ensime
- How to pattern-match a nested class in Scala?
- Cannot escape a nested future in Scala?
- Gatling Feeder running out of values