Is there a brief syntax for executing a block n times in Scala?

前端 未结 6 1561
误落风尘
误落风尘 2020-12-13 05:54

I find myself writing code like this when I want to repeat some execution n times:

for (i <- 1 to n) { doSomething() }

I\'m looking for

6条回答
  •  萌比男神i
    2020-12-13 06:16

    With scalaz 5:

    doSomething.replicateM[List](n)
    

    With scalaz 6:

    n times doSomething
    

    And that works as you would expect with most types (more precisely, for every monoid):

    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] = 
    
    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!
    

    You could also say doSomething replicateM_ 5 which only works if your doSomething is an idiomatic value (see Applicative). It has better type-safety, since you can do this:

    scala> putStrLn("Foo") replicateM_ 5
    res6: scalaz.effects.IO[Unit] = scalaz.effects.IO$$anon$2@8fe8ee7
    

    but not this:

    scala> { System.exit(0) } replicateM_ 5
    :15: error: value replicateM_ is not a member of Unit
    

    Let me see you pull that off in Ruby.

提交回复
热议问题