score:2
Accepted answer
how close is this?
val c : list[list[string]] = list(list("a", "c"), list("b"))
c.map(x => x.mkstring("[", ", ", "]")).mkstring
// string = [a, c][b]
score:0
you can define a function s
that makes a string ["a","b"]
from a list list("a","b")
.
val c : list[list[string]] = list(list("a", "c"), list("b"))
def s[t](list:list[t]) = list.mkstring("[",", ","]")
println(s(c map s)) // [["a", "c"], "b"]
or a more generic option:
val d = list(list("a", list("c"), list.empty[string]), list("b"))
def t[t](list: list[t]): string = list.map {
case a: list[_] => t(a)
case a => a
} mkstring("[", ", ", "]")
println(t(d)) // [[a, [c], []], [b]]
Source: stackoverflow.com
Related Query
- print lists with mkString
- Split list into multiple lists with fixed number of elements
- Print the data in ResultSet along with column names
- Traversing lists and streams with a function returning a future
- Zip two lists of different lengths with default element to fill
- Weird behavior trying to convert case classes to heterogeneous lists recursively with Shapeless
- comparing Scala lists with Java lists
- Split list into multiple lists with fixed number of elements in java 8
- Scala by name parameters with multiple argument lists and currying
- How to print @ symbol in HTML with play framework (scala)
- In Scala, how to pass function with multiple parameter lists as an argument?
- Dealing with explicit parameters required by inner implicit parameter lists
- Scala: how to work with long type parameter lists
- Comparing Lists with scalatest
- What's the neatest way to define circular lists with Scala?
- What's the best way to wrap a monix Task with a start time and end time print the difference?
- Cartesian product of three lists with cats
- Scala lists with existential types: `map{ case t => ... }` works, `map{ t => ... }` doesn't?
- functional style to multiply two lists with Scala
- Why does spark-shell print thousands lines of code after count on DataFrame with 3000 columns? What's JaninoRuntimeException and 64 KB?
- anonymous function with multiple parameter lists
- What does the Scala interpreter (REPL) print exactly (demonstration with Map and HashMap)?
- Calling Scala constructor with multiple argument lists
- Random merge of two lists with preserving order
- Create collection of cartesian product of two (and more) lists with Java Lambda
- Join two lists with unequal length in Scala
- Clean up signatures with long implicit parameter lists
- Scala distribution of lists with condition
- zipping lists with an optional list to construct a list of object in Scala
- Why do Scala maps print an extra value when iterating with foreach?
More Query from same tag
- DecodeJson not working after scalaz version update in application
- How can I define `LoneElement` and `Emptiness` for composed collections?
- for-expression to flatMap Conversion
- In Play 2.2, Spec2 tests, I get Configuration error[Cannot connect to database [default]]
- Flink Streaming - apply function in windows
- some operator questions
- Scala method = trait { ... } meaning
- Best FP idioms for AnyVals in collections
- how implicit keyword works in scala?
- Accessing OneToMany elements using play
- How to merge two Option[String] variables into one when using flatMap in Scala?
- How to run two SparkSql queries in parallel in Apache Spark
- Spark Streaming - java.lang.NoSuchMethodError Error
- Using Squeryl with Akka Actors
- calling Application.method(String s) method from index.scala.html
- Smaller or equal comparison syntax error
- Scala total function as partial function
- In scala, how to declare a nested class to be transient?
- matching types in scala
- spark.RDD take(n) returns array of element n, n times
- Scala conditional compilation
- How to avoid scala's case class default toString function being overridden?
- Java/Scala resource consumption and load
- Spark: java.lang.UnsupportedOperationException: No Encoder found for java.time.LocalDate
- Serialize / Deserialize Object To/From Delimited String
- if/else in Scala: Illegal start of simple expression/Il
- Trait does not conform with type parameter bounds
- Scala Iterator next method caching result?
- Better Hive-Spark connection?
- How to initialize function type parameter in Scala class?