我似乎无法找到一种在 Xtend 中表达以下内容的好方法,而无需借助 while 循环:
for(int i = 0; i < 3; i++){
println("row ");
}
println("your boat");
所以,我想我的问题有两个部分:
- 有没有更好的方法来完成上述操作?我在他们的文档中没有看到任何有希望的东西
- 该语言的大部分功能只是 Xtend 库扩展(它们很棒!)。是否有
range()
我不知道的 Python 功能?
我最终自己滚动并得到了如下内容:
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
}
}