2015-09-13 05:28:39 +00:00
|
|
|
(ns koans.11-lazy-sequences
|
2014-05-02 21:37:11 +00:00
|
|
|
(:require [koan-engine.core :refer :all]))
|
2014-01-26 00:04:22 +00:00
|
|
|
|
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"
|
2016-04-11 03:25:56 +00:00
|
|
|
(= '(1 2 3 4) (range 1 5))
|
2011-03-31 13:11:27 +00:00
|
|
|
|
|
|
|
|
"The range starts at the beginning by default"
|
2016-04-11 03:25:56 +00:00
|
|
|
(= '(0 1 2 3 4) (range 5))
|
2011-03-31 13:11:27 +00:00
|
|
|
|
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]
|
2016-04-11 03:25:56 +00:00
|
|
|
(take 10 (range 100)))
|
2011-03-31 13:11:27 +00:00
|
|
|
|
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]
|
2016-04-11 03:25:56 +00:00
|
|
|
(drop 95 (range 100)))
|
2011-03-31 13:11:27 +00:00
|
|
|
|
|
|
|
|
"Iteration provides an infinite lazy sequence"
|
2016-04-11 03:25:56 +00:00
|
|
|
(= '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19) (take 20 (iterate inc 0)))
|
2011-03-31 13:11:27 +00:00
|
|
|
|
|
|
|
|
"Repetition is key"
|
2015-07-19 22:55:37 +00:00
|
|
|
(= [:a :a :a :a :a :a :a :a :a :a]
|
2016-04-11 03:25:56 +00:00
|
|
|
(repeat 10 :a))
|
2011-03-31 13:11:27 +00:00
|
|
|
|
|
|
|
|
"Iteration can be used for repetition"
|
2015-06-15 04:35:01 +00:00
|
|
|
(= (repeat 100 :hello)
|
2016-04-11 03:25:56 +00:00
|
|
|
(take 100 (iterate identity :hello))))
|