Clojure Threading Operators

When I first started learning Clojure, wrapping my head around the different thinking was hard enough. Then, I saw the threading macros -> and ->>, and I wondered when those would ever be used outside a textbook. It wasn’t until I started doing something complicated that I saw the need to use them.

In the test-key-exclusion example the rows I’m interested in for my report (the ones that are not in the “other” report) are gathered, but nil values appear with any value that did match, because there is no interest in reporting a non-problem. Those values are filtered out with (filter (complement nil? , and then a vector of vectors is created with the non-matching results. ->> is quite useful as documentation to explain what is really going on.


(defn test-key-exclusion
"This function takes csv-data1 (the includees) and tests to see
if each includee is in csv-data2 (the includeds). This function
uses full rows, so ssn, lnam, and fnam can be reported."

[csv-data1 pkey-idx1 csv-data2 pkey-idx2 lnam-idx fnam-idx]

; This is a case for using the thrush ->> operator in Clojure.
; Insert each result into the last position of the next call.

(->> (map #(ret-non-match-rows csv-data2 pkey-idx2 pkey-idx1 %1) csv-data1)
(filter (complement nil?))
(map (fn [row]
(vector (nth row pkey-idx1 nil)
(nth row lnam-idx nil)
(nth row fnam-idx nil))))))

Advertisements

1 Comment

Filed under Clojure

One response to “Clojure Threading Operators

  1. There is certainly a great deal to know about this issue. I really like all the points you made.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s