score:191
See scala.collection.generic.SeqFactory.fill(n:Int)(elem: =>A) that collection data structures, like Seq
, Stream
, Iterator
and so on, extend:
scala> List.fill(3)("foo")
res1: List[String] = List(foo, foo, foo)
WARNING It's not available in Scala 2.7.
score:1
I have another answer which emulates flatMap I think (found out that this solution returns Unit when applying duplicateN)
implicit class ListGeneric[A](l: List[A]) {
def nDuplicate(x: Int): List[A] = {
def duplicateN(x: Int, tail: List[A]): List[A] = {
l match {
case Nil => Nil
case n :: xs => concatN(x, n) ::: duplicateN(x, xs)
}
def concatN(times: Int, elem: A): List[A] = List.fill(times)(elem)
}
duplicateN(x, l)
}
}
def times(n: Int, ls: List[String]) = ls.flatMap{ List.fill(n)(_) }
but this is rather for a predetermined List and you want to duplicate n times each element
score:13
Using tabulate
like this,
List.tabulate(3)(_ => "foo")
score:15
(1 to n).map( _ => "foo" )
Works like a charm.
Source: stackoverflow.com
Related Query
- How to create a list with the same element n-times?
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- How to collate list items with the same first item onto a map
- Scala: Create a new list where each element is the elemnt of old list repeated with different suffix
- scala create new list by omitting the n element from existing list with tuples
- How to use shapeless HList to have a list of the same type but with a different generic?
- How to compare a list element with the next element, to yield this element?
- In Scala, how to deal with heterogeneous list of the same parameterized type
- Scala/RDD : How to compare a value of tuple with a list of values in the same tuple
- How to take the first element of each element of a RDD[Double,Double] and create a diagonal matrix with it?
- In a list of objects how do I subtract a field of one object with the same field of another object in the same List in Scala?
- In Scala, how to get a slice of a list from nth element to the end of the list without knowing the length?
- How to get a list with the Typesafe config library
- How to check if list contains all the same values?
- How to find the largest element in a list of integers recursively?
- How can I idiomatically "remove" a single element from a list in Scala and close the gap?
- Play: How to remove the fields without value from JSON and create a new JSON with them
- How to define a list of functions of the same arity in Scala?
- How to compare every element in the RDD with every other element in the RDD ?
- When create two different Spark Pair RDD with same key set, will Spark distribute partition with same key to the same machine?
- Seeking a scala-esque approach to iterate through a list with access to the "next" element
- How to filter TableQuery with the list of IDs?
- Concatenate element with next in the list if condition is met
- In Scala, given a list of lists, how can I create one nested HashMap from the elements?
- How can a method accept a (T <: Seq)[A] and return T[B] -- the same type of sequence with a different type parameter?
- How to create a data frame in a for loop with the variable that is iterating in loop
- How to create a generic function that can be applied to two one or more types containing the same parameters?
- How to get the 1st element of List which is itself a member of Map of type Map[int, Any]
- How to create an instance of a model with the ebean framework and scala in Play 2.2
- How do you declare a function in Scala that returns itself (or another function with the same signature)?
More Query from same tag
- How to assign value into a breeze Matrix in flatMap Scala-Spark?
- How to do a DB reverse lookup with Akka Persistence in Lagom?
- Slice notation in Scala?
- Reading a large file using Akka Streams
- Scala: Task not serializable in RDD map Caused by json4s "implicit val formats = DefaultFormats"
- How to unmarshall custom object from Http Response Entity in Scala?
- Simultaneous incremental compilation in SBT and Scala-IDE
- Debug build.sbt as a Scala program
- Faster way to get single cell value from Dataframe (using just transformation)
- Scala type system, cannot find common ancestor inline
- How to solve RuntimeException: TypeDoesNotMatch(Cannot convert 1:class java.lang.String to Long for column?
- How to test correct return String in Try construct after exception was thrown?
- Producer-Consumer: Should I synchronize read access
- Sending JSON request with Spray IO Client
- Cannot run wordcount example in flink
- Writing filter for pg-slick for "?|" operator
- BrowserStack error occurs when running selenium tests: Element is not clickable at point (48, -414)
- Scala: Transforming List of Strings containing long descriptions to list of strings containing only last sentences
- Import for getHourOfDay scala
- What Are The Benefits Of Scala?
- Filtering values on right side of scalaz disjunction
- Any way to print test case literally with Scala backticks
- how to create a map with a dataframe
- Scala / Slick, "Timeout after 20000ms of waiting for a connection" error
- Spark how to optimize RE-encoding of key-value pairs
- Enforcing precedence in implicit instances in Scala
- What's the relationship between pattern matching and type system?
- Separation of concerns
- How to choose typeclass by dynamic input in Scala
- Why is TypeTag of T Nothing when I don't explicitly specify type?