vvvvalvalval / scope-capture

Project your Clojure(Script) REPL into the same context as your code when it ran
MIT License
576 stars 14 forks source link

Capturing values in thread first or thread last statements #32

Closed jefffriesen closed 6 years ago

jefffriesen commented 6 years ago

It would be nice to be able to capture each step in threading macros. For example:

(defn abbr-into-set
  [permit keyword-map]
  (sc/spy
   (into #{} (->> keyword-map
                  (filter #(include-exclude-match? permit %))
                  (map first)
                  (map name)))))

I could capture the intermediate state for each step in this thread last macro. When I tried this spy only captured the function arguments. I couldn't see a way to capture the rest in the documentation. Do you think this would be a good feature?

vvvvalvalval commented 6 years ago

@jefffriesen This use case seems too specific for scope-capture - it's more of a job for a tracing library or a custom macro.

Note that, if each step is a pure and deterministic computation (as it should be most of the time, since Clojure encourages functional programming!), you can get the desired result by reproducing the context of the expression + some paredit-fu to re-evaluate a growing number of steps (e.g: move the closing paren of the (->> ...) expression to right after keyword-map, evaluate, then slurp in the next expression, evaluate, and so on.)

You could also imagine creating your own spy->> macro that logs and/or records and/or spies each intermediate step!

jefffriesen commented 6 years ago

Ok, thanks @vvvvalvalval