mpqzog
Last Updated: February 25, 2016
·
2.202K
· runexec # Zipmap and Interleave differences

###### functional

The difference between zipmap and interleave

;; Define test objects

``````user=> (def a [:a :b :c :d :e])
#'user/a
user=> (def b (range 10))
#'user/b
user=> a
[:a :b :c :d :e]
user=> b
(0 1 2 3 4 5 6 7 8 9)``````

;; Let's use interleave on a b

``````user=> (def -interleave (interleave a b))
#'user/-interleave``````

;; Check first item

``````user=> (first -interleave)
:a``````

;; Check all items -- Notice maintained order
;; and that b ends when a ends

``````user=> -interleave
(:a 0 :b 1 :c 2 :d 3 :e 4)``````

;; Let's use zipmap on a b

``````user=> (def -zipmap (zipmap a b))
#'user/-zipmap``````

;; Check first item

``````user=> (first -zipmap)
[:e 4]``````

;; Check all items -- Notice the reverse order
;; and that b ends when a ends

``````user=> -zipmap
{:e 4, :d 3, :c 2, :b 1, :a 0}``````

;; Let's see what the reverse looks like

``````user=> (reverse -zipmap)
([:a 0] [:b 1] [:c 2] [:d 3] [:e 4])``````

;; Let's see what the reverse looks like for interleave

``````user=> (reverse -interleave)
(4 :e 3 :d 2 :c 1 :b 0 :a)``````

;; What's the size? [key, value],

``````user=> (count -zipmap)
5``````

;; What's the size? [value, value, ...]

``````user=> (count -interleave)
10``````

;; What types do they return?

``````user=> (type -zipmap)
clojure.lang.PersistentArrayMap
user=> (type -interleave)
clojure.lang.LazySeq``````