clojure-koans/src/koans/10_lazy_sequences.clj

26 lines
634 B
Clojure
Raw Normal View History

2011-03-31 13:11:27 +00:00
(meditations
2013-02-26 02:46:19 +00:00
"There are many ways to generate a sequence"
2011-03-31 13:11:27 +00:00
(= __ (range 1 5))
"The range starts at the beginning by default"
(= __ (range 5))
2013-02-26 02:46:19 +00:00
"Only take what you need when the sequence is large"
2011-03-31 13:11:27 +00:00
(= [0 1 2 3 4 5 6 7 8 9]
(take __ (range 100)))
2013-02-26 02:46:19 +00:00
"Or limit results by dropping what you don't need"
2011-03-31 13:11:27 +00:00
(= [95 96 97 98 99]
(drop __ (range 100)))
"Iteration provides an infinite lazy sequence"
(= __ (take 20 (iterate inc 0)))
"Repetition is key"
(= [:a :a :a :a :a :a :a :a :a :a ]
(repeat 10 __))
"Iteration can be used for repetition"
(= (repeat 100 :foo)
(take 100 (iterate ___ :foo))))