Add to vector in function

I have two columns (vectors) of different lengths and you want to create a new row vector (if there are enough elements in the column). I am trying to create a new vector (see Unsuccessful attempt below). In Java, this includes the steps of: iterating a vector, checking condition, adding to a vector, return vector. Do I need recursion here? I am sure this is not difficult to solve, but it is very different from the procedural code.

(defn rowmaker [colA colB]
  "create a row of two columns of possibly different length"
  (let [mia (map-indexed vector colA)
        rows []]
    (doseq [[i elA] mia]
        ;append if col has enough elements
       (if (< i (count colA)) (vec (concat rows elA)))  ; ! can't append to rows
       (if (< i (count colB)) (vec (concat rows (nth colB i)))
    ;return rows   
    rows)))

Expected I / O Example

(rowMaker ["A1"] ["B1" "B2"])
; => [["A1" "B1"] ["" "B2"]]
+4
source share
5 answers
(defn rowMaker [colA colB]
  "create a row from two columns"
  (let [ca (count colA) cb (count colB)
        c (max ca cb)
        colA (concat colA (repeat (- c ca) ""))
        colB (concat colB (repeat (- c cb) ""))]
    (map vector colA colB)))
+1
source
(defn rowmaker
  [cols]
  (->> cols
       (map #(concat % (repeat "")))
       (apply map vector)
       (take (->> cols
                  (map count)
                  (apply max)))))
+1
source

. .

(defn row-maker
  [col-a col-b]
  (loop [acc []
         as (seq col-a)
         bs (seq col-b)]
    (if (or as bs)
      (recur (conj acc [(or (first as) "") (or (first bs) "")])
             (next as)
             (next bs))
      acc)))
+1

:

(defn rowMaker [v1 v2]
  (mapv vector (concat v1 (repeat "")) v2))

(rowMaker ["A1"] ["B1" "B2"])
;[["A1" "B1"] ["" "B2"]]

:

(rowMaker ["B1" "B2"] ["A1"])
;[["B1" "A1"]]

, mapv, , . map, :

(defn map-filler [filler f & colls]
  (let [filler (vec filler)
        colls (vec colls)
        live-coll-map (->> colls
                           (map-indexed vector)
                           (filter (comp seq second))
                           (into {}))
        split (fn [lcm] (reduce
                         (fn [[x xm] [i coll]]
                           (let [[c & cs] coll]
                             [(assoc x i c) (if cs (assoc xm i cs) xm)]))
                         [filler {}]
                         lcm))]
    ((fn expostulate [lcm]
       (lazy-seq
        (when (seq lcm)
          (let [[this thoses] (split lcm)]
            (cons (apply f this) (expostulate thoses))))))
     live-coll-map)))

, filler . , rowmaker :

(defn rowmaker [& colls]
  (apply map-filler (repeat (count colls) "") vector colls))

.

(rowmaker ["A1"] ["B1" "B2"])
;(["A1" "B1"] ["" "B2"])

(rowmaker ["B1" "B2"] ["A1"])
;(["B1" "A1"] ["B2" ""])

!

0
(defn make-row
  [cola colb r]
  (let [pad ""]
    (cond
      (and (not (empty? cola))
           (not (empty? colb))) (recur (rest cola)
                                       (rest colb)
                                       (conj r [(first cola) (first colb)]))
      (and (not (empty? cola))
           (empty? colb)) (recur (rest cola)
                                 (rest colb)
                                 (conj r [(first cola) pad]))
      (and (empty? cola)
           (not (empty? colb))) (recur (rest cola)
                                       (rest colb)
                                       (conj r [pad (first colb)]))
      :else r)))
0

All Articles