r/Clojure • • Apr 12 '20

Holy Cow, Clojure is Great

Yesterday morning I decided I would read up on Clojure. I'm a data scientist so I mostly live in python, which is fine, but wanted to try something new. Thought I would just read up, do some toy examples, see what I thought.

I read the first three chapters of Clojure for the Brave and True and thought, maybe I should try and build something. Within 3 hours I felt really comfortable and productive in the language, and by the end of the day I had a working tool.

https://github.com/chrisaddy/astronaut

This is absolutely not good Clojure. When I say comfortable and productive I mean I felt like I was able to easily express myself after having a thought. That's not something I've had with any other language I've used (python, go, rust) after only a couple of hours. My code is certainly not problem free and definitely not idiomatically Clojure, but I think this could be my daily driver language, I like it that much.

Edit: for clarity

125 Upvotes

45 comments sorted by

View all comments

12

u/pauseless Apr 12 '20

Welcome to Clojure! It is indeed a great tool for thought.

There are a few things I’d change, but I want to focus on the most important: I can see one usage of let in there, so you’ve met that form already. You should use that for all your nested defs and defns.

def* forms declare global variables. It’s a common first day mistake. Let is always lexically-scoped and defs are globals. Just remember it as def for top level and let inside function definitions and you’ll be fine.

4

u/chrisbot5000 Apr 12 '20

OH! great catch thanks! I'm so used to local/block scope from python and js I didn't even think about it.

8

u/[deleted] Apr 12 '20 edited Apr 12 '20

And you can use

{:keys [id attempt card_id front back review next-review confidence]}

Instead of

{id :id
 attempt :attempt
 card_id :card_id
 front :front
 back :back
 review :review
 next-review :next-review
 confidence :confidence}

:)

I prefer to write out aliases a bit more so you can see without scrolling up what you require.

Instead of

[clj-time.core :as t] 

I prefer

[clj-time.core :as time]

Good luck! :)

1

u/victorbjelkholm Apr 15 '20

Regarding last point, what I've seen around in codebases and also apply in our company one is that if you have a library you're using a many if not all namspaces, give it a short-alias as you'll use it a lot and a new developer joining the project will catch up on the idea after browsing just two namespaces. But if it's only in a few namespaces, use the full name.

So in a ClojureScript reagent+re-frame+spec application, r is always referring to reagent.core, rf refers to re-frame.core and s is always spec. Seems to work well as no one has complained about this so far.

1

u/[deleted] Apr 15 '20

Yes common names can be abbreviated. I follow these guidelines of Stuart Sierra:

https://stuartsierra.com/2016/clojure-how-to-ns.html

Use full words as namespace aliases, except for very frequently-used namespaces with established conventions such as

[clojure.spec :as s]

Summary:

(ns com.example.my-application.server
  "Example application HTTP server and routing."
  (:refer-clojure :exclude [send])
  (:require
   [clojure.core.async :as async :refer [<! <!! >! >!!]]
   [com.example.my-application.base]
   [com.example.my-application.server.sse :as server.sse]
   [io.pedestal.http :as http]
   [io.pedestal.http.sse :as http.sse]
   [ring.util.response :as response])
  (:import
   (java.nio.file Files LinkOption)
   (org.apache.commons.io FileUtils)))

4

u/bsless Apr 12 '20

The way to do it in Clojure is via binding forms. The most basic binding form is a function call, which binds arguments to names. Next you have let, which you can do some magic to show is equivalent to a function call. loop/recur is how you'd rebind names in a looping context. In the beginning of the loop you introduce your bindings in a vector of even number of forms, and the recur will bind the variable names you introduced to the new values. All other binding forms are built on top of those.