Frequently Needed Things | Clojure
Welcome!
This is a collection of things you might need using Clojure—and how I resolve them.
§ Help, I forgot the Java/JS interop syntax—again!
§ I want to print a value inside a function call stack
Assume your code looks like this:
(defn make-other-thing [] (produce-other-thing (filter-things-out (transform-thing-to-things (make-thing)))))
But now, you want to take a look at the result of transform-thing-to-things without interfering with the rest of the function.
This would be the naive approach:
(defn make-other-thing [] (let [things (transform-thing-to-things (make-thing))] (println things) (produce-other-thing (filter-things-out things))))
But I think this is way better:
(defn inspect [thing] (println thing) thing)
(defn make-other-thing [] (produce-other-thing (filter-things-out (inspect (transform-thing-to-things (make-thing))))))
Or even:
(defn inspect [thing] (println thing) thing)
(defn make-other-thing [] (-> (make-thing) transform-thing-to-things inspect filter-things-out produce-other-thing))
And since inspect
is a function that probably should’ve been in the standard library—we put it in our utils
namespace:
(ns example.utils.debug)
(defn inspect [thing] (println thing) thing)
(ns example.main (:require [example.utils.debug :refer [inspect]]))
(defn make-other-thing [] (-> (make-thing) transform-thing-to-things inspect filter-things-out produce-other-thing))
§ ...
not found on classpath. Help!
- Check the spelling of the thing you want to include.
- Really check it.
- In every place.
If it’s not the spelling, it’s probably one of the following:
- The resource exists with the right name, but is not actually on the classpath.
- Is it in the right folder? That is, with your
:paths
looking like this ({:paths ["src" "test" "resources"] ...}
), is it in either./src
,./test
or./resources
from the project root? - Did you add the folder to the classpath? ->
- Is it in the right folder? That is, with your