From someone that has never touched clojure ( or any lisp) i would say that the python equivalent using either/right libraries made me feel like running away as far as i could.
Could anyone provide me with an example of a code that would actually read nicer in clojure than in python ? Make it as arbitrary as you'd like, i'm honestly trying to understand
. i've read enough about lisp bluring the line between data and code, so i'm starting to get an intuition of its benefit. I was just hoping to finaly see a real world example, and i'm a bit disappointed.
Nicer to read is pretty subjective, but I'll tell you why I've switched 2 projects from Python and Ruby: speed. Our systems just had too much to crunch through, and the global interpreter lock pretty much killed us. We switched over to Clojure in 2009 and never looked back. In the early days, we had to wrap a lot of Java, but it's been several years since that was necessary. Currently, Clojure + Kafka + Apache Storm is the scaleability trifecta.
One final comment, which is often tragically overlooked, in my opinion: ease of deployment. Package management and deployment often sucks in other languages (especially in Ruby). By contrast, Leiningen is a SWEET dependency management tool, and the built-on-Java approach leaves you with a jar that you scp and you're done.
I do most of my work in python, and don't plan on switching any time soon, but after playing with Clojure I sure do miss leiningen over the mess that is pip / virtualenv / conda / zomgihatepackagementinpython.
anecdotally, a lot of go's adoption was likewise driven by python and ruby programmers looking for speed while still retaining some "high-level language" features.
A side-by-side comparison of the implementation of some nontrivial software or algorithm between Clojure and Python would be misguided. These two languages have sufficiently different paradigms that a direct port of a nontrivial program from one language to the other would end up in a misuse of the target language's abstractions and idioms.
Clojure is a Lisp-like language and would be well suited to taking an input and applying successive transformations to it without keeping state along the way. Meanwhile, Python mixes imperative and functional paradigms, and managing state comes a bit easier. In particular, the Either monad is much more natural an idea in Clojure than it is in Python, and indeed does not suit Python well at all.
You asked for an example in which Clojure would "read nicer" than Python. Consider the task of being given a long string of (natural language) text and
1. splitting it into sentences,
2. tokenizing those sentences into words,
3. attaching part of speech tags to each word.
Implementing in Clojure, imagine we have ready-made functions called splitSentences (which takes a string and returns a list of strings containing sentences), tokenize (which takes a string containing a sentence and returns a list of words in that sentence), and tag (which takes a list of words and returns a list of 2-tuples whose entries are those words along with their part of speech tags). In Clojure, this might look like
(map tag (map tokenize (splitSentences text)))
In Python, this might look like
sentences = splitSentences(text)
tokenizedSentences = [tokenize(sentence) for sentence in sentences]
taggedTokenizedSentences = []
for tokenizedSentence in tokenizedSentences:
taggedTokenizedSentences.append([tag(word) for word in tokenizedSentence])
I don't think there's any magical piece of code that is objectively prettier in Clojure than in Python. Syntax is just syntax. Honestly, the homoiconicity of Lisps is nice and all but it's not something that often matters in practice. The syntax looks funky for the first three days you write Lisp, then it just fades into background like with most other languages. I guess Lisp grammar is particularly elegant because it's so small but still allows great expressive power. It doesn't get in the way.
You're not insane. You will be hard-pressed to find a side-by-side comparison of Clojure and Python. I say this for two reasons:
1. A program in Clojure should not necessarily look the same as one in another language. Oftentimes the overall program architecture is going to be very different.
2. For the cases where you can translate certain concepts (example, casting a string to an int) -- oftentimes Clojure code might come across as pretty hideous and verbose.
That Clojure code looks like what I would write: non-idiomatic and procedural.
EDIT: greyrest's code looks more idiomatic and thus way shorter but still understandable.
I have been using Lisp languages, off and on, since the 1980s and generally like them. That said, if you like Python and are effective with it, there is no reason to try Clojure unless you want to for fun. I have had to use Python a lot in the last year for Tensorflow and it is certainly a nice enough language.
There are a wealth of information about lisp variants, clojure, and functional programming. If you did some simple research you would not be so disappointed.
Lisp has been around decades, surely you can find some "real world" examples that you can learn from.
Personally, I started reading the clojure documentation and going through 4clojure https://www.4clojure.com/ , a kind of simple programming introduction problems to clojure
People have solved all the questions, and you can find them online really really easily
Just do some research, and come back once you have done a basic amount of work, and can no longer say "I've never touched clojure".
Instead, come back and say "I did some research, I worked through some 4clojure questions, and I still don't understand..." then I think you'll get a more productive discussion.
Could anyone provide me with an example of a code that would actually read nicer in clojure than in python ? Make it as arbitrary as you'd like, i'm honestly trying to understand . i've read enough about lisp bluring the line between data and code, so i'm starting to get an intuition of its benefit. I was just hoping to finaly see a real world example, and i'm a bit disappointed.