Live data from Hacker News

Clojure & Python, Side by Side

tin.nu

21–30 of 67 posts

Re: Clojure & Python, Side by Side

#21
post #7

Earlier quoted context omitted.

This occurred to me after I finished writing that post.

Yea... I'm still confused as to the whole point of pure/near-pure functional programming if we're not getting better code density/more productivity against other languages which I (and some others) find easier to work with. I guess all I'm saying is "convince me!" :) I'm all ears.

The big differences between Clojure and Python are probably twofold: it's a Lisp, and it focuses on getting concurrency right. We've had Lisps for a while, and Python only really lacks Lisp macros, so let's think about the concurrency stuff...

Clojure's design goals combine some deep ideas about identity, state and time together and includes data structures and a concurrency API that supports these goals. It's going to be hard for anyone to just convince you in a comment like this, and even if they could, I'm not the one to do it as I'm a Clojure noob.

You should read this essay http://clojure.org/state and watch this talk http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hic....

I think you will find that Clojure is pretty interesting, even if you don't start using it right away. I apologize for just throwing you links, but I can't do it justice, and Rich Hickey is a great lecturer, so you'll get more from his stuff.

Re: Clojure & Python, Side by Side

#22
post #4

This is pretty cool. I know python well enough to hack my way around it. I'm probably missing something, but now I have to ask: what would be the point in me learning clojure?

Clojure has excellent concurrency support.

Clojure has a focus on functional programming, immutable data[1] and the sequence abstraction[2].

Clojure is a Lisp and has most[3] of the super-powerful metaprogramming facilities that you would expect from a Lisp. This means you can, with enough effort, mold the language to suit your needs, even turning it into a completely different language should you wish to do so.

Clojure is not object oriented, though it does allow you to instantiate and create Java objects for the purpose of interacting with Java libraries. Clojure does support OO-like code through its Records/Protocol system.

Clojure has out-of-the-box support for multimethods.

Clojure runs on the JVM and therefore has access to the many many libraries written in Java or other JVM languages.

Pure Clojure code is, as far as I can tell, higher performance than pure CPython code.

PS: Just for the record, I love Python and use it a lot (and have actually been out of touch with Clojure for almost nine months now, sadly). The above are reasons why you may want to learn Clojure - not reasons to drop Python.

[1] The benefits on pure-functional code and immutable data are that it makes concurrency easier and safer and that it makes code easier to reason about as you do not need to consider that 1) side-effects are happening behind your back and 2) the data is being changed elsewhere (which is also why it makes concurrency easier).

[2] Similar idea to Pythons iterables, except IMHO Clojure's sequences are much more a part of the core language than iterables in Python, in that in Clojure most things are sequences, while in Python this isn't the case (though a lot of things are iterables).

[3] I say most because Clojure does not have reader macros

Re: Clojure & Python, Side by Side

#24
post #7

Earlier quoted context omitted.

This occurred to me after I finished writing that post.

Yea... I'm still confused as to the whole point of pure/near-pure functional programming if we're not getting better code density/more productivity against other languages which I (and some others) find easier to work with. I guess all I'm saying is "convince me!" :) I'm all ears.

getting better code density/more productivity

Its not so much about brevity or productivity as it is about correctness. Functional programming and immutable data makes it easier to reason about code and to prevent bugs/errors, especially in the context of concurrent/multicore code.

Re: Clojure & Python, Side by Side

#25

Earlier quoted context omitted.

Unfortunately, Jython is at 2.5.2, 3.5 years behind cpython.

That is certainly a problem. But there are very few language constructs in Python >= 2.6 that are missing in Python 2.5, and the really missing features (e.g, multiprocessing module) have better alternatives in the JVM, or are redundant. You lose some portability, but Jython 2.5 doesn't seem to be that limited in capabilities. Anyways, YMMV. All I got to do once was set my Django application to run on Jython, and tha…

While I'd agree Python 2.5 to 2.7 doesn't bring a swath of language features with it... it's a good sign of Jython's slowing/sluggish growth/development.

Personally, I want to use a language implementation that will be around and (well!) supported for many years.

Re: Clojure & Python, Side by Side

#27
post #3

I love this format. Can anyone point to more "Rosetta Stone" examples like this? (Aside from ever-prevalent "X in Coffeescript compiles to Y in Javascript")

Not using the side by side format, sorry.

http://www.nevercertain.com/2011/08/17/scala-ruby-rosetta.ht...

Re: Clojure & Python, Side by Side

#28

This is really neat. Although, one thing that makes it easier to map this program from Python to Clojure is that the original program was written in a pretty functional style. It would be interesting to see if a more imperative sample program, or one using more object-oriented features, would map similarly.

I agree. For some things, like more mathy things that only perform one function, it seems like functional programming is the way to go. However, for HUGE programs based on data and complex interactions between and extensions of objects, it seems like a more object-oriented style would be better... all about the right tool for the job, I guess.

Re: Clojure & Python, Side by Side

#29
post #17

Would love to see this in Ruby

Very much agreed. Although clojure might be faster, it is so so difficult to read, and from a rubyist's POV, looks like a disaster compared to python. Maybe that's just a result of being spoiled by super clean syntax and almost human readable methods... but that's gotta count for something
Post reply on HN