The problem is that snds
is the lazy seq, so (snds lst)
throws an error. filter
always returns a lazy seq.
Your function also is too complicated. Try to make it simpler:
(defn costlist [lst]
(zipmap (map first lst)
(remove nil? (map second lst))))
Now you can do what you want:
(costlist (list '("Milk" 4) '("Bread" 2) '("Milk")))
I'm using list
because quote
prevents evaluation of an expression (see ToBeReplaced's answer):
=> '( '("Milk" 4) '("Bread" 2) '("Milk"))
((quote ("Milk" 4)) (quote ("Bread" 2)) (quote ("Milk")))
So, you should avoid using quote
for building lists.
Your solution also suggests that nil
values may occur only at the end of the list. You can easily fix it:
(defn costlist [lst]
(->> (filter (comp #{2} count) lst)
(map vec)
(into {})))
So, now
(costlist (list '("Milk" 4) '("Milk") '("Bread" 2)))
will work too.