Range, a Scala language concept
Last updated
The (1 to n)
syntax produces a "Range" which is a representation of a sequence of numbers.
In Functional Programming languages, a representation often refers to the fact that evaluation of the thing is not done immediately, bringing performance benefits.
For example, `(1 to n)` is just a structure that literally says "1 to n". Only when you convert it to a list, or "force" it, does it go through the full range.
From algorithm perspective, this means that you can perform complex yet efficient computations in a fraction of code compared to other languages which all perform eager evaluation.
Also refer to: View
What else is great is that because Range is different from other collections, it provides many useful specialised methods like .sum which may perform in O(1). (worth seeing examples in Stack Safety).