r/Clojure • u/chrisbot5000 • 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
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
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! :)
→ More replies (2)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/recuris 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
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
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
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
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
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?
3
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
carorcdrmean 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!
→ More replies (1)
1
u/brunogadaleta Apr 13 '20
You like python and clojure ? Be sure to check Hy lang , a clojure-like lispy syntax for python...
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.