one-zero-zero-one
one-zero-zero-one

Reputation: 1580

Zipping Collections

How do you zip two sequences in Clojure? IOW, What is the Clojure equivalent of Python zip(a, b)?

EDIT:
I know how to define such a function. I was just wondering whether standard library provides such a function already. (I would be *very* surprised if it doesn't.)

Upvotes: 4

Views: 554

Answers (3)

Siddhartha Reddy
Siddhartha Reddy

Reputation: 6211

Is this is close enough?

(seq (zipmap [1 2 3] [4 5 6]))
;=> ([3 6] [2 5] [1 4])

Upvotes: 0

Nikolaus Gradwohl
Nikolaus Gradwohl

Reputation: 20124

if you want the input to be lists you can define a zip function like this

(defn zip [m] (apply map list m))

and call it like this

(zip '((1 2 3) (4 5 6)))

this call produces ((1 4) (2 5) (3 6))

Upvotes: 0

Brian
Brian

Reputation: 1822

You can easily define function like Python's zip:

(defn zip
  [& colls]
  (apply map vector colls))

In case of (zip a b), this becomes (map vector a b)

Upvotes: 3

Related Questions