Problem 39

60 views
Skip to first unread message

Cyrex Cyborg

unread,
Nov 10, 2017, 9:18:43 AM11/10/17
to 4Clojure
Greetings, clojurians! I want to ask the community - why (comp flatten seq zipmap) failing the tests?
Message has been deleted

Leif

unread,
Nov 12, 2017, 11:27:15 PM11/12/17
to 4Clojure

Hi, Cyrex.

zipmap makes a Map, which is an unordered collection. So you are not guaranteed to get the pairs out in the same order you put them in. You may, depending on the clojure version, the exact keys, and the size of the input, but this is just a lucky coincidence. Look what happens if you pass in longer vectors (clojure 1.8.0):

(def v (shuffle (range 10)) ;;=>
[5 6 2 4 0 3 7 8 9 1]
(def w (shuffle (range 10)) ;;=>
[0 5 6 2 3 8 1 4 7 9]
(zipmap v w) ;;=> unordered
{0 3, 7 1, 1 9, 4 2, 6 5, 3 8, 2 6, 9 7, 5 0, 8 4}
(seq (zipmap v w)) ;;=> random order out
([0 3] [7 1] [1 9] [4 2] [6 5] [3 8] [2 6] [9 7] [5 0] [8 4])

Also, be aware that flatten acts recursively, so it may pass the tests, but fail more generally:

(f [:a :b] [[1 2] [3 4]]) ;;=>
([:a [1 2]] [:b [3 4]])
(flatten '([:a [1 2]] [:b [3 4]])) ;;=>
(:a 1 2 :b 3 4) ;; Wrong

Hope that helps,
Leif

Reply all
Reply to author
Forward
0 new messages