r/Clojure • • Sep 24 '19

Clojure guide for new Clojurists

Hey I rarely post here anything and just mostly ghost around and read what other people are talking about. But that changes now.

I have been working on open source guide for learning Clojure. I have been trying to keep more practical/pragmatic approach in comparison to many books I have been reading. The guide is not even remotely complete yet, but I think it is far enough for me to share it with you. The guide is called It Starts With Clojure, and it is currently available in Github at

https://github.com/anan44/it-starts-with-clojure

I would really like some feedback and to hear what you guys think about feasibility of learning source like this for upcoming Clojurists. I feel like the Clojure Ecosystem is kinda short on materials like this.

The course expects the reader to have some programming knowledge, but unlike many other materials it does not make assumptions that the background would be Java. It also aims to prepare the reader to actually create real applications instead of all the niceties of FP paradigm (even though I plan to cover some of that too). I also try to keep it well hyperlinked to other sources. In no way am I trying to replace existing resources but rather to compliment them. Also my goal is to keep this as a living document that can evolve, so it won't become obsolete or outdated.

I would really appreciate if you would give even few minutes to write your thoughts on this. Thanks!

95 Upvotes

21 comments sorted by

6

u/the_whalerus Sep 25 '19

I haven’t perused the whole guide yet (mobile) but I wanted to say the hardest part for me learning Clojure has been the build tooling.

I’m a js guy and am only vaguely familiar with Java, so getting my head around all the jvm concepts has been the most challenging thing.

3

u/kunqananaa Sep 25 '19

Thanks for the tip!

I’ll try to keep that in mind. I’ve already covered some build tooling, but I’ll try to include more.

1

u/pLeThOrAx Sep 25 '19

As someone also new to clojure ( and js!) I was wondering what you mean WRT jvm concepts?

2

u/the_whalerus Sep 25 '19

Things about classpaths, jars, maven, build specifics. For some reason I’ve never felt like I understood much about how java comes together but I can’t ever quite put my finger on why.

2

u/kunqananaa Sep 25 '19

I suggest that you forget about maven and such in your adventures to Clojure. Leiningen is your friend and he makes life easy for you.

1

u/the_whalerus Sep 25 '19

Doesn’t lein use maven under the hood?

6

u/joinr Sep 25 '19 edited Sep 25 '19

Doesn’t lein use maven under the hood?

It does, but that's not a pre-requisite or even desirable topic to learn. Under the hood, my car uses an internal combustion engine, with a sophisticated computer, as well as all sorts of fascinating stuff that I find totally irrelevant in order to engage in the act of driving.

lein does use aether behind the scenes as a library to interact with maven repositories (such as resolving dependencies and publishing things to well know repositories), but that's pretty much it: behind the scenes. You don't install anything. You don't manually set up the jvm class path. If you accept lein (or the clojure CLI script these days) as your primary interface for managing dependencies and handling all that stuff for you, it ends up doing a pretty decent job isolating you from the "under the hood" stuff. If you grow beyond the porcelain API, and need to configure more (like using custom internal maven repositories, etc.) then that's a separate concern and something you can grow into. There are some fairly rare dysfunctional states you can get into that leak the underlying maven stuff via errors, but that's still fairly rare IME (like trying to resolve dependencies with no network connection, having a proxy setup that isn't configured, etc.)

The only other area lein exposes some jvm intersection is how it sets up projects and corrals the classpath for you (where jvm looks for class files to load). The default lein project setup gives you a src/ folder, where the subdirectories typically correspond to projectname/core.clj -> (ns projectname.core ..) 1:1 in the source file. There's some minor weirdness with hyphenated names like (ns the-ns.core) having a folder with underscores (which is why I avoid hyphenated namespaces), but everything else is pretty simple (from defaults): the lein project will ensure that - upon starting the repl - everything in the /src directory is brought into the classpath for the jvm, which makes said classes (or in this case, .clj files) "discoverable" by clojure. Clojure will expect to find a directory structure (unless told otherwise) that corresponds to /package/name/file.clj -> (ns package.name.file) to establish a direct mapping with the directory structure. Unless you "want" to deviate from this (which lein allows), there's nothing else to learn for pure clojure dev. If you "want" to mix clojure/java projects, then there are additional things to pick up, but that's not required. Same thing with clojure/cljs, you've got some additional options to use (typically defaults) with an expectation of how files are laid out, but the same ideas should hold true there.

Both clj script and lein also allow for git dependencies if you like too, without deviating from the existing familiar dependency management. If that's too much, you can also just load-file from the repl for a very simple development experience, until you're tuned up with lein.

lein tutorial explaining this and more

more details on project structure

3

u/kunqananaa Sep 26 '19

Damn that was a great aswer!

5

u/totemo Sep 25 '19

On the TODOs:

Installation of Java on Linux varies by distribution. On Fedora, the package names and even the package manager has changed over time. On Ubuntu there are similar complications. However, if you're smart enough to work out Linux, you can work out how to install Java.

On Ubuntu 18.04:

 sudo apt-get update
 sudo apt-get install openjdk-11-jdk

Getting Java 12 on Ubuntu 18.04 requires a PPA and is probably more trouble than an intro guide needs to go into. Maybe Ubuntu 19.04 repositories have a later Java version. IDK.

On Fedora 30 (and later):

sudo dnf install java-1.8.0-openjdk-devel
# OR:
sudo dnf install java-11-openjdk-devel
# OR:
sudo dnf install java-latest-openjdk-devel # Currently, Java 12 

Clojure installation:

  • On any Linux distribution, you can get the latest Clojure by following the instructions on clojure.org.

  • Ubuntu 18.04: you can get Clojure 1.9 with sudo apt-get install clojure

  • Fedora 30: sudo dnf install clojure gets you Clojure 1.7.0. Bleagh.

3

u/kunqananaa Sep 25 '19

Thank you so much! I’ll just pop this in when i get home. Or if you want to be a contributor you can make a pull request to the repo.

1

u/totemo Sep 28 '19

You might want to exercise some editorial discretion on the github site in relation to my comment (above) . :) My objective was to provide you the information but I wasn't expecting the comment to be reproduced verbatim. For example, my reflection on the difficulty of getting Java 12 on Ubuntu 18.04 could be omitted for the sake of concision. The same with the Clojure package versions. It would suffice to say that the best way to install Clojure is via the instructions on clojure.org and installing it via packages is likely to give you something out of date.

1

u/kunqananaa Sep 28 '19

I am planning to look into it later when I have time. I just trew it there since it is still much better than ”Todo”.

Write first, edit later. :)

7

u/sahilmuthoo Sep 24 '19

I love the practical project based approach. I've been meaning to learn Clojure propely with a real-world project.

I spent some time dabbling but lost motivation. Your guide has rekindled my desire.

Expect more comments and questions on GitHub. Cheers!

4

u/tincholio Sep 26 '19

That looks super promising! I'm trying to get some co-workers started with Clojure, and I think your guide will be a nice starting point!

1

u/Eno6ohng Sep 25 '19

This looks very promising! Have you thought of hosting it as a book on https://readthedocs.org or a similar site?

2

u/kunqananaa Sep 27 '19

I have not really give that much though for hosting it on this kinda site. That sounds like a worth looking into.

Long term plan is to write front end for this guide and host it under its own domain.

1

u/pLeThOrAx Sep 25 '19

That's awesome! Thank you. will definitely check it out and get back to you :) as a noob to clojure coming from a non-java background, it's been hard connecting the interop dots so to speak. Some things aren't always immediately obvious, and sometimes a little intuition goes a long way. Saving this post!

1

u/kunqananaa Sep 25 '19

I have not covered the interop yet, but I intend to. Book called Programming Clojure by Alex Miller covers this topic rather nicely. If you are eager to get there before I finish my guide.

1

u/slifin Sep 27 '19

Feels like we need to unify our efforts here: https://clojure.org/guides/getting_started, since that's where I would expect newcomers to arrive

The main thing that is hard to teach, always a moving target, opinionated, and super important for Clojure is editors and their REPLs and their editing modes, I know I departed this community years ago because I thought it was impossible to edit a lisp sanely

1

u/kunqananaa Sep 27 '19

My first trial with Clojure was in 2015 and I also gave up because editors made the whole thing unpleasant. Later I tried again and overcame the issue. One of my goals is that one else has to give up for silly reason as editors.

Once the guide becomes more complete, I will defintelly see about possibilities of getting it included on the official page/guides. For now the guide is not quite there yet. But we are definitelly moving to the right direction.

0

u/olymk2 Sep 25 '19

Advise on mocking / stubbing with i am trying to mock out calls to an external web page which uses clj-http client I would like to stub this out and give the returned result so i can test my logic in different situations, searching a bit I came across spec instrument which seems like it does what I want.

How ever i am strugling to get this to work all the examples I have found operate on function in the current namespace, if i am mocking from another namespace or external library do i need a fully namespace path or can i mock out globally for the duration of the test ?

Open to using other libraries but if i can do it with spec that's one less requirement.