io non riesco a trovare un ottimo modo per esprimere quanto segue in Xtend senza ricorrere a un ciclo while:XTend For-loop Supporto e aggiunta di supporto Gamma
for(int i = 0; i < 3; i++){
println("row ");
}
println("your boat");
Quindi, credo che la mia domanda ha due parti:
- C'è un modo migliore per fare quanto sopra? Non ho visto nulla di promettente nel loro documenation
- Una gran parte delle funzionalità del linguaggio sono solo estensioni della libreria Xtend (e sono grandiose!). Esiste la funzionalità
range()
alla Python che non conosco?
ho finito rotazione mia e ottenuto qualcosa di simile al seguente:
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
}
}
Si può anche evitare di scrivere 'Integer' perché è automagicamente dedotte da Xtend. –