Hacker News new | past | comments | ask | show | jobs | submit login
Clojure Distilled (yogthos.github.io)
168 points by nickmain on Sept 26, 2014 | hide | past | favorite | 51 comments



I am really suggesting to try some classic Scheme course first, like my favorite CS61A by Brian Harvey, or that wonderful Racket-based course (what's the difference?) on Coursera by Gregor Kiczales (which is worth watching even if you are mere a brainwashed Java dev).

Another way is through pg's books, especially, of course, "On Lisp" and Arc tutorial and then arc.arc (original version from the 2009 arc3.tar which isn't cluttered with silly docstrings and other nonsense).

There is also the way of CL. For that there is pg's ANSI CL, but before that a much better "CL Language Concepts" from Symbolics (pdfs are available from bitsavers).

Then, perhaps, one will have slightly different view of what Clojure is really. Yes, it is a real marvel of software craftsmanship, especially that it was originally a solo effort, but no, there is no miracles in it.)


    which is worth watching even if you are mere a brainwashed Java dev
and this is why so many people are repelled by the Lisp (or Scheme) evangelism. I don't particularly like Java, but I use it for Android development. Calling people brainwashed because they are using Java is simply rude.


In my opinion Java is the biggest fraud and waste in the history of IT (next comes SAP) - nothing but hype, ignorant parroting of silly memes by "managers" (so-called bandwagon and peer effects) lies, and taking advantage of ignorant.

People who "know no better" (cannot even visit the rosettacode.org, dismissed plain old C - these evil pointers!, Erlang - functional programming is for academy, or CL - not an enterprise level, etc, etc) are, well, brainwashed.

I am not blaming the people, I am just describing reality as I see it, and my view is by no means new or even "original".

Java was just a way to make money (in 2000s) like "derivatives traders" in the last 10 years. Same manipulations, same techniques.

For example, almost all of the very few popular apps I have installed on my Nexus4 are NDK apps - FB, Viber, Skype - you name it.


Java is just a programming tool and, while I would have preferred to use C++14 for all my Android projects, it is what it is (NDK make sense only for heavy duty applications).

Now, imagine for a second that you are a junior programmer, that only knows Java. You've heard of this thing called Clojure and you want to read and learn more about it. What would you think if someone called you brainwashed just because you only know Java ? For most people this means EOD (end of discussion). Calling someone brainwashed just because he doesn't have your level of expertise with other languages will just make it ignore the good parts of your comment.

In the end it is about what is more important to you: vent some steam about how much you hate Java or guide someone to learn more about a Lisp language.


Not so long ago a student of a good school in US would know Scheme and C, not Java or Python, and the move to Java as the language for teaching CS is, in my opinion, the result of the same regrettable process of brainwashing.

btw, I never talk Java issues in a context of a personally, only in a broader sense of a "social phenomena" partly explained by well studied "effects" in social psychology.

Unfortunately, Clojure will not be suitable for Android development in observable future exactly because "Java" cannot fulfill its promises - Clojure's runtime is "too heavy and too slow" (too wasteful) to run even on a newest smartphone hardware, and in this, again, so-called reality re-asserted itself.


> which is worth watching even if you are mere a brainwashed Java dev

And people wonder why Lisp advocates have such a bad reputation.


> even if you are mere a brainwashed Java dev

I see where you're coming from, SLW.


I've been working with Clojure for 18 months now, after a bit more than 10 years of Java. I'm having so much fun I don't think I could ever go back.

Also, this is an excellent summary / introduction to key clojure concepts, well done to the author.


Seconded. I was working with a mix of Java and Ruby for roughly a decade. I've been working full time with Clojure for about two years now, and on and off for a couple of years before that.

Fantastic introductory guide. Clojure really needs something like this. There are some good books on Clojure but I find they are aimed at either the determined beginner or the experienced (or both.) This though I feel I could pass around to anyone that wants to get an idea of what Clojure is about.


I'm not done reading but it seems like a very nice introduction to people not familiar with the function style of programming.

This is also one of the few document that can be converted from html to epub without much pain. It looks quite good with

    pandoc ClojureDistilled.html -o ClojureDistilled.epub --indented-code-classes=clojure


I love clojure, and dream of the day I can work with it.

But do you know why more people get into ruby or js? It's community is beginner friendly. Most texts on the subject of lisps feel like wannabe dissertations, preferably with the Computer Modern font and 5 pages long.



I would never suggest a programmer learn Clojure as their first language, because of the enormous Java stacktraces. Then there's the setup involved (Java, usually Leiningen) to get to "Hello, World" compared to Python or Ruby. Just too many places where the student can get seriously stuck. Clojure's strengths (immutable structures, expression freedom, macros, Java interop, JVM) are not relevant to the new programmer.


Yes, the Java stack traces are probably the worst "ugly" thing in Clojure but from my experience the simplicity of syntax (not to be confused with familiarity, familiar != simple) makes it much easier for someone who's never programmed before to start writing Clojure code vs many other languages where there's a lot of syntax to that you simply need to "know" to get going.

Also, on a more advanced note, I've used https://github.com/ptaoussanis/timbre with great success to get nicely formatted and colored Clojure errors and stack traces.


Doesn't have to be just a dream, check out HN's Who's Hiring for September, plenty of opportunities to have your everyday work be in Clojure out there, including our company.


Extracting keys from maps into a definition, with common-lisp (using my macro defk)

(defk login (user pass) (and (string= user "Bob") (string= pass "secret")))

(login (list-to-hash '(user "Bob" pass "secret")) (list-to-hash '(user "Bob" pass "secret"))

Given the following definitions:

(defmacro defk(name h-list &rest body) `(defun ,name (h) (let ,h-list ,@(loop for k in h-list collect `(setq ,k (gethash (quote ,k) h))) ,@body)))

(defun list-to-hash(list) (let ((h (make-hash-table))) (loop for (k v) on list by #'cddr do (setf (gethash k h) v)) h))


It's probably more idiomatic to implement something similar to the with-slots macro by using symbol-macrolet:

  (defmacro with-keys (keys table &body body)
    (let ((gtable (gensym)))
      `(let ((,gtable ,table))
         (symbol-macrolet ,(loop for k in keys
                                 collect `(,k (gethash ',k ,gtable)))
           ,@body))))
This way you can both access and set the keys:

  CL-USER> (defparameter *table* (make-hash-table))
  *TABLE*
  CL-USER> (setf (gethash 'a *table*) 10
                 (gethash 'b *table*) 20)
  20
  CL-USER> (with-keys (a b) *table*
             (list a b))
  (10 20)
  CL-USER> (with-keys (a b) *table*
             (setf a 100
                   b 200))
  200
  CL-USER> (list (gethash 'a *table*)
                 (gethash 'b *table*))
  (100 200)
If you really want to destructure a hash-table when its an argument, it is not much harder to write a new version of defun on top of with-keys or zeroyzeroa's solution (his/hers only takes a single argument).


I'm currently learning Clojure. For all the "aha!" moments, there are plenty of "WTFs?", too. Not that it's a fault of the language, it's just that it requires a different way of looking at things. I've only skimmed this article, but look forward to reading it in depth later. Thanks!

I'd be interested if anyone has some similar pieces to share.


Most of my Clojure WTFs were not related to the language itself, but rather the tooling. This was years ago, but everyone used to say, "Before diving into Clojure, you should really learn emacs and all these other things first." I'm glad I ignored them and sputtered along in a text editor while learning the basics of the language, because I would've otherwise burnt out.

With apps like Light Table now available, maybe beginners have an easier ramp these days.


I never got into Emacs myself, I started with Eclipse and then moved to Cursive (https://cursiveclojure.com/) which I simply can't recommend enough, I also use Light Table for some of my smaller projects.

The fact that you don't have to use Emacs to work with Clojure is a huge benefit in my opinion. Emacs takes a lot of investment to learn and use effectively, and not everybody has the time, nor the inclination to do that.


Good to know! I started with emacs, but decided to use Light Table for now. Nothing against emacs, but I don't want to make things harder by learning a new language and a new editor at the same time. Aside from Light Table (and presumably Leiningen) do you have any other tooling recommendations?


I will say that cider has done a very nice job of getting interactive development using Emacs to work well. I am a 20+ year Emacs user and love working with Clojure via cider. I will say that I think getting started with Light Table is a great way to start, but don't leave Emacs too far behind!

Good luck go you!


I also am a long time emacs user, but whenever a plugin freezes emacs I immediately stop using it. Is is cider now stable? I know nrepl.el would crash emacs frequently if you asked it to evaluate the wrong thing.


I'm not sure how long this feature has been around, but C-c C-b will abort any running evaluation(s)... I've used it more than once to save a non-responsive cider repl session.


Well I've been using cider / nrepl.el for a long time and never knew this. Don't know how I missed it.

Thank you.


I've never had cider or the old nrepl.el crash or freeze on me, they've always worked fine. I develop on Mac OSX and Linux, what platform are you on? And what Emacs version? I tend to be on the latest GNU releases. Are you using a GNU Emacs or something else? If you're on Mac, I highly recommend this distribution [1]

[1] -- http://emacsformacosx.com


I'm on the latest release of GNU emacs. I'm mostly on Windows, which could be the issue.


I use Emacs on Windows too, but I don't develop there, it's mostly a glorified notepad for initial analysis of log files, so I can't really help there. I would recommend getting melpa setup on your emacs and keep cider as up-to-date as possible to see if that helps, assuming you're not doing that already either via melpa or directly from source.

Good luck to you!


For emacs/clojure users I highly recommend Sam Aaron's Emacs Live. It is an "opinionated" emacs setup and while some of the opinions might not be to your liking, it is really easy to set up and get going with clojure using it.


I really like IntelliJ with cursive:

https://cursiveclojure.com/userguide/


I chose to learn both at the same time, and would recommend it if you're willing to buckle down for a month or two of frustration. I really feel like I further developed my ability to go head first into learning in a way I had in the past. Still learning a lot though about both. I should mention that I'm a freelancer; if I was an employee it might be hard to get a way with spending so much time learning rather than producing in the short-term.


Definitely recommend people try Cursive Clojure, the clojure Intellij plugin. Much easier to use/setup than emacs IMHO. Lots of clojure people have been switching.


Nicer debugging experience too.


My personal favorite editor is IntelliJ IDEA Community Edition + cursiveclojure.com plugin (not affiliated with the cursiveclojure.com people but highly recommend it). I don't come from an Emacs background but from what I've seen and heard from Emacs people is that it combines all the best parts of Emacs when it comes to Lisp code manipulation.


I'm the developer of Cursive - thanks to you and others in this thread, I'm really glad you're all liking it!


So I'm about half way through it now. I'm really impressed with how much information it's conveying in a succinct form. I'm going through Clojure for the Brave and True, and think this supplements it nicely.

There are a few typos, but the most glaring one was the incomplete python code underneath the "Code Structure" heading:

l = [1, 2, 3, 4, 5]

for i in l i = i*i

for i in l if (i mod 2 == 0)

print l

I assume the author wanted to show how the list was mutated. And those conditionals need trailing colons :)


thanks, I just fixed that, I'm not a python guy as you can tell :)


  Higher order functions can be easily chained 
  together to create complex transformations:

  (filter even?
    (map #(* 3 %) [1 2 3 4 5])) 
  =>(6 12)
This will need to be updated to make use of transducers, I guess.


Only if it enhances the poetic value.


It's a very good summary.


When using rawgit, please follow the owners request about using cdn.rawgit.com for anything that might result in heavy traffic. I guess links submitted to HN often results in heavy traffic.

https://rawgit.com/faq#diff-between-rawgit-and-cdn


So the url should be changed to this (which loads for me):

https://cdn.rawgit.com/yogthos/yogthos.github.com/master/Clo...


Would it really be so difficult to redirect my request...


That link is down for me, but I think this is the same content?

http://yogthos.github.io/ClojureDistilled.html




But the "real" URL also would have proper CSS etc. and looks slightly less broken.


Noted, thanks!


The main link's stopped working with a message to use the cdn version instead:

https://cdn.rawgit.com/yogthos/yogthos.github.com/master/Clo...



I'm really enjoying reading this - thanks!

For those that don't want to tax the owner of rawgit - try github pages - it's just HTML, right? http://yogthos.github.io/ClojureDistilled.html




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: