Clojure拉链功能

alu*_*lun 2 sequences clojure standard-library

我需要通过组合给定seq的第一,第二等元素来构建seqs(vec vec).

经过快速搜索并查看备忘单.我还没有找到一个并完成自己的写作:

(defn zip 
  "From the sequence of sequences return a another sequence of sequenses
  where first result sequense consist of first elements of input sequences
  second element consist of second elements of input sequenses etc.

  Example:

  [[:a 0 \\a] [:b 1 \\b] [:c 2 \\c]] => ([:a :b :c] [0 1 2] [\\a \\b \\c])"
  [coll]
  (let [num-elems (count (first coll))
        inits (for [_ (range num-elems)] [])]
    (reduce (fn [cols elems] (map-indexed
                              (fn [idx coll] (conj coll (elems idx))) cols))
        inits coll)))
Run Code Online (Sandbox Code Playgroud)

我有兴趣,如果有一个标准的方法吗?

Kyl*_*yle 7

(apply map vector [[:a 0 \a] [:b 1 \b] [:c 2 \c]])
;; ([:a :b :c] [0 1 2] [\a \b \c])
Run Code Online (Sandbox Code Playgroud)

您可以使用变量arity map来完成此操作.

map文档字符串:

...返回一个惰性序列,包括将f应用于每个coll 的第一项集合的结果,然后将f应用于每个coll中的第二项集合,直到任何一个colls都用完为止.其他colls中的任何剩余项目都会被忽略....