Construire des plages
Générez des suites de nombres.
Construire des plages est une leçon Scala for Backend Engineering & Functional Programming gratuite sur CoddyKit. Ceci est la leçon 3 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Scala for Backend Engineering & Functional Programming, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
What Is a Range?
A range represents an ordered sequence of evenly spaced integers, like 1, 2, 3, 4, 5.
Ranges are memory efficient because they store only the start, end, and step, not every number. They are ideal for counting and looping.
The to Keyword
The simplest range uses to, which includes both endpoints.
So 1 to 5 covers 1, 2, 3, 4, and 5. The final number is part of the range.
val r = 1 to 5
println(r)Inclusive Ranges
A to range is called inclusive because it contains its upper bound.
To see the actual numbers rather than the compact description, you can convert the range to a list.
val r = 1 to 5
println(r.toList)The until Keyword
Use until when you want to stop just before the upper bound.
So 1 until 5 gives 1, 2, 3, 4 and leaves out 5. This is exclusive of the end value.
val r = 1 until 5
println(r.toList)to versus until
The difference is just the last number. to includes the end, until excludes it.
1 to 5 has five elements, while 1 until 5 has four. Choosing the right one avoids off-by-one bugs.
println((1 to 5).toList)
println((1 until 5).toList)Stepping with by
By default a range steps by one. Add by to change the step size.
Here we count from 0 to 10 in jumps of 2, producing the even numbers.
val evens = 0 to 10 by 2
println(evens.toList)Counting Down
A negative step lets a range count backward.
With by -1, the range starts high and decreases. This is the standard way to iterate in reverse.
val down = 5 to 1 by -1
println(down.toList)Range Size
You can ask a range how many elements it contains with size.
This is computed instantly from the bounds and step, without listing every number.
val r = 0 to 20 by 5
println(r.size)Checking Membership
The contains method tells you whether a value falls inside a range.
It respects the step too, so 3 is not contained in a step-2 range of even numbers.
val evens = 0 to 10 by 2
println(evens.contains(4))
println(evens.contains(3))Ranges Are Collections
A range behaves like any other sequence, so you can map or sum over it.
Here we square each number and add the results, all without building an explicit list first.
val total = (1 to 4).map(n => n * n).sum
println(total)Empty Ranges
If the start is already past the end, the range is simply empty.
For example 5 until 5 contains nothing. Empty ranges are valid and just produce no elements when looped.
val r = 5 until 5
println(r.isEmpty)Quick Check
Test what you learned about building ranges.
Recap
You build ranges with to (inclusive) and until (exclusive), adjust the step with by, and count down with a negative step.
Ranges are lightweight collections that support size, contains, and map. Next you will use them to drive loops.
Questions Fréquemment Posées
La leçon « Construire des plages » est-elle gratuite ?
Oui — le texte complet de « Construire des plages » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Scala for Backend Engineering & Functional Programming, passe à CoddyKit PRO. Le cours Scala for Backend Engineering & Functional Programming comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Construire des plages » ?
Générez des suites de nombres. Tu pratiques Scala for Backend Engineering & Functional Programming avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Scala for Backend Engineering & Functional Programming ?
Aucune expérience préalable n'est requise. Scala for Backend Engineering & Functional Programming sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 3 sur 4.
Combien de temps prend la leçon « Construire des plages » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Scala for Backend Engineering & Functional Programming ?
Oui. Chaque leçon Scala for Backend Engineering & Functional Programming inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Créer des tuples
- Décomposer des tuples
- Construire des plages
- Plages dans les boucles