Lazy Evaluation erklärt
Berechnen Sie Werte erst bei Bedarf.
Lazy Evaluation erklärt ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
What Is Laziness?
In Scala, most values are strict: they are computed the moment you define them. Laziness flips this. A lazy value is only computed when it is first needed, never before.
This matters when computations are expensive, may fail, or might never be used at all. Why pay for work nobody asks for?
Strict val
A normal val evaluates its right-hand side immediately, even if you never read it again.
Run this and notice that "computing" prints before "after". The work happens at definition time.
object Demo extends App {
val x = { println("computing"); 42 }
println("after")
println(x)
}lazy val
Prefix a val with lazy and evaluation is deferred until first access.
Here "after" prints first, then "computing" only when x is read. The result is also cached: the body runs at most once.
object Demo extends App {
lazy val x = { println("computing"); 42 }
println("after")
println(x)
println(x)
}By-name Parameters
A parameter declared as => A is passed by name: the argument is not evaluated until used inside the method.
This is the building block of laziness. LazyList uses by-name parameters so a tail is never computed until requested.
def maybe(cond: Boolean, value: => Int): Int =
if (cond) value else 0
val r = maybe(false, { println("side"); 99 })
// "side" never prints; value was unusedMeet LazyList
LazyList is Scala's lazy sequence (it replaced the older Stream in 2.13+). Its head is evaluated eagerly, but its tail is lazy.
Elements are produced on demand and memoized once computed, so you never recompute the same cell twice.
val ll = LazyList(1, 2, 3)
println(ll)
// prints LazyList(<not computed>) until forcedForcing Evaluation
A LazyList stays unevaluated until you force it. Calling toList, foreach, or printing each element forces the cells.
The REPL shows <not computed> for the lazy tail. Forcing reveals the real values.
object Demo extends App {
val ll = LazyList(1, 2, 3)
println(ll.toList)
}The #:: Operator
You prepend to a LazyList with #::. Unlike list's ::, the right operand of #:: is by-name, so the tail is not built until needed.
This lets you describe sequences whose rest is computed lazily.
val ll = 1 #:: 2 #:: 3 #:: LazyList.empty
println(ll.head)Memoization
Once a LazyList cell is computed, its value is stored. Reading it again is free and the producing expression does not run twice.
Run this: "calc" prints only on first access of each forced element, not on repeated reads.
object Demo extends App {
val ll = LazyList(1, 2).map { n =>
println("calc " + n); n * 10
}
println(ll.head)
println(ll.head)
}Lazy Tail in Action
Because the tail is lazy, you can map over a LazyList and only the elements you touch get computed.
Here "map" prints just once even though the list has three elements, because only the head is forced.
object Demo extends App {
val r = LazyList(1, 2, 3).map { n =>
println("map " + n); n + 1
}
println(r.head)
}Why Laziness Helps
Laziness enables three things: avoiding unused work, building infinite sequences, and decoupling how you describe a computation from how much of it runs.
You write the full pipeline once; the consumer decides how many elements to pull.
A Caution
Laziness has costs. A LazyList memoizes, so holding a reference to its head keeps every forced element in memory. For huge or infinite data, avoid retaining the head.
Also, side effects in lazy code run at unexpected times, which can confuse debugging.
Quick Check
Test your understanding of lazy evaluation.
Recap
Strict evaluation runs work immediately; lazy evaluation defers it until needed. lazy val and by-name parameters (=> A) are the primitives.
LazyList builds on them: eager head, lazy memoized tail, forced via toList or element access. Next, we build LazyLists by hand.
Häufig gestellte Fragen
Ist die Lektion „Lazy Evaluation erklärt“ kostenlos?
Ja — der vollständige Text von „Lazy Evaluation erklärt“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Lazy Evaluation erklärt“?
Berechnen Sie Werte erst bei Bedarf. Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?
Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Lazy Evaluation erklärt“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?
Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Lazy Evaluation erklärt
- Eine LazyList erstellen
- Unendliche Streams
- Lazy entnehmen und filtern