Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove an item by type from a nested list or vector in Clojure?

Is there a way to remove items in a nested list by type such that (1 [2] 3 (4 [5] 6)) becomes (1 3 (4 6)) if I want to remove just the vectors?

Using postwalk, I can replace all vectors with nil, but I can not find a way to remove them.

(clojure.walk/postwalk 
  #(if (vector? %) nil %) '(1 [2] 3 (4 [5] 6)))

=>(1 nil 3 (4 nil 6))
like image 866
dansalmo Avatar asked Sep 28 '12 20:09

dansalmo


1 Answers

Far from perfect but perhaps it is a good start:

 (clojure.walk/prewalk #(if (list? %) (remove vector? %) %) '(1 [2] 3 (4 [5] 6)))
like image 88
DanLebrero Avatar answered Oct 13 '22 10:10

DanLebrero