-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_lazy_sequences.clj
More file actions
40 lines (30 loc) · 1.21 KB
/
11_lazy_sequences.clj
File metadata and controls
40 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns koans.11-lazy-sequences
(:require [koan-engine.core :refer :all]))
(meditations
"There are many ways to generate a sequence"
(= '(1 2 3 4) (range 1 5))
"The range starts at the beginning by default"
(= '(0 1 2 3 4) (range 5))
"Only take what you need when the sequence is large"
(= [0 1 2 3 4 5 6 7 8 9]
(take 10 (range 100)))
"Or limit results by dropping what you don't need"
(= [95 96 97 98 99]
(drop 95 (range 100)))
"Iteration provides an infinite lazy sequence"
(= '(1 2 4 8 16 32 64 128) (take 8 (iterate (fn [x] (* x 2)) 1)))
"Repetition is key"
(= [:a :a :a :a :a :a :a :a :a :a]
(repeat 10 :a))
"Iteration can be used for repetition"
;; NOTE: A valid and possible solution is provided below,
;; but I think here the intended goal is to use the
;; identity function instead - likely to familiarize
;; a new Clojurian to the notion that the parameter
;; to be recieved by the function provided to
;; iterate ([fn x]) may take (and often does)
;; the second parameter, x.
;; (= (repeat 100 "hello")
;; (take 100 (iterate (fn [_] "hello") "hello")))
(= (repeat 100 "hello")
(take 100 (iterate (fn [x] x) "hello"))))