11. Sequence Comprehensions - Koans Walkthrough

This video covers sequence comprehensions, also known as list-comprehensions in most functional languages. Clojure has a "for" macro that provides a powerful way to create sequences from other sequences.

Here is the 11_sequence_comprehensions.clj source code:

(meditations
  "Sequence comprehensions can bind each element in turn to a symbol"
  (= __
     (for [index (range 6)]
       index))

  "They can easily emulate mapping"
  (= '(0 1 4 9 16 25)
     (map (fn [index] (* index index))
          (range 6))
     (for [index (range 6)]
       __))

  "And also filtering"
  (= '(1 3 5 7 9)
     (filter odd? (range 10))
     (for [index __ :when (odd? index)]
       index))

  "And they trivially allow combinations of the two transformations"
  (= '(1 9 25 49 81)
     (map (fn [index] (* index index))
          (filter odd? (range 10)))
     (for [index (range 10) :when __]
       __))

  "More complex transformations can be formed with multiple binding forms"
  (= [[:top :left] [:top :middle] [:top :right]
      [:middle :left] [:middle :middle] [:middle :right]
      [:bottom :left] [:bottom :middle] [:bottom :right]]
       (for [row [:top :middle :bottom] column [:left :middle :right]]
         __)))

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.