clojure-koans/src/koans/21_partition.clj

22 lines
859 B
Clojure
Raw Normal View History

(ns koans.21-partition
(:require [koan-engine.core :refer :all]))
2012-08-04 10:11:33 +00:00
(meditations
"To split a collection you can use the partition function"
2023-01-04 13:43:12 +00:00
(= '((0 1) (2 3)) (partition 2 (range 4)))
2012-08-04 10:11:33 +00:00
2013-02-26 02:46:19 +00:00
"But watch out if there are not enough elements to form n sequences"
2023-01-04 13:43:12 +00:00
(= '([:a :b :c]) (partition 3 [:a :b :c :d :e]))
2012-08-04 10:11:33 +00:00
"You can use partition-all to include any leftovers too"
2023-01-04 13:43:12 +00:00
(= '((0 1 2) (3 4)) (partition-all 3 (range 5)))
2012-08-04 10:11:33 +00:00
"If you need to, you can start each sequence with an offset"
2023-01-04 13:43:12 +00:00
(= '((0 1 2) (5 6 7) (10 11 12)) (partition 3 5 (range 13)))
2012-08-04 10:11:33 +00:00
2013-11-18 17:29:13 +00:00
"Consider padding the last sequence with some default values..."
2023-01-04 13:43:12 +00:00
(= '((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [:hello] (range 7)))
2012-08-04 10:11:33 +00:00
2013-11-18 17:29:13 +00:00
"... but notice that they will only pad up to the given sequence length"
2023-01-04 13:43:12 +00:00
(= '((0 1 2) (3 4 5) (6 :these :are)) (partition 3 3 [:these :are "my" "words"] (range 7))))