Fork me on GitHub

So when trying to solve an exercise, I usually end up "testing" a function and leaving the result as a comment so I can see what it does when I come back to it. It's sort of been how I document code. But would you actually leave those lines in your source code? Or is this what people are talking about including at the bottom with a (comment ...) at the bottom.


(ns aoc.y2018.d01)

(def data
  (->> "resources/y2018-d01.txt"
       (map #(Integer. %))))

(take 5 data)
;; => (7 15 18 -15 8)

(defn part1 [data]
  (apply + data))

(part1 data)
;; => 505


like the (take 5 data) and the (part1 data) with the eval as a comment. Is it ok to leave it in there as a form of documentation or does that annoy people?


I'd at least put it in a comment form or use #_ -

(take 5 data)


I like putting #_ on its own line since that doesn't effect indentation


I think (comment ..) is still evaluated during compile time, which might not be what you want


e.g. spec macros are still expanded


I think having a comment form at the bottom is fairly common practice. There’s some in the stdlib.


When I want to show more complex behavior of the functions I’ve written — how to get the data they need from disk, transform that data, call the functions with the data and how different functions interact with each other — I’ve used comment.


That’s mostly for migrations or one off code, though. Namespaces that aren’t used outside of a repl, for one specific task and then rarely revisited.


Your approach, to have the result on the following line, looks really nice to me, especially because it’s not showing off the interplay between different functions, but instead is reminding you what to expect as the output of a single function.


For an app, though, I wouldn’t leave those kinds of comments as the source code for the whole app should show how all of those functions interact.


That doesn’t mean you can’t, especially as you’re learning, just that I haven’t found a benefit to at this point in my Clojure journey.


(I generally use the namespace docstring, individual function docstrings, and the use of private function definitions to outline how a namespace is meant to be used.)


This is the perfect use case for a unit test.


thank you for the discussion folks! I do want to get into more testing and figuring out what that's all about. And the other advice seems spot on too.


We're in the "use (comment ..) forms and leave them in the source code" at work.


Rich does it too, per Stu Halloway's comment in one of his recent talks (where he referred to them as "Rich Comment Forms" 🙂 )


Hello, I hope this is the right place to ask. I finished clojure for brave and true and wanted to lay my hands on some web development. I tried luminus standard lein template, but it seems to be a bit overwhelming with mount, embedded repl, migrations, different configurations for dev/prod, etc. I can't even tell what 90% of the code does there. Do you maybe happen to have some more gradual clojure web-development material that would introduce all these things separately? Additionally, I think I'm having an issue with embedded repl. I'm using emacs and connect to the nrepl that's running within said lumius project with cider-connect. I can call the code in the repl, but I can't use 'go-to' feature. I'm getting cider-find-var requires the nREPL op "info" (provided by cider-nrepl). Is it supposed to work this way, or should I pass some flag somewhere? Thanks!


hey, if you scroll up just a bit there is a thread that started with me asking something similar. It started with a database discussion but then turned into a great advice thread on getting started with web development


the tl/dr is look at something simpler like just using the Compojure and Ring libraries and making a small server side rendered app with just those two.


@nvm-registrations for CIDER to be a bit more intelligent, it needs to have a dependency (`[cider/cider-nrepl "0.20.0"]`) for it to figure out how to navigate. If not you just have a "dumb" repl. If you use m-x cider-jack-in it should make sure those dependencies are met; if you prefer to start it elsewhere and use cider-connect you'll need to make sure those dependencies are on the classpath or you are ok with just the "dumb" repl experience


I actually hadn't realized that. I usually go with cider-jack-in but that is good to know. is that true if you used lein repl while in the project directory or is the lein nrepl connection "smart" by default too?


the only thing that dictates "smart" versus not is the presence of cider-nrepl on the classpath. If you use lein repl and have the deps there it will be smart. else you will be greeted by cider-find-var requires the nREPL op "info" (provided by cider-nrepl)


cider-nrepl extends the middleware available to nrepl. which at its core receives messages like {:op eval code [your code]} and returns the result. the middleware can extend (or replace) what ops are understood in these messages


I was trying to check my understanding with this but I still see this warning when connecting to a running lein repl with cider-connect: WARNING: CIDER requires cider-nrepl to be fully functional. Many things will not work without it!


I put the cider nrepl dependency in my project.clj and ran lein deps before restarting the repl


does it matter that my cider repl says it is version 0.21.0snapshot but I put "0.20.0" in my project.clj?


cool! Thanks. I had confused myself too because I had been told that cider now puts those dependencies in for you if possible but never actually see them put into my project.clj or anything. I only see it saying something as the repl is getting initialized


it doesn't add them to your project. it adds them at startup time. this way people who don't use cider don't have unnecessary dependencies


it logs exactly what it is doing in *Messages*: > [nREPL] Starting server via /usr/local/bin/clojure -Sdeps '{:deps {nrepl {:mvn/version "0.5.3"} cider/cider-nrepl {:mvn/version "0.20.0"}}}' -m nrepl.cmdline --middleware '["cider.nrepl/cider-middleware"]'...


hmm interesting, thanks


apparently when you embedding a repl into your application you need to explicitly pass cider-nrepl: