How Does One Make Scala Control Abstraction in Repeat Until?

后端 未结 4 956
挽巷
挽巷 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:31

    As above yet recursive :)

    def repeat(b: => Unit) = new {def until(c: => Boolean) = { b; if (c) until(c) }}
    
    var i = 0
    repeat {
      println(i)
      i+=1
    } until (i < 10)
    

    It's @tailrec optimized too.

    Llove scala :)

提交回复
热议问题