11

First day and first attempt at using Scala - so go easy on me! I'm trying to rewrite some old Java code I have which is simply a function which takes two numbers and prints out the numbers from x to y. For example, i have the increment function:

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

However, im struggling writting a corresponding decrement function which will decrease from start to finish? I have read Scala downwards or decreasing for loop? but am still unsure

Thank you

Community
  • 1
  • 1
rwb
  • 4,309
  • 8
  • 36
  • 59

8 Answers8

18
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
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • Perfect thanks. I now have no idea how i didnt see that from http://stackoverflow.com/questions/2617513/scala-downwards-or-decreasing-for-loop?answertab=votes#tab-top Definitely not thinking straight! – rwb Apr 02 '12 at 13:07
  • One more thing - sorry. How would i get the value of the iterator in the println? – rwb Apr 02 '12 at 13:08
  • `start to finish by -1` will return a `Range` which you can iterate over. – dacwe Apr 02 '12 at 13:11
  • Is there anyway to write this function in functional form? – rwb Apr 02 '12 at 13:42
  • 1
    `def dec(start: Int, finish: Int) = { start to finish by -1 }; dec(10, 1).foreach( i => println("decrementing " + i) );` – dacwe Apr 02 '12 at 14:15
  • @Ryan functional: def dec(from:Int, to:Int):Int = if( from > to) dec(from-1,to) else to (recursion is 'functional' you get the idea – AndreasScheinert Apr 02 '12 at 16:05
6
for (i <- (6 to 3 by -1)) {println ("i: " + i)}
i: 6
i: 5
i: 4
i: 3

If you happen to forget by -1, you can move up and use a function, to revert the result:

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

To exclude the second boundary, use until:

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

Alternatively, you could define an Iterator for your purpose. Extending an Iterator is easy; just implement 'hasNext:Boolean' and 'Next:[T]', where T is the type to handle - in our case Int or maybe Long or 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)
user unknown
  • 35,537
  • 11
  • 75
  • 121
2

This way you can use Decreasing for loop in 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
Nilesh Shinde
  • 457
  • 5
  • 10
1

highnum to lownum by -1 (switch with other negative or positive step to change stepping)

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

I think this is a dupe of Scala downwards or decreasing for loop?

Community
  • 1
  • 1
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
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)
}

This is also a method. but may not be the best

Tunaki
  • 132,869
  • 46
  • 340
  • 423
StandForTheRight
  • 135
  • 3
  • 14
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")
   }
}

method call:

printInDecreasingOrder(10, 2)

Result:

Current value (decreasing from 10 to 2) is 10

Current value (decreasing from 10 to 2) is 9

Current value (decreasing from 10 to 2) is 8

Current value (decreasing from 10 to 2) is 7

Current value (decreasing from 10 to 2) is 6

Current value (decreasing from 10 to 2) is 5

Current value (decreasing from 10 to 2) is 4

Current value (decreasing from 10 to 2) is 3

Current value (decreasing from 10 to 2) is 2

printInDecreasingOrder(1, 10)

Result:

first num is smaller than second

Azam Khan
  • 516
  • 5
  • 12
0

A point to add to this answer, if you do something like this

for(j <- finish to start){......}

sbt does not even show the error. So if you want a decrementing for loop you need to do this

for(j <- finish to start by -1){.......}
CV_Ruddha
  • 406
  • 2
  • 13
0

Here is a global increment/decrement solution inspired by 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)
  } 
}
Community
  • 1
  • 1
Christopher Chiche
  • 15,075
  • 9
  • 59
  • 98