Hacker News new | ask | show | jobs
by Goladus 5719 days ago
I've used Python for about 4 years and am just starting to use Clojure, so I'll just add a few comments that others haven't mentioned. I'm not trying to offer a definitive comparison.

Clojure's data structures seem a lot like Python's but are a bit more elegant and avoid many of the little python headaches that come up often like d['k'] vs d.k vs d('k'). In clojure it would be (d :k) or (:k d) and both work. If you need memoization there are functions to help you. In clojure there definitely seems to be an attempt to make all the core data structures as compatible as possible (even having a formal abstraction (iSeq) for all of them)

Culturally, Python seems to care more about a minimal core language. Clojure.core has probably 3-4 times as many built-ins as Python. Many of the clojure functions are supporting features Python doesn't have or handles with syntax, like macros and conditional expressions, but there are also clojure functions like even?, that probably won't ever be a Python built-in.

Especially for predicates, functions like even?, every?, ffirst,

2 comments

> and avoid many of the little python headaches that come up often like d['k'] vs d.k vs d('k').

When does that come up? d['k'] is a key access (to a collection), d.k is an attribute access (to an object) and d('k') is a method call. I'm not sure where the headache/confusion would be here, unless you're trying to do very weird things with Python (which you should not)

They are all mapping functions, though. You have a key 'k', which is accepted by d and will return a unique result. Why should I have to care whether d is a collection, object, or method? In fact in many cases it's pretty easy to implement all three.

    class months(object):
        def __init__(self):
            m='jan feb mar apr may jun jul aug sep oct nov dec'.split(' ')
            n=range(1,13)
            self.__dict__.update(dict(zip(m,n)))
        def __getitem__(self, key):
            return self.__dict__[key]
        def __call__(self, key):
            return self.__dict__[key]
    
    d=months()
    
    print d['jan']  # 1
    print d('jan')  # 1
    print d.jan     # 1
> I'm not sure where the headache/confusion would be here, unless you're trying to do very weird things with Python (which you should not)
I never said there was confusion. The headache comes when you're trying decide whether to present an interface to a mapping function as a collection, an object, or a function.
Especially for predicates, functions like even?, every?, ffirst,

(For the record, this line supposed to be edited out, but apparently I forgot to actually delete it)