Ian CT
Ian CT

Reputation: 1411

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?

Upvotes: 1

Views: 330

Answers (2)

Cwt
Cwt

Reputation: 8576

Further tooling support:

IntelliJ - Cursive plugin: Since version 0.1.29 provides:

New Structural Editing commands: Kill Sexp, Thread/Unthread Form, Move Form Up/Down.


VS Code - Calva plugin: You would use the Thread Last All command to get the desired result. Full list of threading options are:

Command Title Command Key
Thread First clojureLsp.refactor.threadFirst
Thread First All clojureLsp.refactor.threadFirstAll
Thread Last clojureLsp.refactor.threadLast
Thread Last All clojureLsp.refactor.threadLastAll

Upvotes: 0

Sam Estep
Sam Estep

Reputation: 13304

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.

Upvotes: 5

Related Questions