By Alvin Alexander. Last updated: July 16, 2022
As a quick note, I was just reminded that you can populate a Scala List
using a Range
, like this:
scala> (1 to 5).toList
res0: List[Int] = List(1, 2, 3, 4, 5)
scala> (1 to 10 by 2).toList
res1: List[Int] = List(1, 3, 5, 7, 9)
scala> (5 to 11).toList
res2: List[Int] = List(5, 6, 7, 8, 9, 10, 11)
scala> ('d' to 'h').toList
res3: List[Char] = List(d, e, f, g, h)
Those are just a few examples. For many more ways to populate Scala lists with sample data, see How to populate Scala collections with a Range, How to generate random numbers, characters, and sequences in Scala, and Different ways to create and populate Lists in Scala.