Start on lazy seqs.
This commit is contained in:
parent
717207339f
commit
7d9fb046bb
25
src/koans/lazy_sequences.clj
Normal file
25
src/koans/lazy_sequences.clj
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
(meditations
|
||||||
|
"There are a wide range of ways to generate a sequence"
|
||||||
|
(= __ (range 1 5))
|
||||||
|
|
||||||
|
"The range starts at the beginning by default"
|
||||||
|
(= __ (range 5))
|
||||||
|
|
||||||
|
"It's important to only take what you need from a big sequence"
|
||||||
|
(= [0 1 2 3 4 5 6 7 8 9]
|
||||||
|
(take __ (range 100)))
|
||||||
|
|
||||||
|
"You can also limit results by dropping what you don't need"
|
||||||
|
(= [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))))
|
@ -90,6 +90,14 @@
|
|||||||
"Hello, you silly world."
|
"Hello, you silly world."
|
||||||
"Hello to this group: Peter, Paul, Mary!" ]}
|
"Hello to this group: Peter, Paul, Mary!" ]}
|
||||||
|
|
||||||
|
"lazy_sequences" {"__" [[1 2 3 4]
|
||||||
|
[0 1 2 3 4]
|
||||||
|
10
|
||||||
|
95
|
||||||
|
'(range 20)
|
||||||
|
:a]
|
||||||
|
"___" ['(fn [x] :foo)]}
|
||||||
|
|
||||||
"sequence_comprehensions" {"__" [[0 1 2 3 4 5]
|
"sequence_comprehensions" {"__" [[0 1 2 3 4 5]
|
||||||
'(* index index)
|
'(* index index)
|
||||||
'(range 10)
|
'(range 10)
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
"conditionals"
|
"conditionals"
|
||||||
"higher_order_functions"
|
"higher_order_functions"
|
||||||
"runtime_polymorphism"
|
"runtime_polymorphism"
|
||||||
|
"lazy_sequences"
|
||||||
"sequence_comprehensions"
|
"sequence_comprehensions"
|
||||||
"creating_functions"
|
"creating_functions"
|
||||||
"recursion"
|
"recursion"
|
||||||
|
Loading…
Reference in New Issue
Block a user