Search code examples
functional-programmingclojureideexpression

Converting a heavily nested Clojure function to the threaded form


I am trying to convert heavily nested expressions in Clojure. One example is the following:

(distinct (flatten (map keys (flatten (filter vector? (vals data))))))

The threaded form would be:

(->> data vals (filter vector?) flatten (map keys) flatten distinct)

Is it possible in Clojure to create a function or macro that help me automate getting the threaded form with the nested form as input? Or are there any third-party tools that I can use?


Solution

  • If you're using CIDER, I would recommend clj-refactor. It has refactoring capability for both -> and ->>, as well as a whole bunch of other stuff.