Ranges - Squeng/Polyglot GitHub Wiki

In Scala, a Range is an ordered sequence of integers that are equally spaced apart.

val nineToFive = 9 to 17
println(nineToFive.mkString(", ")) // 9, 10, 11, 12, 13, 14, 15, 16, 17
val officeHours = 9 until 17
println(officeHours.mkString(", ")) // 9, 10, 11, 12, 13, 14, 15, 16 
val openDoor = 9 until 17 by 2
println(openDoor.mkString(", ")) // 9, 11, 13, 15

In Java, IntStreams and LongStreams can be instatiated as ranges with step 1.

var nineToFive = IntStream.rangeClosed(9, 17);
System.out.println(nineToFive.mapToObj(i -> Integer.toString(i)).collect(Collectors.joining(", "))); // 9, 10, 11, 12, 13, 14, 15, 16, 17
var officeHours = IntStream.range(9, 17);
System.out.println(officeHours.mapToObj(i -> Integer.toString(i)).collect(Collectors.joining(", "))); // 9, 10, 11, 12, 13, 14, 15, 16

In Python, the built-in range() function generates an ordered sequence of integers that are equally spaced apart.

nineToFive = range(9, 17+1)
print(", ".join(f"{i}" for i in nineToFive)) # 9, 10, 11, 12, 13, 14, 15, 16, 17
officeHours = range(9, 17)
print(", ".join(f"{i}" for i in officeHours)) # 9, 10, 11, 12, 13, 14, 15, 16
openDoor = range(9, 17, 2)
print(", ".join(f"{i}" for i in openDoor)) # 9, 11, 13, 15