Skip to content

Commit

Permalink
lecture 05: add next and rest laziness comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
George Shopov committed Apr 16, 2014
1 parent 1f2a8f9 commit 2f1ae34
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lectures/05-lein-laziness.lecture
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,25 @@ Clojure връща мързеливи колекции винаги, когат
(when-let [s (seq coll)]
(cons (f (first s)) (map f (rest s))))))

= `next` vs `rest`

:code
(defn my-repeat
[x]
(lazy-seq
(println "realizing")
(cons x (my-repeat x))))

(def foo (next (my-repeat 42)))
; realizing
; realizing

(def bar (rest (my-repeat 42)))
; realizing

* `next` проверява дали опашката не е празна, а тази проверка изисква реализацията на главата на опашката
* `rest` просто връща опашката без да се интересува дали е празна, или не и затова избягва реализирането на главата

= `doall` и `dorun`

Насилствено реализираме цялата последователност
Expand Down

0 comments on commit 2f1ae34

Please sign in to comment.