r/Clojure • u/argsmatter • Jul 02 '22
Things about clojure or tooling, you found out way too late.
Hey guys,
what did you find out very late and wished to have known earlier in clojure or with the tools using clojure?
Please correct me on my points, if you think, that they are (still) wrong.
- Debugging in emacs is easy, when you create a leiningen project.
- I just found out, that i can in general use reduce instead of recursion and it looks pretty fine. It is readable and there will be no stackoverflow. Loops do not really appeal to me.
- destructuring: it's there and it's nice!
What are yours?
27
u/scarredwaits Jul 02 '22 edited Jul 02 '22
Emacs-specific: you can modify library code on the fly. Use M-. to jump to the implementation of a 3rd party library function, try typing and discover the buffer is in read-only mode. Disable read-only by pressing C-x C-q, change the code of the function (add some logging or something), press C-c C-k to evaluate the buffer, and now the function has changed behaviour.
Clarification: the change is not permanent as you cannot save the changed file (at least if it’s in a jar). The function will revert to its original implementation next time you restart your REPL. To change it again re-evaluate the buffer.
7
u/pihkal Jul 03 '22
To be clear, that sequence of key commands is Emacs-specific, but library code can be altered in any REPL.
2
u/jpmonettas Jul 03 '22
If it is only for logging what is happening inside libraries functions there is also FlowStorm. You can instrument any library by just clicking, and is IDE independent. You can see a demo of it at https://youtu.be/cnLwRzxrKDk?t=28
2
24
u/fancyl Jul 02 '22 edited Jun 21 '23
This has been deleted in protest of the greedy API changes and the monetization of user-provided content and unpaid user moderation.
3
2
u/wellingtonthehurf Jul 02 '22
Nice! Something like re-frame-10x does much the same thing but automatically what with everything being stored in app-db. Wish the UI was as nice haha.
But nice to know there's something like it for straight Clojure or non-re-frame CLJS. I suppose if you set it up with a key binding to tap symbol under cursor it wouldn't be a massive deal either.What's your workflow like with it?
24
u/scarredwaits Jul 02 '22
You can put a def in the middle of a function to capture a value, Clojure does not enforce defs having to be top-level. Much more effective than logging/printing because you can mess around with the captured value in your REPL (filter it, transform it etc), just switch the REPL to the relevant namespace. Also, don't commit such code, it's for dev purposes only.
5
u/joinr Jul 02 '22
You get lots of globals now, potentially smashing other "real" defs if you're not careful, or holding onto values you may not want (e.g. heads of large lazy seq). I resorted to shoving stuff in atoms defined outside the function and just reseting to capture.
4
u/scarredwaits Jul 02 '22
Correct, you need to be very mindful when sprinkling code with defs like that. I tend to give them unlikely names such as kk, vv etc, which don’t normally occur in the code, but yeah it could be a bit risky. I’ve also used atoms as you described which has the additional advantage of allowing you to accumulate values over multiple invocations.
5
u/setzer22 Jul 03 '22
I adopted the style of using a
--prefix for debug defs to avoid clashes. So you'd say(def --my-foo foo)instead.
19
u/nathants Jul 02 '22
shadow-cljs exists.
1
u/wellingtonthehurf Jul 05 '22
Been stuck on figwheel-sidecar forever because each time I try to upgrade to -main i run into some error and eventually give up for a year or so.
What are the main benefits of shadow, when compared to figwheel? Been using it for fireplace functions but seems to me it does much the same thing. Which would be reason enough to try to move over to it if I can't get figwheel-main running properly... Trying to codesplit on sidecar but can't get it working...
0
Jul 05 '22
[deleted]
1
u/wellingtonthehurf Jul 05 '22 edited Jul 05 '22
I mean so does lein-npm, native :npm-deps or whatever. Issue I've always encountered that makes me not use npm is it horribly slows down auto-reload, by multiple seconds... even for a single npm lib (because it's bound to pull a total of fiftyeleven2 packages argh)
Is that not the case with shadow?
Because if then, sold!! Would be awesome to not have to rely on what's on cljsjs and/or manually setting stuff up.
0
Jul 05 '22
[deleted]
1
u/wellingtonthehurf Jul 05 '22
alright! Should give migrating a go regardless for code splitting benefits, npm without slowdowns would be such a nice bonus. Cheers
12
u/setzer22 Jul 03 '22
#_ comments the next form and is a much nicer alternative to ;, but what I didn't know until a few weeks ago is that you can do #_#_ to comment the next two forms, without having to comment each individual one!
Great to comment out let bindings:
(let [#_#_foo (bar 1 2 3)] ...)1
u/wellingtonthehurf Jul 05 '22
I just have a mapping to insert #_ before word or form so for me striking a let is just ,-w,-, but good tip :)
12
u/tsunyshevsky Jul 02 '22
(SSierra) Component - once we found out about them we redid the whole app.
(#_ is also pretty handy)
5
u/roguas Jul 02 '22
For "easy" stateful components you can also use mount. If you have a need for true, robust declarative stateful component system there is also integrant.
5
u/bsless Jul 03 '22
After plenty of bad experiences with mount, I'd say the ease isn't worth it
2
u/stefan_kurcubic Jul 04 '22
bad how? could you provide more details?
1
u/bsless Jul 12 '22
My experience has been that code written using mount is promiscuous. Since you can easily "touch" a state from any other place in your code, all guarantees regarding referential transparency and code organization will go out the window.
Then, testing. What is the exact and absolute opposite of joy? That's what it was like. Everything needs to be redefed and rebound dynamically. Any ability to reason about the code, debug it or understand it is destroyed in the process.
It's not that it's impossible to write decent, reasonable, code using mount. With enough discipline and organization, everything is possible; but we're imperfect beings and in the end I found it's even better to replace it with nothing.
2
u/onetom Jul 03 '22
after trying juxt/clip I tried redelay+rmap and I found it a cleaner solution overall.
here is an article, which demonstrates how to use the 2 micro-libraries together, achieve either an Stuart Sierra Component-style or mount-style state management solution:
https://functionalbytes.nl/clojure/redelay/rmap/2020/06/26/redelay.html
1
u/tsunyshevsky Jul 03 '22
Pretty cool article - thanks! The reasoning behind redelay actually makes a lot of sense too. I understand that the lib let’s you handle state as you wish but any experience dealing with stopping/starting what in SS Component would be called “subsystems”? I’m currently running my own solution on top of SSComponent but is not “elegant” at all
1
u/onetom Jul 03 '22
it turned out, that we didn't even use the system stopping capability during development. we just create and start new systems and let the previous ones garbage collected. we use in-memory Datomic DBs for development and testing and a bunch of Martian API clients for talking to 3rd party APIs OR some atoms to fake a few of them. there is nothing really to stop even. but play around with the 2 libs from the REPL to get a feel for the capabilities, then maybe you can have a minimal, concrete example we can discuss.
2
u/tsunyshevsky Jul 03 '22
Got it, I guess that makes sense. Not sure I can really only rely on the gc to “stop” the needed systems but, as you said, without toying with it a little I might just have the wrong head model. Thanks!
6
u/jpmonettas Jul 03 '22 edited Jul 03 '22
There is also flow-storm debugger now. It has quite improved my flow of debugging/understanding clojure code (and I'm a emacs cider user) https://github.com/jpmonettas/flow-storm-debugger
Author here, so may be biased.
6
u/roguas Jul 03 '22 edited Jul 04 '22
Reduce should be preferred. It has universal interface (defn reduce-fn [acc item]) and usually guides you towards a better main data structure.
Edit: To add a bit more to it. Loop/recur is more low level, you have to orchestrate reduction on your own. Where reduce will hide away this piping for sequences.
3
u/gleenn Jul 02 '22
The test-refresh plugin is super useful because it auto-reloads files and tests so you don't have to repeatedly wait for "lein test" to start, my secret sauce is to do "lein test-refresh :changes-only" and then mark specific deftests like "(deftest :test-refresh/focus foo-test ...)" and that will rerun only the marked tests, and if I wanna rerun the whole suite I don't even have to kill it, I just remove the ":test-refresh/focus" and it will switch back to full runs. Saves a ton of time waiting for lein to start.
3
u/onetom Jul 03 '22
the focus-meta option can do that to, in Kaocha: https://cljdoc.org/d/lambdaisland/kaocha/1.68.1059/doc/focusing-based-on-metadata
2
u/zupatol Jul 02 '22
I'm only using it for a toy project.
I only just found out how to set breakpoints in library code in emacs (with or without leiningen).
I thought installing clj was a pain on windows until I noticed it could be installed with scoop.
1
u/rufusthedogwoof Jul 02 '22
Hi, curious what you found painful about installing clj on windows?
Scoop isn’t an option at my org. I was kind of hoping it’s just a script and a jar file but I don’t dev on windows much (yet).
2
u/joinr Jul 02 '22
It's a powershell script that you can install locally. Depending on the org though, clj and several projects have now adopted doing everything from github repos using version tags, instead of e.g. clojars. So if you can't reach github for some reason you may need to tunnel out.
I also had some security issues in my powershell due to group policies, specifically related to this since group policy decided that md5 is not FIPS compliant. Some orgs may have similar issues. Since clj is somewhat of a 2nd class citizen (it mostly works though), the trivial fix has been in the queue for ~2 years (not uncommon though). So you may have to patch the script yourself.
If you don't fall into those cases though, the powershell install script works fine. The only pain you may run into is quoting args in the CLI, which means many examples online (assuming bash) won't work without tweaking (with extra quotes). A quick work around is to just dump everything needing quotes (typically dependencies) into a deps.edn file or provide aliases, since the quoting won't be an issue that way.
Or just stick with leiningen, which has worked well on windows (and elsewhere) since ~2012 for me.
1
u/rufusthedogwoof Jul 02 '22
Thanks very much for the response.
If I onboard windows devs maybe they’ll have to stick with lein… even powershell is a blocked as a potential security issue here. /shrug
4
u/Borkdude Jul 02 '22
You can try deps.clj which works without Powershell.
1
u/joinr Jul 04 '22
I never noticed the uberjar option, that could solve several problems for me. My only hangup with tools.deps right now is getting it working with a SOCKS proxy for tunneling. I can do it with leiningen (doing some runtime eval in the user.profile to setup the appropriate networking config for the jvm system properties). I am I guessing I can do the same thing with deps.clj as an uberjar.
1
u/Borkdude Jul 04 '22
Let me know how that goes. For faster startup you can also choose the binary. Or just use babashka with
bb clojure.1
2
u/joinr Jul 02 '22
lein deploys a batch file and I think a ps1. So depending on how austere the environment is, options may be limited (e.g. if cmd is similarly locked down). If, however, you have at least a JRE (oddly enough this is prevalent in my case), I find/found that NightCode is pretty portable, and it bundles leiningen (an older version), and you can get it as an ubjerjar. Although it's no longer in active dev (nobody has forked it), it can be a decent minimal environment. There are even some pure-java terminal emulators. Even then, you may have issues getting out to clojars for dependencies. If you can setup an ssh tunnel, it's possible to use a SOCKS proxy to regain connectivity though. The better option is to get the org to loosen up or carve out exceptions.
VSCode is another (probably better) option these days if the org allows it. It also has some browser based options to allow hosting in a remote server and interaction with 0 install locally (or you can do the local client and ssh around apparently). Windows devs would probably appreciate it as a familiar experience, and the Calva plugin appears to be excellent. It all depends on what you can actually get though. Ii think 99% of the clojure devs are not working in an austere environment and are just used to having everything available, so working under exogenous constraints is a bit of an foreign concept to many (let alone working in an airgapped environment).
1
u/zupatol Jul 02 '22
First you need to find out which one to use. I started using an unofficial one, had some trouble, can't remember what, and switched to leiningen. Later I cloned a project that was using clj, and meanwhile an official alpha version of a clj for windows appeared. This one only has a clj command in powershell. Everywhere else, you need to call "powershell --some-option clj". The project I was looking at had a build process based on lein which called clj in the background and didn't find it. I finally found a way to configure the executable, but it didn't offer the possibility to call it with an option. The scoop installation fixed that because it includes an executable called cmd-clj.
1
u/rufusthedogwoof Jul 02 '22
I wish I knew cider nrepl, and refactor nrepl are tricky to configure when connecting to already run repls.
(Not jacking in).
Maybe I should spend more time with socket repls and inferior modes in emacs.
64
u/scarredwaits Jul 02 '22
If you put a symbol after fn in an anonymous function, that defines a name for it (anonymous with a name, contradictory, I know) which shows up in stack traces, making it much easier to understand where the problem is.