How Does One Make Scala Control Abstraction in Repeat Until?

后端 未结 4 957
挽巷
挽巷 2021-02-08 12:04

I am Peter Pilgrim. I watched Martin Odersky create a control abstraction in Scala. However I can not yet seem to repeat it inside IntelliJ IDEA 9. Is it the IDE?



        
4条回答
  •  无人共我
    2021-02-08 12:25

    How about a one liner for repeat until.

    def repeat(b: => Unit) = new AnyRef {def until(c: => Boolean) {b; while (! c) b}}
    

    Which, for example, gives:-

    scala> repeat {
         |   println("i = "+i)
         |   i+=1
         | } until (i >= 10)
    i = 0
    i = 1
    i = 2
    i = 3
    i = 4
    i = 5
    i = 6
    i = 7
    i = 8
    i = 9
    

提交回复
热议问题