20. Partition - Koans Walkthrough

This video introduces the partition function, which is a powerful function for partitioning a lazy sequence into groups of lists, of any size. You can also specify a step value (or offset) that lets you easily get sliding-window functionality for free.

Here is the 20_partition.clj source code:

  "To split a collection you can use the partition function"
  (= '((0 1) (2 3)) (__ 2 (range 4)))

  "But watch out if there are not enough elements to form n sequences"
  (= '(__) (partition 3 [:a :b :c :d :e]))

  "You can use partition-all to also get partitions with less then n elements"
  (= __ (partition-all 3 (range 5)))

  "If you need to, you can start each sequence with an offset"
  (= '((0 1 2) (5 6 7) (10 11 12)) (partition 3 __ (range 13)))

  "Consider padding the last sequence with some default values.."
  (= '((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [__] (range 7)))

  ".. but notice that they will only pad up to given sequence length"
  (= '((0 1 2) (3 4 5) __) (partition 3 3 [:this :are "my" "words"] (range 7))))

Clojure Koans Walkthrough in Light Table IDE

This screencast tutorial helps you learn the Clojure programming language. Experience the joy of Clojure in the Light Table IDE as we tour through the Clojure Koans, taking you all the way from Beginner to Intermediate to Advanced.

Clojure is a Lisp created by Rich Hickey that runs on the JVM, as an alternative to Java. ClojureScript can target the web browser environment, and node.js, by compiling down to JavaScript, using the Google Closure compiler. Clojure features immutability, functional programming, and being a Lisp, macros.