Hacker News new | past | comments | ask | show | jobs | submit login

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've exaggerated a bit, but that's the idea.



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

Search: