2012-04-02 22 views
7

Primo giorno e primo tentativo di utilizzo di Scala - quindi, non esitare! Sto cercando di riscrivere un vecchio codice Java che è semplicemente una funzione che prende due numeri e stampa i numeri da x a y. Ad esempio, ho la funzione di minimo:Diminuendo per loop in Scala?

def increment(start: Int, finish: Int) = { 
     for (i <- start to finish) { 
     println("Current value (increasing from "+start+" to "+finish+") is "+i) 
     } 
    } 

Tuttavia, im prese scrivendo un corrispondente funzione di decremento che diminuirà dall'inizio alla fine? Ho letto Scala downwards or decreasing for loop? ma sono ancora incerti

Grazie

risposta

15
scala>def decrement(start: Int, finish: Int) = { 
    | for (i <- start to finish by -1) 
    | println("Current value (decreasing from "+start+" to "+finish+") is "+i); 
    | } 
decrement: (start: Int,finish: Int)Unit 

scala> decrement(10, 1) 
Current value (decreasing from 10 to 1) is 10 
Current value (decreasing from 10 to 1) is 9 
Current value (decreasing from 10 to 1) is 8 
Current value (decreasing from 10 to 1) is 7 
Current value (decreasing from 10 to 1) is 6 
Current value (decreasing from 10 to 1) is 5 
Current value (decreasing from 10 to 1) is 4 
Current value (decreasing from 10 to 1) is 3 
Current value (decreasing from 10 to 1) is 2 
Current value (decreasing from 10 to 1) is 1 
+0

Perfetto grazie. Ora non ho idea di come non l'ho visto da http://stackoverflow.com/questions/2617513/scala-downwards-or-decreasing-for-loop?answertab=votes#tab-top Sicuramente non sto pensando bene! – rwb

+0

@Ryan: Nessun problema! :) – dacwe

+0

Un'ultima cosa - scusa. Come potrei ottenere il valore dell'iteratore nel println? – rwb

4
for (i <- (6 to 3 by -1)) {println ("i: " + i)} 
i: 6 
i: 5 
i: 4 
i: 3 

Se vi capita di dimenticare by -1, è possibile spostare e utilizzare una funzione, per riportare il risultato:

for (i <- (3 to 6)) {println ("i: " + ((6+3) - i))} 

Per escludere il secondo limite, utilizzare until:

for (i <- (6 until 3 by -1)) {println ("i: " + i)} 
i: 6 
i: 5 
i: 4 

In alternativa, è possibile definire un iteratore per il proprio scopo. Estendere un iteratore è facile; basta implementare 'hasNext: booleano' e 'Next: [T]', dove T è il tipo da gestire - nel nostro caso Int o forse lungo o BigInt:

class FromToIterator (start: Int, stop: Int) extends Iterator [Int] { 
    var current = start 
    //      3  6  3   6   6  3  6   3 
    def hasNext : Boolean = ((start < stop && current <= stop) || (start > stop && current >= stop)) 
    def next: Int = { 
    val res = current 
    if (start < stop) current += 1 else current -= 1 
    res 
    } 
} 
val it = new FromToIterator (3, 6) 
val ti = new FromToIterator (6, 3) 

for (i <-it) println (i) 
for (i <-ti) println (i) 
0

Ecco un incremento globale/soluzione decremento ispira Scala downwards or decreasing for loop?:

def goThrough(start: Int, finish: Int) = {  
    val d = if(start<=finish) 1 else -1 
    for (i <- start to finish by d) { 
    println("Current value (increasing from "+start+" to "+finish+") is "+i) 
    } 
} 
1

highnum a lownum da -1 (interruttore con altra fase negativa o positiva per cambiare passo)

def decrement(start: Int, finish: Int) = { 
    for (i <- start to finish by -1) { 
    println("Current value (decreasing from "+start+" to "+finish+") is "+i) 
    } 
} 

Penso che questa sia una vittima di Scala downwards or decreasing for loop?

0
object Test extends App{ 

    def decrement(start: Int, finish: Int,dec :Int) = { 
    for (i <- Range(start,finish,dec)) { 
     println("Current value (decreasing from "+start+" to "+finish+") is "+i) 
    } 
    } 

    decrement(5,0,-1) 
} 

Questo è anche un metodo.ma non può essere la migliore chiamata

0
def printInDecreasingOrder(start : Int, end : Int){ 
    if(start > end){ 
    for(i <- start to end by -1){ 
     println(s"Current value (decreasing from $start to $end) is $i") 
    } 
    }else{ 
    println("first num is smaller than second") 
    } 
} 

metodo:

printInDecreasingOrder(10, 2)

risultati:

valore corrente (decrescente dal 10 a 2) è 10

valore corrente (decrescente da 10 a 2) è 9

Valore corrente (d ecreasing da 10 a 2) è 8

valore corrente (decrescente dal 10 a 2) è 7

valore corrente (decrescente dal 10 a 2) è 6

valore corrente (decrescente dal 10 a 2) è 5

valore corrente (decrescente dal 10 a 2) è 4

valore corrente (decrescente dal 10 a 2) è 3

valore attuale (Più basso a 10-2) è 2

printInDecreasingOrder(1, 10)

Risultato:

prima num è più piccolo di seconda

0

In questo modo è possibile utilizzare in diminuzione per il ciclo a Scala.

object Example extends App { 


     for(i <- 20 to 2 by -2){ 


     println("Value of i = "+ i) 

     } 
    } 
------------------ 
O/P 
------------------ 
Value of i = 20 
Value of i = 18 
Value of i = 16 
Value of i = 14 
Value of i = 12 
Value of i = 10 
Value of i = 8 
Value of i = 6 
Value of i = 4 
Value of i = 2