# 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

``````assert((1 to 5).toString == "Range 1 to 5")

assert((1 to 5).reverse.toString() == "Range 5 to 1 by -1")

assert((1 to 5).toList == List(1, 2, 3, 4, 5))
``````

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).

# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

## How our 100 algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.

### Study our 100 Scala Algorithms: 6 fully free, 100 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 22 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.