如果不诉诸while循环,我似乎找不到一种在Xtend中表达以下内容的好方法:

for(int i = 0; i < 3; i++){
    println("row ");
}
println("your boat");

因此,我想我的问题分为两个部分:
  • 是否有更好的方法来执行上述操作?他们的documenation
  • 没什么前途的
  • 该语言具有的大部分功能只是Xtend库扩展(它们很棒!)。我是否不了解Python的range()功能?

  • 我最终自己滚动了,得到了如下内容:

    class LanguageUtil {
    
    def static Iterable<Integer> range(int stop) {
        range(0, stop)
    }
    
    def static Iterable<Integer> range(int start, int stop) {
        new RangeIterable(start, stop, 1)
    }
    
    def static Iterable<Integer> range(int start, int stop, int step) {
        new RangeIterable(start, stop, step)
    }
    }
    
    // implements Iterator and Iterable which is bad form.
    class RangeIterable implements Iterator<Integer>, Iterable<Integer> {
    val int start
    val int stop
    val int step
    var int current
    
    new(int start, int stop, int step) {
        this.start = start;
        this.stop = stop;
        this.step = step
        this.current = start
    }
    
    override hasNext() {
        current < stop
    }
    
    override next() {
        val ret = current
        current = current + step
        ret
    }
    
    override remove() {
        throw new UnsupportedOperationException("Auto-generated function stub")
    }
    
    /**
     * This is bad form. We could return a
     * new RangeIterable here, but that seems worse.
     */
    override iterator() {
        this
    }
    }
    

    最佳答案

    的确切替代品

    for(int i = 0; i < 3; i++){
        println("row ");
    }
    


    for (i : 0 ..< 3) {
        println("row ")
    }
    

    注意独占范围运算符:..<

    关于for-loop - XTend循环支持和添加范围支持,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12126999/

    10-10 14:54