LoginSignup
2
2

More than 5 years have passed since last update.

Clojureでzip関数を作ってみる

Posted at
  • テスト投稿~

Clojureでzipmap関数はあるのにzipseq関数がないので作ってみた。
といっても簡単

zipseq1.clj
(defn zipseq [& colls] (partition (count colls) (apply interleave colls)))

実行結果

(zipseq [1 2 3] [:a :b :c]) ; => ((1 :a) (2 :b) (3 :c))
(zipseq [1 2] [:a :b] [:c :d]) ; => ((1 :a :c) (2 :b :d))

これだと引数が1つの時に例外発生するので、1引数にも対応してみる。(1引数にも対応すると転置行列を作るときに使えるかなあと)

zipseq2.clj
(defn zipseq
  ([coll] (partition 1 coll))
  ([c1 & colls] (let [c (cons c1 colls)] (partition (count c) (apply interleave c)))))

実行結果

(zipseq [1 2 3]) ; => ((1) (2) (3))

できた~♪

2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2