Clojure Functional Programming & JVM Backend Development · บทเรียน

ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด

ทำความเข้าใจลำดับแบบขี้เกียจของ Clojure เพื่อจำลองสตรีมไม่สิ้นสุด เลื่อนการคำนวณ และประมวลผลข้อมูลอย่างมีประสิทธิภาพโดยไม่ใช้หน่วยความจำมากเกินไป

บทเรียน 4 จาก 413 ขั้นตอน

ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clojure Functional Programming & JVM Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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
เริ่มต้นได้ฟรี

เรียนรู้ Clojure ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clojure Functional Programming & JVM Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด”

ทำความเข้าใจลำดับแบบขี้เกียจของ Clojure เพื่อจำลองสตรีมไม่สิ้นสุด เลื่อนการคำนวณ และประมวลผลข้อมูลอย่างมีประสิทธิภาพโดยไม่ใช้หน่วยความจำมากเกินไป คุณปฏิบัติ Clojure Functional Programming & JVM Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Clojure Functional Programming & JVM Backend Development นี้ได้ไหม

ได้ บทเรียน Clojure Functional Programming & JVM Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทรานสดิวเซอร์เพื่อการประมวลผลอย่างมีประสิทธิภาพ
  2. โมนาดและนามธรรมเชิงฟังก์ชัน
  3. การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check
  4. ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด
← กลับไปที่ Clojure Functional Programming & JVM Backend Development