-
Notifications
You must be signed in to change notification settings - Fork 0
/
Partition
27 lines (14 loc) · 861 Bytes
/
Partition
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
ClojureScript Koans Partition Answers
http://clojurescriptkoans.com/#partition/1
1)To split a collection you can use the partition function
(= '((0 1) (2 3)) (partition 2 (range 4)))
2)But watch out if there are not enough elements to form n sequences
(= '( (:a :b :c) ) (partition 3 [:a :b :c :d :e]))
3)You can use partition-all to also get partitions with less than n elements
(= '((0 1 2)(3 4)) (partition-all 3 (range 5)))
4)If you need to, you can start each sequence with an offset
(= '((0 1 2) (5 6 7) (10 11 12)) (partition 3 5 (range 13)))
5)Consider padding the last sequence with some default values..
(= '((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [ :hello ] (range 7)))
6).. but notice that they will only pad up to given sequence length
(= '( (0 1 2) (3 4 5) (6 :this :are) ) (partition 3 3 [:this :are "my" "words"] (range 7)))