virtualeyes
virtualeyes

Reputation: 11237

How to iterate over range with negative step?

Is there a way to do something like:

0 to -10 map { i=>
...
}

repl gives me:

scala.collection.immutable.IndexedSeq[Unit] = Vector()

Upvotes: 14

Views: 9488

Answers (2)

dbyrne
dbyrne

Reputation: 61041

Add the by clause:

0 to -10 by -1
res0: Range(0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10)

Upvotes: 19

Arjan
Arjan

Reputation: 21465

0 to (-10, -1)

or

0 to -10 by -1

Upvotes: 49

Related Questions