Clojure Koans 21 Partition Notebook
As always, Clojure Koans exercise spoilers are below.
Original Clojure Koans repository: https://github.com/functional-koans/clojure-koans/
Let us parti-tion! ;)
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
{:hello (clojure-version)}
Initial Thoughts
Splitting and partitioning in code can be like splitting hairs - each language has their own particular way of doing it, and the differences can be subtle. I'm hoping to find that Clojure's approach is simple and intuitive.
;; "To split a collection you can use the partition function"
(= ((0 1) (2 3)) (partition 2 (range 4)))
;; skill 21001: Split a collection into segments of length/size N by using the `partition` function
;; "But watch out if there are not enough elements to form n sequences"
(= ((:a :b :c)) (partition 3 [:a :b :c :d :e]))
;; "You can use partition-all to include any leftovers too"
(= ((0 1 2) (3 4)) (partition-all 3 (range 5)))
;; skill 21002: Split a collection in segments of N, where any final remaining items are also included in a smaller segment, by using the `partition-all` function (?)
;; "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)))
;; skill 21003: Create segments from a collection **with an offset** by using the `partition` function with an extra "offset" argument.
;; "Consider padding the last sequence with some default values..."
(= ((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [:hello] (range 7)))
;; skill 21004: Add padding to a partion's last sequence when it is not the full size by supplying a collection (sequence? vector?) of things to pad with
;; "... but notice that they will only pad up to the given sequence length"
(= ((0 1 2) (3 4 5) (6 :these :are)) (partition 3 3 [:these :are "my" "words"] (range 7)))
;; description: Partition the numbers 0 through 6 into segments of length 3, where any segments of less than length 3 will be padded with items from the vector `[:these :are "my" "words"]` until the segment has reached the partion size of 3
;; question 21001: Can a lazy sequence be used to completey/fully pad "non-full" partions, and if so, how exactly would it look? (show code example)
Closing Thoughts
So far, we've been exposed to `partition` and `partition-all` and I believe there is more to explore. Does partition behave differently for strings, lists, vectors, and maps? I would guess that it doesn't, so more investigation is required.
Do you know of any functions that are partition-like, or are built from direct use of the partition functions?
On another note, I think it might be fun to showcase some of the variation between Python, JavaScript, and Clojure. Since today is day 21, there are only 6 more days before the Clojure Koans are finished and I'll be bringing 3 days of custom (non-Clojure Koans related) content.