Wednesday, September 30, 2009

Short circuiting reductions

Sometimes reduce needs to short circuit

(defn reducer
  "Returns a reduction closure the terminates when pred is false. Applies f to the last value that returns true. f defaults to identity if not provided. Behaves like reduce if pred is always true."
  ([pred] (reducer pred identity))
  ([pred f] (do-stuff:TBD))

Wednesday, September 23, 2009

Uses for juxt

The juxtaposition operator can be used in parsing text.

Suppose we have the following text to process:


Consider the following function which gets information from the clipboard

(defn bom-parser
    (map (&
      (juxt last second (constantly part-num))
      (p split #"\s+")
      ((& split-lines
        (p gsub #"\"" "")
        (p gsub #"NOT SHOWN" ""))

In the mapping operation you can see a call to juxt. This helps turn a block of text into a list of vectors.