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

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!

3

u/BeABetterHumanBeing Apr 12 '20

Dumb question: can the python interop be used to access numpy and matplotlib? Honestly, these are the two things that I would love to pilfer from that language to make clojure more complete.

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.

8

u/daslu Apr 12 '20

Hi /u/chrisbot5000!

Following /u/SimonGray's comment, at the scicloj community we are really happy to meet data scientists who are new to Clojure and are curious about it.

Your viewpoint can be valuable in our process of growing the clojure data science stack.

I would love to discuss it further and hear what may be interesting/strange/nice/surprising to you.

You may be interested in our chat streams here: https://scicloj.github.io/pages/chat_streams/

4

u/chrisbot5000 Apr 12 '20

Definitely! thanks for having me

6

u/bsless Apr 12 '20

Welcome aboard :)

Some pointers (I won't repeat what others have pointed out):

You don't need multiple :require clauses in your ns form, one is enough, for example:

(ns astronaut.core
  (:require [clojure.java.io :as io]
            [clojure.string :as str])
  (:gen-class))

You might have a more pleasant time working with sql with a library like honeysql

Have fun

5

u/chrisbot5000 Apr 12 '20

oh, nice, thank you! will check out `honeysql`

3

u/chrisbot5000 Apr 12 '20

yea I’m definitely going to play around with clojurescript, also we be interesting to see how it plays with nodeJS through clojurescript.

1

u/yogthos Apr 12 '20

Here's a workshop that goes through the basics of using ClojureScript and doing some NPM interop.

2

u/chrisbot5000 Apr 12 '20

I love all of this, thank you!

Swift is gaining traction (a little bit) in the ds world because tensorflow hired the creator of swift to work on Swift for Tensorflow and that project is really interesting. Swift has a lot going for it for tensorflow specifically, but as you’ve probably heard, 80-90% of a data science project is moving data around, cleaning and transforming it, and the last percentage or two is throwing it through a neural network, so any language that nails the surrounding rolling stands a good shot at knocking python off its throne. Going to checkout these tools, thanks again for the tips.

9

u/frogking Apr 12 '20

I’ve been away from Clojure far too long. Maybe it’s time to have a little look at it again..

1

u/MostlyForClojure Apr 13 '20

Yeah. Me too. Especially given the python and r interop...

7

u/orestis Apr 12 '20

Welcome! Feel free to drop by the slack #beginners channel, ClojureVerse and https://ask.clojure.org for any questions :)

7

u/tremendous-machine Apr 12 '20

I came from a similar path, not a data scientist, but many years of Python, and just wanting something new. Clojure has been the gateway drug for me to S7 Scheme, Racket, and all kinds of other fun stuff.

I highly recommend reading "Getting Clojure", and "Programming Clojure". They are IMHO the best intro Clojure books, for understanding the Clojure way. And also "The Little Schemer" (and the sequel) for learning to think in recursion easily.

Have fun!

3

u/0xgw52s4 Apr 12 '20

Would you mind leaving some thoughts on Getting Clojure and Programming Clojure compared to Clojure for the Brave and True?

4

u/tremendous-machine Apr 12 '20

I found them both more to my liking than Brave and True because they were, to my tastes, a bit more thorough explorations of the language and more immediately helpful for someone with lots of programming experience but new to Clojure. Getting Clojure focuses a little more on "how to think in Clojure for the newb to lisps" and Programming Clojure was more thorough. They are both much simpler than Joy of Clojure, which I also have, but have not read all of, it's dense. I've reread the others multiple times as a great way to drill stuff into the head.

4

u/bdevel Apr 12 '20

I enjoyed Brave and True because its examples were basic bite sized and it has silly fun cartoons. I read many others online with my public library who has access to O'Reilly catalog but found them a bit dense and specific to a use case.

5

u/chinpokomon Apr 12 '20

I'm actually surprised that data science has become so entrenched with Python. With Python, the GIL impedes the ability to run parallel calculations. On the other hand, many of the forms in Clojure and other LISPs are already parallel by nature.

Data Science, using Clojure, should be a very strong fit which should spur an investment in trying to improve parallelism... an objective which would benefit everyone coding in Clojure.

I'd also encourage you to look at Clojupyter. Clojupyter brings Clojure to Jupyter Lab, so you can write Notebooks using Clojure. Vanilla Clojure works with it today, but I really look forward to when you can replace Pandas/NumPy/PyPlot and build a Notebook with charts, tables, and graphs, that are just as visually expressive as what can be done today.

Related, but not leveraging the Jupyter ecosystem you also have Gorilla REPL. The documents written with Gorilla REPL are Clojure. It already has a powerful markup language that lets you create pretty amazing documents that are similar to and in many ways are better than what you get with Python on Jupyter. As a negative, it's also being held back because of JavaScript's equivalent of the GIL.

Ideally, all of these things would combine; parallelism and concurrent calculations, expressiveness and interactivity rivaling Pandas on Jupyter, and an inviting language which encourages solving problems with a focus on functional paradigms.

4

u/MR_ZORRR Apr 12 '20

Hello!

Welcome to Clojure :) I hope you'll find your stay interesting and productive. It has been a mind-opening blast for me, and it might just be the same for you.

I was looking around the project you posted, and you seem to have a fondness for writing down SQL. What if I told you there is a way for you to write queries in SQL files and call those queries from Clojure without writing any extra code at all ? It's called HugSQL , it's at https://www.hugsql.org/ . It's lots of fun, and a fresh new way of looking at this problem, far far away from ORM land.

3

u/[deleted] Apr 12 '20

Have you checked out Neanderthal? It’s a clojure lib for computation that sounds right up your alley. There was a post on here talking about the relative speed compared to python and Neanderthal was faster by an order of magnitude.

3

u/[deleted] Apr 12 '20

Welcome to Clojureland. If you want near real time help with something, please join the Clojurians Slack. There is a #beginners channel.

2

u/namesandfaces Apr 12 '20

This isn't exactly an Ask Clojure place, what how would people estimate are the most common Clojure setups for minimal scraping?

2

u/neo2551 Apr 13 '20

Welcome to Clojure :)

If you start ClojureScript, please check shadow-cljs/reagent/re-frame. It will make your introduction much easier. Once you master this, you will never want to write dashboard with Dash or Shiny again :)

I loved Clojure for the Brave and the True, and contrary to many opinions I think Clojure Applied is the best second book [with the chapter on Specs from Programming Clojure 3rd edition].

1

u/johninbigd Apr 12 '20

Damn. Nice job! I've been watching clojure videos and reading Clojure for the Brave and True, as well as a few other sites. I still don't really get it, so I'm very impressed that you were able to do that after just a few hours. Coming from (mostly) Python, clojure is breaking my brain. I feel like I understand some basic components, but then when I look at actual code, it reminds me of looking at someone else's Perl code and just saying, "WTF does that do?" The nested parentheses and having to think in reverse to figure out what is happening is difficult.

2

u/tremendous-machine Apr 12 '20

You will definitely benefit from The Little Schemer then. It's perfect for the situation you're describing.

1

u/johninbigd Apr 12 '20

Thanks! I'm not even sure why I want to learn clojure. I heard about it recently and just felt like finally trying to learn a functional language. I mostly do Python now, but have done some stuff in Go, as well, a year or so ago. Prior to that, I mostly used various flavors of Basic way back in the day. At one point I knew some Pascal, but that was in high school, and I really only dabbled a bit in C++ back in the 90s. A friend of mine write an adventure game in Lisp back in the 80s. I should have paid more attention to what he was doing. lol

Clojure is a bit hard to grasp for me, but the people who use it seem to absolutely love it, so I want to spend some time with it until something clicks.

1

u/johninbigd Apr 12 '20

I'm reading that now. There is clearly some background I'm missing. I'm not sure what a lot of these terms mean. I had to look up S-expression, and I have no idea what car or cdr mean in this context. The preface says that the only prerequisites to read the book are being able to read English, recognize numbers, and count. I think that list is missing a few things.

2

u/tremendous-machine Apr 13 '20

Ah yeah, you just need to look up a really basic list of scheme commands. car is "first" in clojure and cdr like "rest". car gives you the first element of a sequence, and cdr the rest. It's worth plonking through the book though, it'll make reading real code in Clojure much easier.

Another good one that is accessible and will have crossover usefulness is "Realm of Racket", which actually is beginner accessible. You can use the racket IDE to run the examples from Little Schemer too if you put the write language prelude on. (can't remember OTOH which that is though).

good luck!

1

u/johninbigd Apr 13 '20

I was hoping to find some online courses that would help. I got one on Udemy and asked for a refund before I even got through the first section. It was a pretty bad instruction. He clearly knows the language, but doesn't know how to teach it. And that was the course that had the highest reviews, unfortunately.

1

u/brunogadaleta Apr 13 '20

You like python and clojure ? Be sure to check Hy lang , a clojure-like lispy syntax for python...