Fork me on GitHub
#beginners
<
2019-02-09
>
Chase14:02:37

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.

Chase14:02:47

(ns aoc.y2018.d01)

(def data
  (->> "resources/y2018-d01.txt"
       (slurp)
       (clojure.string/split-lines)
       (map #(Integer. %))))

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

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

(part1 data)
;; => 505

Chase14:02:36

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?

noisesmith17:02:26

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

#_
(take 5 data)

5
noisesmith17:02:50

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

lmergen18:02:02

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

lmergen18:02:31

e.g. spec macros are still expanded

Chris18:02:16

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

futuro18:02:03

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.

futuro18:02:51

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.

futuro18:02:31

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.

futuro18:02:16

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.

futuro18:02:04

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.

futuro18:02:26

(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.)

caleb.macdonaldblack20:02:55

This is the perfect use case for a unit test.

Chase21:02:23

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.

seancorfield22:02:32

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

seancorfield22:02:01

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

aangst20:02:04

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!

Chase21:02:20

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

Chase21:02:52

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.

dpsutton21:02:42

@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

Chase21:02:04

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?

dpsutton21:02:20

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)

dpsutton21:02:15

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

Chase21:02:45

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!

Chase21:02:19

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

Chase21:02:57

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

Chase21:02:27

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

dpsutton21:02:13

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

dpsutton21:02:59

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"]'...

aangst21:02:56

hmm interesting, thanks

aangst21:02:58

apparently when you embedding a repl into your application you need to explicitly pass cider-nrepl: https://github.com/clojure-emacs/cider-nrepl#via-embedding-nrepl-in-your-app