0Pricing
Clojure Functional Programming & JVM Backend Development · Lekcja

Leniwe sekwencje i nieskończone strumienie

Opanuj leniwe sekwencje Clojure, aby modelować nieskończone strumienie, odraczać obliczenia i wydajnie przetwarzać dane bez przepełniania pamięci.

Leniwe sekwencje i nieskończone strumienie to bezpłatna lekcja Clojure Functional Programming & JVM Backend Development na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Clojure Functional Programming & JVM Backend Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

What Is Laziness?

A lazy sequence computes its elements only as they are needed. This lets you describe potentially infinite data and consume just the part you want.

An Infinite Range

range with no arguments returns an infinite lazy sequence. It never materializes fully; you take only what you need.

(take 5 (range))
; => (0 1 2 3 4)

iterate

iterate builds an infinite sequence by repeatedly applying a function to a starting value.

(take 5 (iterate (fn [x] (* x 2)) 1))
; => (1 2 4 8 16)

repeat and cycle

repeat yields the same value forever; cycle loops a collection forever. Both are lazy.

(take 3 (repeat :x))   ; => (:x :x :x)
(take 5 (cycle [1 2])) ; => (1 2 1 2 1)

lazy-seq

You build your own lazy sequences with lazy-seq. The body runs only when the next element is requested.

(defn nums-from [n]
  (lazy-seq
    (cons n (nums-from (inc n)))))

(take 4 (nums-from 10)) ; => (10 11 12 13)

Lazy Transformations

map, filter, and take-while are all lazy. Chaining them does no work until you consume the result.

(->> (range)
     (map (fn [x] (* x x)))
     (filter even?)
     (take 4))
; => (0 4 16 36)

Realization & Chunking

Lazy seqs are realized in chunks of 32 for efficiency. So asking for one element may compute up to 32. Keep transformations side-effect-free to avoid surprises.

(first (map (fn [x] (println "seen" x) x) (range 100)))
; prints 0..31 due to chunking

Forcing Realization

Use doall to fully realize a lazy sequence (e.g. to trigger side effects), or dorun when you do not need the results.

(doall (map println [1 2 3]))

Avoiding the Head-Holding Trap

If you keep a reference to the head of an infinite seq while walking it, the whole realized portion stays in memory. Let go of the head to allow garbage collection.

; Bad: binding holds the head
(let [s (range)] (last (take 1000000 s)))

Practical Stream: Fibonacci

Lazy sequences elegantly express recursive streams like Fibonacci numbers.

(def fibs
  (map first
    (iterate (fn [[a b]] [b (+ a b)]) [0 1])))

(take 7 fibs) ; => (0 1 1 2 3 5 8)

Laziness vs Transducers

Lazy seqs build intermediate sequences; transducers (from a prior lesson) avoid them. Use laziness for infinite/streamed data and transducers for high-throughput pipelines.

Quick Check

Test your lazy-sequence intuition.

Recap

You learned how lazy sequences defer computation and model infinite streams.

  • Generate with range, iterate, repeat, cycle, lazy-seq
  • map/filter stay lazy until consumed
  • Beware chunking and head-holding

Często zadawane pytania

Czy lekcja „Leniwe sekwencje i nieskończone strumienie” jest bezpłatna?

Tak — pełny tekst „Leniwe sekwencje i nieskończone strumienie” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Clojure Functional Programming & JVM Backend Development, przejdź na CoddyKit PRO. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.

Co nauczysz się w „Leniwe sekwencje i nieskończone strumienie”?

Opanuj leniwe sekwencje Clojure, aby modelować nieskończone strumienie, odraczać obliczenia i wydajnie przetwarzać dane bez przepełniania pamięci. Ćwiczysz Clojure Functional Programming & JVM Backend Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Clojure Functional Programming & JVM Backend Development?

Nie wymagamy żadnego doświadczenia. Clojure Functional Programming & JVM Backend Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Leniwe sekwencje i nieskończone strumienie”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Clojure Functional Programming & JVM Backend Development?

Tak. Każda lekcja Clojure Functional Programming & JVM Backend Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Transducery do wydajnego przetwarzania
  2. Monady i abstrakcje funkcyjne
  3. Testowanie oparte na właściwościach z clojure.test.check
  4. Leniwe sekwencje i nieskończone strumienie
← Powrót do Clojure Functional Programming & JVM Backend Development