views:

122

answers:

3

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

+1  A: 

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)

Brian
A: 

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

Nikolaus Gradwohl
A: 

Is this is close enough?

(seq (zipmap [1 2 3] [4 5 6]))
;=> ([3 6] [2 5] [1 4])
Siddhartha Reddy
Not quite: `(seq (zipmap [1 2 1] [3 4 5]))` => `([2 4] [1 5])`.
kotarak