octopusgrabbus
octopusgrabbus

Reputation: 10695

How To Interleave Each Element From Multiple Sequences Into Vector

I want to create a vector from each element of the sequences returned below beginning with (vector. I know (vector is the wrong way to get what I want. It is just a placeholder.

I'm struggling with how to take the first element out of the three sequences and get ["abc-de-fghi" smith barney] [def-hi-jklm" ox todd], then the second element, third, and so on.

Thank you for your help.

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

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

Edit: Solution

I wound up with the following to create a vector from each element of multiple sequences.

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

Upvotes: 3

Views: 1117

Answers (1)

Rayne
Rayne

Reputation: 32675

I think what you need is just map. It can take multiple collections:

user> (map vector [1 2 3] [:a :b :c] ["a" "b" "c"])
([1 :a "a"] [2 :b "b"] [3 :c "c"])

Upvotes: 7

Related Questions