9 ms·
Clojure & Python, Side by Side
- timf 15y agoSee the previous discussion (about Justin's blog post): http://news.ycombinator.com/item?id=2386575 http://news.ycombinator.com/item?id=2386575
- storborg 15y agoI 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")
- rw 15y agoIn 30 languages: http://rosettacode.org/wiki/Sudoku http://rosettacode.org/wiki/Sudoku
- jbrechtel 15y agoNot using the side by side format, sorry. http://www.nevercertain.com/2011/08/17/scala-ruby-rosetta.html http://www.nevercertain.com/2011/08/17/scala-ruby-rosetta.ht...
- genieyclo 15y agohttp://hyperpolyglot.org/ http://hyperpolyglot.org/
- dorkitude 15y agoSQL and MongoDB MapReduce side by side: http://dbpedias.com/w/images/9/90/MongoDB.JPG http://dbpedias.com/w/images/9/90/MongoDB.JPG
- beefman 15y agohttp://en.literateprograms.org/Miller-Rabin_primality_test_%28Clojure%29 http://en.literateprograms.org/Miller-Rabin_primality_test_%...
- brendano 15y agoMatlab, R, NumPy: http://mathesaurus.sourceforge.net/ http://mathesaurus.sourceforge.net/
- skylan_q 15y agoThis 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?
- tdfx 15y agoLets you target the JVM without writing Java, among other things.
- rglullis 15y agocough http://www.jython.org/ http://www.jython.org/ cough
- tdfx 15y agoThis occurred to me after I finished writing that post.
- skylan_q 15y agoYea... 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.
- rayiner 15y agoI mean the answer is that the Python example is nearly purely functional to begin with. It uses list comprehensions heavily and I couldn't find any mutating assignments in a quick glance of the code. Which is not surprising, since it was written by Peter Norvig, who wrote a very popular Lisp book. I think what Clojure can offer on top of that is macros, the advantages of which are really only evident on bigger or harder problems.
- jkkramer 15y ago
- jphackworth 15y agoThis 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.
- phreakhead 15y agoI 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.
- asmala 15y agoI think you're absolutely right that functional programming is particularly suited for things like mathematics. But you can also enjoy many of the benefits of a functional approach by moving most of the complexity into purely functional, well, functions and leaving the state-modifying code fairly simple. For a good pragmatic example of this approach, check out the snake game in Programming Clojure v2 from Pragmatic Programmers. Coming from Ruby this approach was a bit mind-boggling at first, but even with my fairly limited Clojure exposure (or should that be expojure?) I've come to appreciate how easy pure functions are to test and to reason about.
- amalag 15y agoWould love to see this in Ruby
- jenius 15y agoVery 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
- fbru02 15y agoI'm a rubyist and now Clojure convert. The code does not look difficult to me, but i'm sure you can make it much more concise if you were to add some mutation in the Clojure code. As my career progressed i found that I don't care almost at all about syntax but semantics. Clojure offers far better semantics for almost everything including Data Structures, concurrency and meta-programming. That's were the true power of the language is on its semantics.
- oacgnol 15y agoIs there a site out there that has common implementations of problems using different languages? I would love to be able to compare.
- karavelov 15y agohttp://rosettacode.org/wiki/Category:Solutions_by_Programming_Task http://rosettacode.org/wiki/Category:Solutions_by_Programmin...
- phreakhead 15y agoPython looks like a lot less typing.
- cageface 15y agoAnd much more readable, without the parenthesis balancing dance.
- AlexBaranosky 15y agoJust like English is soooo much more readable than German, because well, I don't know German :)
- kleiba 15y agoTrue, but for instance I know neither Python nor Clojure, and yet Python looks more accessible on first glance - even though I have some experience with other LISPs.
- cageface 15y agoI know Common Lisp well, thanks. Only dabbled with Clojure.
- ShardPhoenix 15y agoI found the Python more readable even though I have written programs in Clojure and never written programs in Python. In fact I'd be willing to bet that Python-like syntax would be found to be more readable than Lisp-like syntax for programmers with N years of experience in that language, if such a study could be conducted. (ie, I'd bet that an average programmer with 5 years of Python experience and nothing else can correctly read programs faster than an average programmer with 5 years of Clojure experience and nothing else (not that the latter exists)).
- jpeterson 15y agoFunny, I was thinking the opposite -- the Clojure code is far more readable.
- 15y ago
- rmnoon 15y agoIn addition to performance metrics, this site has implementations of their benchmarks in all the languages they test. It's not pretty and side-by-side, but it's fascinating. http://shootout.alioth.debian.org/ http://shootout.alioth.debian.org/
- igouy 15y agoMake it side-by-side :-) http://shootout.alioth.debian.org/help.php#twowindows http://shootout.alioth.debian.org/help.php#twowindows
- danbmil99 15y agoThe parentheses -- my eyes -- pain
- schiptsov 15y agoDon't you see how noisy and unreadable and clojure's code is? =)
- protez 15y agoLISP is the coolest language ever and maybe forever, in theory. In practice, it also works phenomenal, but just for 1%.
- viandante 15y agoMeh, I am an amateur when it comes to programming and still I am loving clojure over python. Truth is that when you are using lists all the time, lisp seems just more natural. All those for and if in object oriented languages feel pretty awkward when you manipulate lists and hashes. Just an amateur opinion although.
- phatbyte 15y agoI can only admire the persistance (free time) of someone coding in Clojure. Everytime someone says to me "I hate Objective-C syntax" I show them Clojure... If I want fast performance I code in C or other low level language. Sorry Clojure community, I'm not drinking the kool aid this time.
- chris_j 15y agoWhat makes you specifically mention fast performance? I wasn't aware that this was the most important selling point of Clojure. What kool aid is it that you won't drink this time? For me, Clojure is great because it allows you to build powerful abstractions and has some interesting concurrency constructs, while running on the JVM and making it easy to interoperate with Java code. Of course, being a Lisp, it uses Lisp syntax.
- jwr 15y agoWhat's really unique about the Clojure community is that (as opposed to many other language communities) we don't respond to vague handwaving-type flamebaits such as this one. Most of us just get stuff done using Clojure, quickly, getting concise and most importantly — correct code, even in massively multithreaded applications.
- swannodette 15y agoSide by side assumes that you would write the Clojure solution this way. But that's not how I (nor, I imagine, any experienced Lisper) would do it. Lisp programmers are not afraid to write compilers in Lisp targeting Lisp. This is not something a Python programmer would consider doing. I would implement Dan Friedman et al's Scheme constraint solver (cKanren) in Clojure (which can be done in a few hours) so I could solve all sorts of problems, not just Sudoku, in 80 lines of code instead of 200 with very good performance.
- asmala 15y agoGreat, I was wondering what would be an idiomatic way of solving this in Clojure. Given the addition of core.logic into Clojure 1.3.0, one could even skip the implementation of cKanren. Although to be honest I'm not altogether sure how core.logic measures up to cKanren.
- swannodette 15y agoI'm the main developer of core.logic :) core.logic is simply missing cKanren constraint solving extensions at the moment, but again they won't be hard to add since core.logic is a fairly faithful interpretation of miniKanren (the system which cKanren extends).