如何正确使用线程宏?

时间:2012-05-10 15:25:47

标签: clojure

我的问题是如何让Clojure的一个线程宏在我的特定情况下工作?谢谢。

我收到此错误:

(IllegalArgumentException Don't know how to create ISeq from: 
bene_csv.core$test_key_exclusion$fn__346  clojure.lang.RT.seqFrom (RT.java:487)

当我调用此函数时:

bene-csv.core=> (test-key-exclusion bene-data 1 gic-data 0 2 3)

鉴于此代码 - 没有线程宏的工作解决方案被注释掉了 -

(defn ret-non-match-rows
    "Expects a sequence of sequences, like what is returned from clojure-csv.
     Returns nil if there's a match; else returns failing row."

    [s-o-s cmp-col-idx inq-row-idx inq-row]

    (let [inq-row inq-row]
        (loop [[row & remain-seq] s-o-s pos 0]
            (let [cmp-val (nth inq-row inq-row-idx nil)]
            (cond
                (not row) inq-row
                (= cmp-val (nth row cmp-col-idx)) nil
                :not-found (recur remain-seq (inc pos)))))))

(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
     also gathers enough other data, so that excludees (those not found),
     can be identified."

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

    (-> (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))))))
(comment
    (map (fn [row]
            (vector (nth row pkey-idx1 nil)
                    (nth row lnam-idx nil)
                    (nth row fnam-idx nil)))

         (filter (complement nil?)
            (map #(ret-non-match-rows csv-data2 pkey-idx2 pkey-idx1 %1) csv-data1)))

数据类似于以下内容:

[["0123456789" "SMITHFIELD" "HAM"]["1123456789" "LITTLE" "CHICKEN"] ...]

1 个答案:

答案 0 :(得分:15)

您使用的是"thread-first" macro,但您应该使用"thread-last" macro。绑定到符号->>的“线程最后”宏与“线程优先”宏执行相同的操作,除了它将表单作为最后一个参数而不是第一个参数插入。

所以你的代码看起来像这样:

(->> (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))))))