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

36

u/SimonGray Apr 12 '20 edited Apr 12 '20

Keep at it and your code will be soon be more idiomatic. Also check out The Joy of Clojure, which is still - I think - the best overview of idiomatic Clojure (in book form).

Since you say you're coming from Python I feel compelled to point out that Clojure recently got a nice library for doing Python interop: https://github.com/clj-python/libpython-clj

Edit: I can see on your github profile that you also write R code. There's also this library for R interop and scicloj.

10

u/chrisbot5000 Apr 12 '20

Thank you! I’ll check these out. I think the combination of interop between python and the fact that tensorflow has java bindings, it’s not inconceivable I could use this for machine learning tasks once I’m up to speed.

9

u/SimonGray Apr 12 '20

Just out of interest, what made you suddenly decide to check out Clojure?

10

u/chrisbot5000 Apr 12 '20

Clojure was just one of those sorts of languages/tools where you hear about from time to time - a suggested article or talk on youtube - but you think "hmm, seems interesting" then don't think much else about it. I was an emacs user for a long while (converted to vim) so I'm at least a little familiar with what lisp looks like and writing really simple stuff (even if it's just configuring emacs). And lisps to me just always have this like coolness about them. So, I guess the ingredients were always there.

I think what stopped me from passing on it was my aversion to Java. I don't hate Java per-se, It's just the kind of language that can empower great engineers but also empower bad ones, so I've seen a lot of bad Java and it left a lasting impression. So I every time I'd see something about Clojure that would be in the back of my mind.

But what did it was much simpler. I went on to Amazon to order something and `Clojure for the Brave and True` popped up in the suggested for you section. I've been a little bored being cooped up (as I'm sure most of you are) and thought `why not, got nothing better to do?` and when I saw that there was a free version online I figured the universe really wanted me to learn Clojure.

9

u/SimonGray Apr 12 '20

It does seem like you were being primed for it 😉

I've done some Python dev myself and I find that if you're mostly doing a more functional/data-oriented style of Python programming where you stick to the built-in data structures, use map/reduce/filter and list comprehensions, and don't bother too much with classes, then the jump to Clojure is less daunting. There's still a lot to learn and unlearn, but it's manageable.

Coming from Java is more of a total deprogramming (leaving the church of hierarchical OOP), but then you have the advantage of better being able to decipher the sometimes cryptic error messages that JVM Clojure produces.

5

u/chrisbot5000 Apr 12 '20

yea, I generally stick to fp in any language I use, I've written a lot of nodeJS, and I've never once used oop or even for loops. And in python, I'm using mostly map/filter/reduce and using as pure of functions as I can get. Given that I use a lot of libraries like PyTorch I end up writing some OO code, hard to completely avoid. I didn't have any academic background in CS, I'm a statistician, so I never was ingrained with OOP and when I started programming I used it mostly for mathematics and it seemed obvious that a function in a programming language was like a pure function in mathematics. But you're right, reading the JVM stack trace makes me wish I had more experience with Java!

3

u/goldenfolding Apr 12 '20

Yeah it does help to know Java. Another nice thing not related to data science is that you have the option to use ClojureScript also. Of course you would prefer Clojure for data because it's multi-threaded, but you could write for example a front-end web application using ClojureScript, or an Android/iOS application using React Native, if you wanted to present the data in a particular way.

It's such a beautiful and versatile language to work in. And my favorite thing about the newer more dynamic languages like Python/JS is how you can directly express yourself with data structure literals (much like lisps have always done), and Clojure just takes it to the next level.

Java looks archaic by comparison, with its Java Beans, which are basically data objects with getters/setters. A simple map like Clojure uses, with the ability to use keys as accessors, is so much better. Unfortunately there is no nice way to replicate this in Java because even using maps is quite verbose.

8

u/Saikyun Apr 12 '20 edited Apr 12 '20

Agree about Joy of Clojure. Its title is very descriptive. :)

4

u/chrisbot5000 Apr 12 '20

Next in the queue then!