Avi Drucker / Apr 18 2021 / Published
Remix of Clojure by Nextjournal
Clojure Koans 11 Lazy Sequences Notebook
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure
Initial Thoughts
Foo bar baz
;; "There are many ways to generate a sequence"
(= (1 2 3 4) (range 1 5))
;; skill 11001: Generate a sequence from number X to number Y by using the `range` function
Clojure
;; "The range starts at the beginning by default"
(= (0 1 2 3 4) (range 5))
;; skill 11002: Generate a sequence starting at zero by using the `range` function
Clojure
;; "Only take what you need when the sequence is large"
(= [0 1 2 3 4 5 6 7 8 9]
(take 10 (range 100)))
;; skill 11003: Get the first N items of a sequence by using the `take` function
Clojure
;; "Or limit results by dropping what you don't need"
(= [95 96 97 98 99]
(drop 95 (range 100)))
;; skill 11004: Get all items of a sequence except for the first N items by using the `drop` function
Clojure
;; "Iteration provides an infinite lazy sequence"
(= (1 2 4 8 16 32 64 128) (take 8 (iterate (fn [x] (* x 2)) 1)))
;; skill 11005: Create a lazy sequence by using the `iterate` function
;; question 11001: How can the above skill be made more relevant and/or directly useful seeming?
;; question 11002: What exactly is a lazy sequence?
;; 11003 How does it differ/relate to a non-lazy sequence?
;; 11004 How are lazy sequences made/used?
;; 11005 Where are they best used/avoided?
Clojure
;; "Repetition is key"
(= [:a :a :a :a :a :a :a :a :a :a]
(repeat 10 :a))
;; skill 11006: Populate a sequence with a keyword using the `repeat` function
;; question 11006: How can the above skill be generalized further? suggestion: replace keyword with item/thing/element (?) ... TODO: see docs
Clojure
;; "Iteration can be used for repetition"
(= (repeat 100 "hello")
(take 100 (iterate (str %) "hello")))
;; question 11007: What are the differences between `repeat` and the combo of `take` and `iterate` (beyond just sytanx semantics)?
Clojure
Closing Thoughts
Foo bar baz