Kevin Meredith
Kevin Meredith

Reputation: 41909

Creating a List of Range

I'm trying to get List(0,1,2,...n)

Is there a cleaner/better way than:

scala> List(0 to 9)
res0: List[scala.collection.immutable.Range.Inclusive] = List(Range(0, 1, 2, 3, 4, 
                                                                   5, 6, 7, 8, 9))

scala> List(0 to 9).flatten
res1: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Upvotes: 0

Views: 198

Answers (3)

0__
0__

Reputation: 67290

Also

List(0 to 9: _*)

I suspect though that List.range is the most efficient one.

Upvotes: 1

Seth Tisue
Seth Tisue

Reputation: 30453

scala> List.range(0, 10)
res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Upvotes: 6

Dan Gallagher
Dan Gallagher

Reputation: 1022

The best way might be:

(0 to 9).toList

Upvotes: 8

Related Questions