score:89
Accepted answer
1 to n foreach { _ => some.code() }
score:10
I would suggest something like this:
List.fill(10)(println("hi"))
There are other ways, e.g.:
(1 to 10).foreach(_ => println("hi"))
Thanks to Daniel S. for the correction.
score:10
With scalaz 6:
scala> import scalaz._; import Scalaz._; import effects._;
import scalaz._
import Scalaz._
import effects._
scala> 5 times "foo"
res0: java.lang.String = foofoofoofoofoo
scala> 5 times List(1,2)
res1: List[Int] = List(1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
scala> 5 times 10
res2: Int = 50
scala> 5 times ((x: Int) => x + 1).endo
res3: scalaz.Endo[Int] = <function1>
scala> res3(10)
res4: Int = 15
scala> 5 times putStrLn("Hello, World!")
res5: scalaz.effects.IO[Unit] = scalaz.effects.IO$$anon$2@36659c23
scala> res5.unsafePerformIO
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
[ Snippet copied from here. ]
score:20
You can create a helper method
def rep[A](n: Int)(f: => A) { if (n > 0) { f; rep(n-1)(f) } }
and use it:
rep(5) { println("hi") }
Based on @Jörgs comment I have written such a times-method:
class Rep(n: Int) {
def times[A](f: => A) { loop(f, n) }
private def loop[A](f: => A, n: Int) { if (n > 0) { f; loop(f, n-1) } }
}
implicit def int2Rep(i: Int): Rep = new Rep(i)
// use it with
10.times { println("hi") }
Based on @DanGordon comments, I have written such a times-method:
implicit class Rep(n: Int) {
def times[A](f: => A) { 1 to n foreach(_ => f) }
}
// use it with
10.times { println("hi") }
Source: stackoverflow.com
Related Query
- Most elegant repeat loop in Scala
- Custom Scala enum, most elegant version searched
- Scala - Most elegant way of initialising values inside array that's already been declared?
- Scala most elegant way to handle option and throw exception from Scala Map
- Casbah Mongo as scala Array: is this the most elegant way?
- Elegant way to invert a map in Scala
- Efficiently repeat a character/string n times in Scala
- How to convert Row of a Scala DataFrame into case class most efficiently?
- Scala for loop over two lists simultaneously
- Can I stop the execution of an infinite loop in Scala REPL?
- Creating the most basic Scala project with Maven?
- Getting the index of the current loop in Play! 2 Scala template
- foreach loop in scala
- May a while loop be used with yield in scala
- scala - Spark : How to union all dataframe in loop
- How to write function simulating while loop in Scala
- Closing over the loop variable in Scala
- For loop in scala without sequence?
- Scala for loop and iterators
- What is the most elegant way to deal with an external library with internal state using a function programming language?
- How to correctly get current loop count from a Iterator in scala
- How Does One Make Scala Control Abstraction in Repeat Until?
- Scala while(true) type mismatch? Infinite loop in scala?
- How to repeat argument in String format in Scala
- scala while loop assignment
- Scala elegant list comprehension as in F#
- Why are Scala "for loop comprehensions" so very slow compared to FOR loops?
- Incrementing the for loop (loop variable) in scala by power of 5
- The most idiomatic way to perform conditional concatenation of strings in Scala
- What is the most succinct Scala way to reverse a Map?
More Query from same tag
- Thread safe file rename in Amazon Web Services S3
- Scala: Play Framework 2 ignores httpOnly flag in application.conf
- Call hierarchy and/or data flow tool for Scala
- Error in finding classpath for CassandraAdminCredentials when using Apache Ignite xml via Scala/Spark
- How to use ScalaQuery to insert a BLOB field?
- How to declare generic type function in scala?
- Map JSON to case class for scalatest
- How do I map over an HList where all of the elements are instances of a typeclass?
- Which tools can I use to benchmark a scala code?
- Techniques to add a REPL to a Java project
- Error found when importing spark.implicits
- Substituion of property on README file on play framework
- How to handle case-insensitive keywords with Scala parser combinators
- Old heap gen space not being garbaged collected in Scala
- Scala: Generalised method to find match and return match dependant values in collection
- Why is zipWithIndex implemented in Iterable and not Traversable?
- Deep remove specific empty json array in circe scala
- What's the rationale behind curried functions in Scala?
- How do I debug Lift applications in Eclipse?
- How to re-write this code idiomatically in Scala?
- Splitting a scalaz-stream process into two child streams
- How to determine resources for fatjar?
- Scala constraint based types and literals
- Custom Response from Controller in Finatra
- Optimization query for DataFrame Spark
- Execute parallel requests in Gatling
- What to learn after PHP? Scala or Clojure?
- Iterating over a pair RDD to run a function on an RDD from the second value.
- Scala version of Jgit
- Why doesn't keys() and values() work on (String,String) one-pair RDD, while sortByKey() works